diff --git a/packages/webpack5/__tests__/configuration/base.spec.ts b/packages/webpack5/__tests__/configuration/base.spec.ts index df76f6ffda..eeed190d70 100644 --- a/packages/webpack5/__tests__/configuration/base.spec.ts +++ b/packages/webpack5/__tests__/configuration/base.spec.ts @@ -101,4 +101,14 @@ describe('base configuration', () => { force: true, }); }); + + it('supports --env.profile', () => { + init({ + platform: 'ios', + profile: true, + }); + const config = base(new Config()); + + expect(config.get('profile')).toBe(true); + }); }); diff --git a/packages/webpack5/src/bin/index.ts b/packages/webpack5/src/bin/index.ts index b8e1656bbe..87ceed5516 100644 --- a/packages/webpack5/src/bin/index.ts +++ b/packages/webpack5/src/bin/index.ts @@ -1,6 +1,6 @@ #!/usr/bin/env node -import { redBright, green, greenBright } from 'chalk'; +import { redBright, green, greenBright, yellow } from 'chalk'; import { program } from 'commander'; import dedent from 'ts-dedent'; import webpack from 'webpack'; @@ -115,6 +115,28 @@ program errorDetails: env.verbose, }) ); + + // if webpack profile is enabled we write the stats to a JSON file + if (configuration.profile || env.profile) { + console.log( + [ + '', + '|', + `| The build profile has been written to ${yellow( + 'webpack.stats.json' + )}`, + `| You can analyse the stats at ${green( + 'https://webpack.github.io/analyse/' + )}`, + '|', + '', + ].join('\n') + ); + fs.writeFileSync( + path.join(process.cwd(), 'webpack.stats.json'), + JSON.stringify(stats.toJson()) + ); + } } }; diff --git a/packages/webpack5/src/configuration/base.ts b/packages/webpack5/src/configuration/base.ts index 0c2659f6d8..20d190e779 100644 --- a/packages/webpack5/src/configuration/base.ts +++ b/packages/webpack5/src/configuration/base.ts @@ -212,6 +212,11 @@ export default function (config: Config, env: IWebpackEnv = _env): Config { }); }); + // enable profiling with --env.profile + config.when(env.profile, (config) => { + config.profile(true); + }); + // worker-loader should be declared before ts-loader config.module .rule('workers') diff --git a/packages/webpack5/src/index.ts b/packages/webpack5/src/index.ts index a8ca9ffe70..9b7053d4ce 100644 --- a/packages/webpack5/src/index.ts +++ b/packages/webpack5/src/index.ts @@ -43,6 +43,9 @@ export interface IWebpackEnv { // enable verbose output verbose?: boolean; + // enable webpack profiling + profile?: boolean; + // misc replace?: string[] | string; }