编译耗时分析

speed-measure-webpack-plugin 插件能够列出输出、loader和plugins的耗时

Taro项目中引入插件

安装
  1. npm install --save-dev speed-measure-webpack-plugin

修改@tarojs/mini-runner/dis/index.js
  1. // Taro 是在@tarojs/mini-runner/dis/index.js 文件中,调用了 webpack 进行打包
  2. const speedMeasurePlugin = require("speed-measure-webpack-plugin");
  3. const smp = new speedMeasurePlugin();
  4. ......
  5. function build(appPath, config) {
  6. ...
  7. let webpackConfig = webpackChain.toConfig();
  8. const onBuildFinish = config.onBuildFinish;
  9. webpackConfig = smp.wrap(webpackConfig);
  10. const compiler = webpack(webpackConfig);
  11. return new Promise((resolve, reject) => {
  12. ...
  13. })
  14. }
  15. ......

编译结果

image.png

多进程打包 thread-loader & 缓存 cache-loader,babel-loader

安装
  1. npm install --save-dev thread-loader cache-loader babel-loader

webpack plugins
  1. // config/plugins/comoilerPlu.js
  2. module.exports = (ctx) => {
  3. ctx.modifyWebpackChain(args => {
  4. const chain = args.chain
  5. chain.module.rules.delete('script')
  6. chain.merge({
  7. module: {
  8. rule: {
  9. script: {
  10. test: /\.[tj]sx?$/i,
  11. use: {
  12. threadLoader: {
  13. loader: 'thread-loader',
  14. options: {
  15. workers: 3,
  16. workerParallelJobs: 30,
  17. }
  18. }
  19. },
  20. },
  21. },
  22. },
  23. })
  24. chain.module.rule('scss').oneOf('0').use('cacheLoader').loader('cache-loader').before('1')
  25. chain.module.rule('scss').oneOf('1').use('cacheLoader').loader('cache-loader').before('1')
  26. })
  27. }
  28. // config/index.js 引入插件 plugins/compilerPlu.js
  29. ......
  30. const config = {
  31. ...
  32. plugins: [
  33. '@tarojs/plugin-sass',
  34. '@tarojs/plugin-terser',
  35. path.resolve(__dirname, 'plugins/compilerPlu.js')
  36. ],
  37. ...
  38. }
  39. ......

编译结果
image.pngimage.png

结论

引入插件后编译时间没有明显改变

terser-webpack-plugin 文件压缩

‘@tarojs/plugin-terser’, 插件中已引入此插件

参考文档

编写插件,将 Taro 编译打包耗时缩短至三分之一