项目中使用vuejs, 随着页面越来越多,编译速度越来越慢,不得不提升一下,提升总结如下:
.开启cache
module: { rules: [ { test: /\.js$/, loader: ['babel-loader?cacheDirectory=true'] }, // ... 其他loader ] }
1. 开启cache
module.exports = { cache: true, // ... 其他配置 }2. 配置别名,让编译器更快找到相应的文件
resolve: { //... 其他配置 modules: [path.resolve(__dirname, '../../node_modules')], // node_modules文件夹所在的位置取决于跟webpack.base.conf.js相对的路径 alias: { //... 其他配置 api: path.resolve(__dirname, '../../server/api') // api文件所在的位置取决于跟webpack.base.conf.js相对的路径,在项目中会自动转换跟项目文件的相对路径 //... 其他配置 } }3. 解析js时需要的目录, 避免过度编译
{ test: /\.js$/, loader: 'happypack/loader?id=js', include: [path.join(__dirname, '..', 'asset')], exclude: file => ( /node_modules/.test(file) && !/\.vue\.js/.test(file) ) }4. 提取公用模块
plugins: [ new webpack.optimize.CommonsChunkPlugin({ async: 'shared-module', minChunks: (module, count) => ( count >= 2 // 当一个模块被重复引用2次或以上的时候单独打包起来。 ) }), //... ]5. 预编译第三方代码块
使用DllPlugin和DllReferencePlugin预编译
6. 启用多进程
先安装npm install happypack --save-dev
const os = require('os'); const HappyPack = require('happypack'); const happThreadPool = HappyPack.ThreadPool({size: os.cpus().length}); //... module.exports = { plugins: [ // ... new HappyPack({ id: 'js', loaders: ['babel-loader?cacheDirectory=true'], threadPool: happThreadPool, debug: true }) ], module: { rules: [ { test: /\.vue$/, loader: 'vue-loader', options: { loaders: { js: 'happypack/loader?id=js' } } }, { test: /\.js$/, loader: ['happypack/loader?id=js'] // ... }, //... ] } }
7. webpack-uglify-parallel并行压缩js代码
npm install webpack-uglify-parallel --save-dev
const UglifyJsparallelPlugin = require('webpack-uglify-parallel'); const os = require('os'); // ... 其他配置 plugins: [] new UglifyJsparallelPlugin({ workers: os.cpus().length, mangle: true, compressor: { warnings: false, drop_console: true, drop_debugger: true } })
评论