一: 简单查看编译耗时
webpack5默认是开启profile的,
webpack.config.js
module.exports = {entry: './src/index.js',output: {filename: 'main.js'},
}
npm i core-js
import 'core-js/stable'
src/index.js
const util = () => {return 'util'
}let zhang = util()zhang += 'zhang'
打包输出信息
> demo01@1.0.0 build
> webpackasset main.js 1010 KiB [emitted] (name: main)
runtime modules 1.13 KiB 5 modules
modules by path ./node_modules/core-js/modules/*.js 338 KiB./node_modules/core-js/modules/es.symbol.js 322 bytes [built] [code generated]./node_modules/core-js/modules/es.json.stringify.js 2.98 KiB [built] [code generated]./node_modules/core-js/modules/es.symbol.description.js 2.54 KiB [built] [code generated]./node_modules/core-js/modules/es.symbol.async-iterator.js 230 bytes [built] [code generated]+ 295 modules
modules by path ./node_modules/core-js/internals/*.js 226 KiB./node_modules/core-js/internals/path.js 85 bytes [built] [code generated]./node_modules/core-js/internals/export.js 2.63 KiB [built] [code generated]./node_modules/core-js/internals/global.js 649 bytes [built] [code generated]./node_modules/core-js/internals/object-get-own-property-descriptor.js 1.11 KiB [built] [code generated] + 256 modules
./src/demo0513/index.js 136 bytes [built] [code generated]
./node_modules/core-js/stable/index.js 11.6 KiB [built] [code generated]
webpack 5.91.0 compiled successfully in 1061 ms
可以看到编译耗时1061ms
二: 查看各个阶段耗时
webpack.config.js
const SpeedMeasurePlugin = require("speed-measure-webpack-plugin");
const smp = new SpeedMeasurePlugin(); const config = {entry: './src/index.js',output: {filename: 'main.js'},mode: 'development',
}// 使用插件包装你的配置
module.exports = smp.wrap(config);
npm i core-js
src/index.js
import 'core-js/stable'const util = () => {return 'util'
}let zhang = util()zhang += 'zhang'
> demo01@1.0.0 build
> webpack(node:24240) [DEP_WEBPACK_COMPILATION_NORMAL_MODULE_LOADER_HOOK] DeprecationWarning: Compilation.hooks.normalModuleLoader was moved to NormalModule.getCompilationHooks(compilation).loader
(Use `node --trace-deprecation ...` to show where the warning was created)SMP ⏱
General output time took 1.038 secsSMP ⏱ Loaders
modules with no loaders took 0.647 secsmodule count = 561asset main.js 1010 KiB [compared for emit] (name: main)
runtime modules 1.13 KiB 5 modules
modules by path ./node_modules/core-js/modules/*.js 338 KiB./node_modules/core-js/modules/es.symbol.js 322 bytes [built] [code generated]./node_modules/core-js/modules/es.json.stringify.js 2.98 KiB [built] [code generated]./node_modules/core-js/modules/es.symbol.description.js 2.54 KiB [built] [code generated]./node_modules/core-js/modules/es.symbol.async-iterator.js 230 bytes [built] [code generated]+ 295 modules
modules by path ./node_modules/core-js/internals/*.js 226 KiB./node_modules/core-js/internals/path.js 85 bytes [built] [code generated]./node_modules/core-js/internals/export.js 2.63 KiB [built] [code generated]./node_modules/core-js/internals/global.js 649 bytes [built] [code generated]./node_modules/core-js/internals/object-get-own-property-descriptor.js 1.11 KiB [built] [code generated] + 256 modules
./src/demo0513/index.js 136 bytes [built] [code generated]
./node_modules/core-js/stable/index.js 11.6 KiB [built] [code generated]
webpack 5.91.0 compiled successfully in 1043 ms
从 SMP 的信息可以看到
构建耗时: 1.038s (和webpack打印的1043ms差不多)
有 561 个模块没有使用任何loader来处理,耗时0.647s