Skip to content

Commit

Permalink
fix: webpack require failed in config file (#6225)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc authored Mar 8, 2021
1 parent 00074b9 commit 92c6f88
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 1 deletion.
6 changes: 6 additions & 0 deletions packages/bundler-webpack/src/requireHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ export function getFileName(filePath: string) {
return filePath.split('/').slice(-1)[0];
}

let inited = false;

export function init() {
// Allow run once
if (inited) return;
inited = true;

const filesMap = files.map((file) => {
const fileName = getFileName(file);
return [file, `@umijs/deps/compiled/webpack/${fileName}`];
Expand Down
1 change: 1 addition & 0 deletions packages/umi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"access": "public"
},
"dependencies": {
"@umijs/bundler-webpack": "3.4.0-beta.5",
"@umijs/core": "3.4.0-beta.5",
"@umijs/deps": "0.2.25",
"@umijs/preset-built-in": "3.4.0-beta.5",
Expand Down
3 changes: 3 additions & 0 deletions packages/umi/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Service } from './ServiceWithBuiltIn';
import fork from './utils/fork';
import getCwd from './utils/getCwd';
import getPkg from './utils/getPkg';
import initWebpack from './initWebpack';

// process.argv: [node, umi.js, command, args]
const args = yParser(process.argv.slice(2), {
Expand Down Expand Up @@ -50,6 +51,8 @@ if (args.version && !args._[0]) {
const name = args._[0];
if (name === 'build') {
process.env.NODE_ENV = 'production';
// Init webpack version determination and require hook for build command
initWebpack();
}
await new Service({
cwd: getCwd(),
Expand Down
5 changes: 4 additions & 1 deletion packages/umi/src/forkedDev.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { join } from 'path';
import { chalk, yParser } from '@umijs/utils';
import { Service } from './ServiceWithBuiltIn';
import getCwd from './utils/getCwd';
import getPkg from './utils/getPkg';
import initWebpack from './initWebpack';

const args = yParser(process.argv.slice(2));

(async () => {
try {
process.env.NODE_ENV = 'development';
// Init webpack version determination and require hook
initWebpack();

const service = new Service({
cwd: getCwd(),
pkg: getPkg(process.cwd()),
Expand Down
39 changes: 39 additions & 0 deletions packages/umi/src/initWebpack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { init } from '@umijs/deps/compiled/webpack';
import { init as initRequreHook } from '@umijs/bundler-webpack/lib/requireHook';
import { existsSync, readFileSync } from 'fs';
import { join } from 'path';

const DEFAULT_CONFIG_FILES = [
'.umirc.ts',
'.umirc.js',
'config/config.ts',
'config/config.js',
];

function getConfigFile(opts: { cwd: string }) {
const configFile = DEFAULT_CONFIG_FILES.filter((file) => {
return existsSync(join(opts.cwd, file));
})[0];
return configFile ? join(opts.cwd, configFile) : null;
}

export default () => {
// 1. read user config
// 2. if have webpack5:
// 3. init webpack with webpack5 flag

const configFile = getConfigFile({ cwd: process.cwd() });
const configContent = configFile ? readFileSync(configFile, 'utf-8') : '';

// TODO: detect with ast
const haveWebpack5 = configContent.includes('webpack5:');

if (haveWebpack5) {
process.env.USE_WEBPACK_5 = '1';
init(true);
} else {
init();
}

initRequreHook();
};

0 comments on commit 92c6f88

Please sign in to comment.