之前的文章《你值得了解的VIM中文亂碼的問題(分享)》中,給大家了解了VIM中文亂碼的問題。下面本篇文章給大家了解webpack3升級(jí)到webpack4版本遇到的問題,伙伴們來看看吧。
據(jù)說webpack3
比webpack4
編譯速度將近快了 60%-80%。
成功升級(jí)之后,于是來記錄下,項(xiàng)目主要是vue ^2.5.9
,webpack ^4.10.2
,webpack-dev-sever ^3.1.4
,配合升級(jí)的還有vue-loader ^15
項(xiàng)目重現(xiàn)編譯之后由原來的1.7MB
減少到1.1MB
,看來在壓縮這塊也是由效果的。
需要修改的地方有以下幾點(diǎn):
vue-loader14
到15
需要增加如下配置
const VueLoaderPlugin = require('vue-loader/lib/plugin') ++++ const MiniCssExtractPlugin = require('mini-css-extract-plugin') // webpack 4 +++ const ExtractTextPlugin = require('extract-text-webpack-plugin') //for webpack3 ----- module.exports = { ... plugins: [ + new VueLoaderPlugin(), ++++ + new MiniCssExtractPlugin({filename:'mian.css'}) //for webpack 4 +++ - new ExtractTextPlugin({filename:'main.css'}) //for webpack 3 --- ] ... }
webpack-dev-server
升級(jí)之后需做如下改動(dòng)
devServer: { ++ contentBase: path.resolve(__dirname, '../dos-html'), // 需要指定路徑 ++ port: 7001, hot: true, // open: false, inline: true, compress: true, historyApiFallback: true, .... },
webpack3
升級(jí)4
之后需要改動(dòng)的配置
plugins: [ //已經(jīng)移除 new webpack.optimize.CommonsChunkPlugin({ name: 'vendor', minChunks: function (module) { // any required modules inside node_modules are extracted to vendor return ( module.resource && /.js$/.test(module.resource) && module.resource.indexOf( path.join(__dirname, '../node_modules')) === 0 ) } }), new webpack.optimize.UglifyJsPlugin(...)//已經(jīng)移除 } // ===> 修改為以下 const UglifyJsPlugin = require('uglifyjs-webpack-plugin'); moudel.exports = { mode: 'production', ++ 這里指定模式。 ... optimization: { splitChunks: { name(module) { return ( module.resource && /.js$/.test(module.resource) && module.resource.indexOf(path.join(__dirname, '../node_modules')) === 0 ) } }, minimize: true, minimizer: [ new UglifyJsPlugin({ uglifyOptions: { compress: { warnings: false, // drop_debugger: true, // drop_console: true }, sourceMap: false } }) ] }, ... }
其他的各種報(bào)錯(cuò)信息,注意看,可能是模塊版本太低了吧,都升級(jí)下就OK了。
【完】
推薦學(xué)習(xí):Web pack入門視頻教程