美文网首页
webpack4性能优化

webpack4性能优化

作者: holidayPenguin | 来源:发表于2023-02-07 12:26 被阅读0次

优化手段介绍

  • speed-measure-webpack-plugin: 在打包的过程中,能够精确的帮你分析出每一个步骤耗费的时间,然后我们可以针对时间比较长的部分专门做优化。
  • babel-loader 的 cacheDirectory:将转译的结果缓存到文件系统中
  • eslint-loader 的 cache
  • cache-loader:对sass-loader、postcss-loader、vue-loader做打包缓存,缩短打包的时间。
  • happypack:可以在打包的时候开启多线程打包
  • webpack-parallel-uglify-plugin:UglifyJsPlugin这个包的打包时间太长,可以使用webpack-parallel-uglify-plugin,开启多核同步压缩增加压缩的效率。
  • hard-source-webpack-plugin:为模块提供中间缓存步骤

总之,最有用的还是webpackterser-webpack-plugin 缓存、babel-loader 的 cacheDirectoryeslint-loader 的 cachecache-loader也会有作用,happypack、`webpack-parallel-uglify-plugin``基本没啥用

开始

初始启动

项目第一次启动需要耗时40s左右,第二次启动耗时30s左右,这是因为webpack的 terser-webpack-plugin 缓起作用

babel-loader 的 cacheDirectory` 和 eslint-loader 的 cache

然后开启babel-loader 的 cacheDirectoryeslint-loader 的 cache,初次启动耗时增加了几秒(猜测可能在创建缓存),再次启动耗时在15s左右

babel-loader 会将转译的结果缓存到文件系统中。cacheDirectory 默认值为 false。当有设置时,指定的目录将用来缓存 loader 的执行结果。之后的 webpack 构建,将会尝试读取缓存,来避免在每次执行时,可能产生的、高性能消耗的 Babel 重新编译过程(recompilation process)。如果设置了一个空值 (loader: 'babel-loader?cacheDirectory')或者 true (loader: 'babel-loader?cacheDirectory=true'),loader 将使用默认的缓存目录node_modules/.cache/babel-loader,如果在任何根目录下都没有找到 node_modules 目录,将会降级回退到操作系统默认的临时文件目录。

{
  test: /\.js$/,
  loader: 'babel-loader?cacheDirectory=true',
  include: [resolve('src'), resolve('test'), resolve('config'), resolve('node_modules/webpack-dev-server/client')],
},

eslint-loader开启缓存

{
  test: /\.(js|vue)$/,
  enforce: 'pre',
  include: [resolve('src')],
  loader: 'eslint-loader',
  options: {
    formatter: require('eslint-friendly-formatter'),
    emitWarning: true,
    cache: true,
  },
},

cache-loader

再启用 cache-loader,初次启动耗时增加了几秒,再次启动耗时11.5s

const cacheLoader = {
  loader: 'cache-loader'
}

{
  test: /\.vue$/,
  use: [
    cacheLoader,
    {
      loader: 'vue-loader',
      options: vueLoaderConfig
    }
  ]
},

const loaders = options.usePostCSS ? [cacheLoader, cssLoader, postcssLoader] : [cacheLoader, cssLoader]

happypack

开启多线程 happypack,并未提升速度,而且时间还增加了,不知道是不是我没全部使用多线程的原因。

4-3 使用 HappyPack · 深入浅出 Webpack (wuhaolin.cn)

将常用的 loader 替换为 happypack/loader?id=xxxx,并使用 id 指定创建的 HappyPack 插件

const HappyPack = require('happypack');

module.exports = {
  module: {
    rules: [
      {
        test: /\.js$/,
        // 把对 .js 文件的处理转交给 id 为 babel 的 HappyPack 实例
        use: ['happypack/loader?id=babel'],
        // 排除 node_modules 目录下的文件,node_modules 目录下的文件都是采用的 ES5 语法,没必要再通过 Babel 去转换
        exclude: path.resolve(__dirname, 'node_modules'),
      },
    ]
  },
  plugins: [
    new HappyPack({
      // 用唯一的标识符 id 来代表当前的 HappyPack 是用来处理一类特定的文件
      id: 'babel',
      // 如何处理 .js 文件,用法和 Loader 配置中一样
      loaders: ['babel-loader?cacheDirectory'],
      // ... 其它配置项
    }),
  ],
};

webpack-parallel-uglify-plugin

webpack-parallel-uglify-plugin 没有试,不确定

hard-source-webpack-plugin

文档:hard-source-webpack-plugin · GitCode

hard-source-webpack-plugin 在另外一个飞冰的项目中使用,编译一两分钟的项目节省了10s钟,并且和 speed-measure-webpack-plugin 不能一起使用

npm install --save-dev hard-source-webpack-plugin or yarn add --dev hard-source-webpack-plugin

var HardSourceWebpackPlugin = require('hard-source-webpack-plugin');

module.exports = {
  context: // ...
  entry: // ...
  output: // ...
  plugins: [
    new HardSourceWebpackPlugin()
  ]
}

总结

webpack的 terser-webpack-plugin 缓存babel-loader 的 cacheDirectoryeslint-loader 的 cachecache-loader都会有作用,40s多的项目耗时减少了30s

参考

相关文章

  • Android性能优化 - 消除卡顿

    性能优化系列阅读 Android性能优化 性能优化 - 消除卡顿 性能优化 - 内存优化 性能分析工具 - Tra...

  • Android性能优化 - 内存优化

    性能优化系列阅读 Android性能优化 性能优化 - 消除卡顿 性能优化- 内存优化 性能分析工具 - Trac...

  • 前端性能优化(中)

    性能优化调研系列文章 《前端性能优化(上)》 《前端性能优化(中)》 《前端性能优化(下)》 《前端性能优化(上)...

  • 前端性能优化(下)

    性能优化调研系列文章 《前端性能优化(上)》 《前端性能优化(中)》 《前端性能优化(下)》 《前端性能优化(中)...

  • webpack4搭建react, 性能优化

    前几天用webpack4配置了下react,这里主要说下碰到的一些问题,细节可以看源代码。 hash缓存js和cs...

  • 使用Webpack4优化Web性能

    利用 Webpack 来优化 Web 性能属于加载性能优化 的一部分: ☛ Web Performance Opt...

  • Awesome Extra

    性能优化 性能优化模式 常见性能优化策略的总结 Spark 性能优化指南——基础篇 Spark 性能优化指南——高...

  • 常用的后端性能优化六种方式:缓存化+服务化+异步化等

    性能优化专题 前端性能优化 数据库性能优化 jvm和多线程优化 架构层面优化 缓存性能优化 常用的后端性能优化六大...

  • webpack 性能优化

    webpack性能优化 开发环境性能优化 生产环境性能优化 开发环境性能优化 优化打包构建速度 优化调试功能 生产...

  • iOS性能优化 - 整理

    本文主要包含: 性能优化 - 卡顿性能优化 - 耗电优化性能优化 - APP启动优化安装包瘦身 一  性能优化 -...

网友评论

      本文标题:webpack4性能优化

      本文链接:https://www.haomeiwen.com/subject/ctgxkdtx.html