From 04b87c03b3676d836abf119e768dc3e9fd4d425b Mon Sep 17 00:00:00 2001 From: Malash Date: Tue, 14 Nov 2023 13:07:21 +0800 Subject: [PATCH] add `parallel` option in goji.config.js --- packages/cli/src/config/loaders.ts | 20 ++++++++++---------- packages/cli/src/config/webpack.config.ts | 8 +++++++- packages/cli/src/index.ts | 10 ++++++++++ 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/packages/cli/src/config/loaders.ts b/packages/cli/src/config/loaders.ts index 547617ff..db03de11 100644 --- a/packages/cli/src/config/loaders.ts +++ b/packages/cli/src/config/loaders.ts @@ -7,23 +7,23 @@ export const getEnvForPreprocess = (nodeEnv: string, target: GojiTarget) => ({ TARGET: target, }); -// `thread-loader` enable multi-thread compiling for Webpack const MOST_ECONOMICAL_WORKER_COUNT = 3; +const getDefaultWorkerCount = () => { + // `thead-loader`'s workers default to `os.cpus().length - 1` + // after benchmarking I believe 1 master + 3 workers = 4 thread should be the most economical config on most cpus + // so we use `max(1, min(core_count - 1, MOST_ECONOMICAL_WORKER_COUNT))` as worker count + const threadCount = os.cpus()?.length ?? 1; + return Math.max(1, Math.min(threadCount - 1, MOST_ECONOMICAL_WORKER_COUNT)); +}; // FIXME: cannot use `thread-loader`'s `warmup` because of this issue // https://github.com/webpack-contrib/thread-loader/issues/122 -export const getThreadLoader = (nodeEnv: string): Array => { - // disable `thread-loader` on CI - if (process.env.CI === 'true') { +export const getThreadLoader = (nodeEnv: string, parallel?: number): Array => { + if (typeof parallel === 'number' && parallel <= 1) { return []; } - // `thead-loader`'s workers default to `os.cpus().length - 1` - // after benchmarking I believe 1 master + 3 workers = 4 thread should be the most economical config on most cpus - // so we use `max(1, min(core_count - 1, MOST_ECONOMICAL_WORKER_COUNT))` as worker count - const threadCount = os.cpus()?.length ?? 1; - const workerCount = Math.max(1, Math.min(threadCount - 1, MOST_ECONOMICAL_WORKER_COUNT)); const options = { - workers: workerCount, + workers: typeof parallel === 'number' ? parallel - 1 : getDefaultWorkerCount(), // no need to kill the worker in dev mode for better re-build performance poolTimeout: nodeEnv === 'production' ? 2000 : Infinity, }; diff --git a/packages/cli/src/config/webpack.config.ts b/packages/cli/src/config/webpack.config.ts index 81530590..bad25fea 100644 --- a/packages/cli/src/config/webpack.config.ts +++ b/packages/cli/src/config/webpack.config.ts @@ -43,6 +43,7 @@ export const getWebpackConfig = ({ watch, progress, nohoist, + parallel, }: { basedir: string; outputPath?: string; @@ -52,8 +53,12 @@ export const getWebpackConfig = ({ watch: boolean; progress: boolean; nohoist?: GojiWebpackPluginOptions['nohoist']; + parallel?: { + minimize?: number; + loader?: number; + }; }): webpack.Configuration => { - const threadLoaders = getThreadLoader(nodeEnv); + const threadLoaders = getThreadLoader(nodeEnv, parallel?.loader); const CSS_FILE_EXT = { wechat: 'wxss', @@ -99,6 +104,7 @@ export const getWebpackConfig = ({ }, }, extractComments: false, + parallel: parallel?.minimize, }), ], // set `optimization.splitChunks` and `optimization.splitChunks` to `false` diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index 6dc0d55a..8a2c54e7 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -13,6 +13,12 @@ interface GojiConfig { configureBabel?: (config: any) => any; progress?: boolean; nohoist?: GojiWebpackPluginOptions['nohoist']; + parallel?: + | { + minimize?: number; + loader?: number; + } + | number; } const GOJI_CONFIG_FILE_NAME = 'goji.config'; @@ -60,6 +66,10 @@ const main = async () => { watch, progress: gojiConfig.progress ?? cliConfig.progress ?? true, nohoist: gojiConfig?.nohoist, + parallel: + typeof gojiConfig.parallel === 'number' + ? { loader: gojiConfig.parallel, minimize: gojiConfig.parallel } + : gojiConfig.parallel, }); // apply goji.config.js configureWebpack