vue项目编译慢,热加载慢的这里看这里瞧了!

2,482 阅读1分钟

刚入职就接手项目优化,好家伙几百兆的图片,地图包。命令行一跑就内存溢出(详见-Vue项目过大,编译时内存溢出问题)。编译时长六七分钟,热重载一次,五分钟往上。

环境是vue-cli3。百度找了个遍发现了dll插件,但是用上后,效果微乎其微。就在无解的时候,在stackOverflow上发现一大神的帖子。 介绍了 hard-source-webpack-plugin ,是一个依赖中间件,把依赖转为缓存存入。速度一下子提升80%。这下好了,又可以摸鱼了!!!

下载插件

npm install hard-source-webpack-plugin -D

vue.config.js里配置

const HardSourceWebpackPlugin= require(``'hard-source-webpack-plugin')

configureWebpack: {
  //缓存设置
  plugins: [new HardSourceWebpackPlugin({
    cacheDirectory: 'node_modules/.cache/hard-source/[confighash]',
    recordsPath: 'node_modules/.cache/hard-source/[confighash]/records.json',
    configHash: function (webpackConfig) {
      // node-object-hash on npm can be used to build this.
      return require('node-object-hash')({ sort: false }).hash(webpackConfig);
    },
    environmentHash: {
      root: process.cwd(),
      directories: [],
      files: ['package-lock.json', 'yarn.lock'],
    },
  })],

然后第一遍编译会慢一点和以前一样,第二遍编译就会快起来了。速度杠杠的!!!

我是二毛,祝你幸福!