noParse
--不解析模塊中的依賴關系 提高打包效率
webpack.config.js
module: { noParse: /jquery/, // 不解析模塊中的依賴關系 提高打包效率 rules: [ { test: /\.js$/, use: { loader: 'babel-loader', options: { presets: [ '@babel/preset-env', '@babel/preset-react' ] } } } ] }
IgnorePlugin
--忽略第三方包指定目錄,讓這些指定目錄不要被打包進去
webpack.config.js
plugins: [ new webpack.IgnorePlugin(/\.\/locale/, /moment/), // 在 moment 引入.locale 語言包時 忽略 減小打包體積 ]
index.js
// import moment from 'moment'; // // 手動引用中文包 // import "moment/locale/zh-cn"; // // 設置語言包 // moment.locale('zh-cn'); // 中文 // let date = moment().endOf('day').fromNow(); // console.log(date)
happypack
--多線程打包
無需多線程時,盡量不要使用happypack 會消耗打包性能
let path = require('path'); let HtmlWebpackPlugin = require('html-webpack-plugin'); let webpack = require('webpack'); let Happypack = require('happypack'); // 多線程打包 happypack module.exports = { mode: 'production', entry: { home: './src/home.js' }, output: { filename: 'home.js', path: path.resolve(__dirname, 'dist') }, plugins: [ new Happypack({ id: 'js', use: [ { loader: 'babel-loader', options: { presets: [ '@babel/preset-env', '@babel/preset-react' ] } } ] }), new HtmlWebpackPlugin({ template: './index.html', filename: 'index.html' }) ], module: { rules: [ { test: /\.js$/, // exclude: /node_modules/, // 優化打包文件 // include:path.resolve('src'), // 優化打包文件 use: 'Happypack/loader?id=js' // 多線程 id匹配js css也可以多線程打包 } ] } }
