Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parallel option in goji.config.js #224

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions packages/cli/src/config/loaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<webpack.RuleSetRule> => {
// disable `thread-loader` on CI
if (process.env.CI === 'true') {
export const getThreadLoader = (nodeEnv: string, parallel?: number): Array<webpack.RuleSetRule> => {
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,
};
Expand Down
8 changes: 7 additions & 1 deletion packages/cli/src/config/webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export const getWebpackConfig = ({
watch,
progress,
nohoist,
parallel,
}: {
basedir: string;
outputPath?: string;
Expand All @@ -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',
Expand Down Expand Up @@ -99,6 +104,7 @@ export const getWebpackConfig = ({
},
},
extractComments: false,
parallel: parallel?.minimize,
}),
],
// set `optimization.splitChunks` and `optimization.splitChunks` to `false`
Expand Down
10 changes: 10 additions & 0 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand Down
Loading