You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
plugins: [// // split vendor js into its own file// new webpack.optimize.CommonsChunkPlugin({// name: 'vendor',// }),// // extract webpack runtime and module manifest to its own file in order to// // prevent vendor hash from being updated whenever app bundle is updated// new webpack.optimize.CommonsChunkPlugin({// name: 'manifest',// minChunks: Infinity,// }),// // This instance extracts shared chunks from code splitted chunks and bundles them// // in a separate chunk, similar to the vendor chunk// // see: https://webpack.js.org/plugins/commons-chunk-plugin/#extra-async-commons-chunk// new webpack.optimize.CommonsChunkPlugin({// name: 'app',// async: 'vendor-async',// children: true,// minChunks: 3,// }),];
// const ExtractTextPlugin = require('extract-text-webpack-plugin');// plugins:[// extract css into its own file// new ExtractTextPlugin({// filename: utils.assetsPath('css/[name].[contenthash].css'),// // Setting the following option to `false` will not extract CSS from codesplit chunks.// // Their CSS will instead be inserted dynamically with style-loader when the codesplit chunk has been loaded by// // webpack. It's currently set to `true` because we are seeing that sourcemaps are included in the codesplit// // bundle as well when it's `false`, increasing file size: https://github.com/vuejs-templates/webpack/issues/1110// allChunks: true,// }),// ]// extract: true// if (options.extract) {// return ExtractTextPlugin.extract({// use: loaders,// fallback: 'vue-style-loader',// });// }
The 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment.
mode: "production";
升级 vue-loader
"vue-loader": "^13.3.0" -> "vue-loader": "14.2.2"
TypeError: Cannot read property 'vueOptions' of undefined
yarnadd-Dvue-loader@latest
vue-loader was used without the corresponding plugin. Make sure to include VueLoaderPlugin in your webpack config
// https://github.com/symfony/webpack-encore/issues/311Youprobablyusevue-loaderv15whichwasreleasedyesterdayandintroducesalotofchangescomparedtov14.Oneofthesechangesisthatyouhavetouseanextraplugin: VueLoaderPlugin(that's not handled yet by Encore).
Inthemeantimecouldyoutryremovingyourversionofthevue-loader/VueLoaderPluginandaddingvue-loader@^14.2.2instead?
yarnadd-Dvue-loader@14.2.2
(1:1)Unknownword>1|// extracted by mini-css-extract-plugin
AssetsOverSizeLimitWarning: asset size limit: The following asset(s) exceed the recommended size limit (244 KiB 250000Byte).
This can impact web performance.
// webpack Error: Callback was already called.// https://github.com/webpack-contrib/mini-css-extract-plugin/issues/493// webpack.dev.js
plugins:[newMiniCssExtractPlugin(),]
// https://segmentfault.com/q/1010000012054980// BaseClient.js:12 Uncaught TypeError: Cannot assign to read only property 'exports' of object '#<Object>'
constTerserPlugin=require('terser-webpack-plugin');module.exports={optimization: {minimize: true,minimizer: [// Compress extracted CSS. We are using this plugin so that possible// duplicated CSS from different presentation can be deduped.newOptimizeCSSPlugin({cssProcessorOptions: config.build.productionSourceMap ? {safe: true,map: {inline: false}} : {safe: true},}),newTerserPlugin({cache: true,parallel: true,sourceMap: Boolean(config.build.productionSourceMap),}),],}}
之前尝试过一些在webpack3的基础上做的构建优化,例如引入HappyPack优化构建速度,开启loader缓存和优化包查找路径等等,详情可以查看前端webpack构建优化
但是随着时间的推移,这种优化产生的效果越来越弱化,手上的项目体积越来越大,对本地开发热更新速度和生产打包发布速度都有了很大的影响。
webpack3升级到webpack4迫在眉睫,这篇博文将记录一些我在升级过程中遇到的坑。
当你遇到这些坑时,通过搜索引擎找到我这篇文章,如果能够解决了手上的webpack配置问题,然后发自内心的感到 ”Save my day!“,”It helps me!“,”Solved my problem!“,”Works for me!“ ,我会感觉自己的这篇博文很有意义。
升级 webpack 到 4
"webpack": "^3.6.0" -> "webpack": "^4.43.0"
移除 CommonsChunkPlugin
升级 html-webpack-plugin
"html-webpack-plugin": "^2.30.1" -> "html-webpack-plugin": "^4.3.0"
移除 extract-text-webpack-plugin,引入 mini-css-extract-plugin 并配置 css-loader
配置 mode 属性
升级 vue-loader
"vue-loader": "^13.3.0" -> "vue-loader": "14.2.2"
移除 postcss-loader。
// postcss: generateLoaders()
更新 HtmlWebpackPlugin 的 chunkSortMode
设置为 HtmlWebpackPlugin 的 chunkSortMode 为"auto": https://github.com/jantimon/html-webpack-plugin#options
修复大小限制的报错
生成 manifest.js,生成 vendors.js
调试开发环境可用
安装transform-es2015-modules-commonjs并且在.babelrc中配置。
引入analyzer分析分析包大小
webpack3与webpack4打包对比
机器参数:
MacBook Pro (15-inch, 2019)
处理器 2.3 GHz Intel Core i9
内存 16 GB 2400 MHz DDR4
文件可以更小一些吗?构建速度可以更快一些吗?
引入TerserPlugin的话,需要首先升级node到v10.17.0+。
增加下面的配置:
现在的webpack3和webpack4打包分析:
通过对比发现,提升了大概5秒的打包速度。
升级vue-loader到v15并且引入thread-loader加速vue-loader
为什么引入thread-loader加速vue-loader?
因为HappyPack无法加速vue-loader15。
vuejs/vue-loader#1273
顺便升级eslint-loader到4。
"eslint-loader": "^1.7.1"->"eslint-loader": "^4.0.2"
现在的webpack3和webpack4打包分析:
webpack3与webpack4开发依赖对比
总结
webpack3到webpack4的升级,主要做了以下这些事情
反思
The text was updated successfully, but these errors were encountered: