如何将webpack3升级至webpack4
背景#
原项目由vue-cli2快速构建, 几年过去了webpack5已经发布,迫于项目逐渐扩大且业务需求, webpack版本升级迫在眉睫。 以下记录了升级过程及问题解决方案
难点#
相关最新安装包不匹配造成无法按照预期运行
针对development#
升级webpack及周边#
⚠️webpack-dev-server需要与webpack匹配, 否则报错
添加mode (v4新)#
参照官网指南, 移除废弃API#
https://v4.webpack.docschina.org/migrate/4/
- 移除
NamedModulesPlugin,NoEmitOnErrorsPlugin插件
- 移除DefinePlugin
- UglifyJsPlugin
升级html编译插件html-webpack-plugin#
错误提示:compilation.mainTemplate.applyPluginsWaterfall('asset-path', outputOptions.filename
升级vue-loader,使用VueLoaderPlugin替换vueLoaderConfig#
错误提示: Cannot read property 'vue' of undefined
移除vueLoaderConfig, 增加VueLoaderPlugin ⚠️ options 字段改为 loader
移除webpack-dev-server/client#
错误提示:Cannot assign to read only property 'exports' of object '[object Object]' 移除resolve('node_modules/webpack-dev-server/client')
针对production#
废弃CommonsChunkPlugin由optimization 代替#
webpack.optimize.CommonsChunkPlugin has been removed
- vendor
- manifest
- vendor-async
废弃extract-text-webpack-plugin由mini-css-extract-plugin代替#
Chunk.entrypoints: Use Chunks.groupsIterable and filter
webpack.prod.js
告警: WARNING in chunk 1 [mini-css-extract-plugin] Conflicting order.
utils.js
修改chunksSortMode#
错误提示: Unhandled rejection Error: "dependency" is not a valid chunk sort mode
告警: Unhandled rejection Error: "dependency" is not a valid chunk sort mode#
WARNING in asset size limit: The following asset(s) exceed the recommended size limit (244 KiB). This can impact web performance.
升级成果: 打包时间提升 26.5%, 体积减小65.5%#
13831ms -> 10160ms 14.78s 16M -> 6M
影响不大的升级#
升级webpack-merge#
升级webpack-bundle-analyzer#
升级plugin#
升级css#
错误提示:Error: true is not a PostCSS plugin
升级eslint#
参考: