webpack3.0升级4.0的方法步骤

作者:_kangzai 时间:2024-10-30 02:01:09 

1.webpack 3.11升级4.26

为了提升打包效率,在webpack3.11基础之上做了升级,webpack4.0发布以来,零配置的webpack对项目本身提供的“打包”和“压缩”功能已经做了优化,如果在项目开始使用4.0而不用vue-cli的默认配置,遇到的问题或许能少一些。

2. 安装/升级依赖

这些依赖有的是在build过程中发现依赖有新的替换或者报错,逐步替换的,如果想遇到多个坑,可以先把webpack、webpack-cli升级到对应版本

devDependencies:


"extract-text-webpack-plugin": "^4.0.0-beta.0"
"html-webpack-plugin": "^4.0.0-beta.14"
"mini-css-extract-plugin": "^0.9.0" (旧的optimize-css-assets-webpack不支持了)
"preload-webpack-plugin": "^3.0.0-beta.4"
"script-ext-html-webpack-plugin": "^2.1.3"
"vue-loader": "^15.3.0"
"webpack": "^4.26.1"
"webpack-cli": "^3.1.2"
"webpack-dev-server": "^3.2.1"
"script-ext-html-webpack-plugin": "^2.1.3"

3. 遇到的问题

升级webpack后,build报错:ERROR in ./src/App.vue
Module Error (from ./node_modules/vue-loader/lib/index.js

解决:升级vue-loader至15.3.0,

webpack.base.conf.js添加


const {VueLoaderPlugin} = require('vue-loader')
module.exports中添加
plugins:[new VueLoaderPlugin()]

修改配置文件 webpack.prod.conf.js:

webpack.optimize.CommonsChunkPlugin has been removed, please use config.optimization.splitChunks instead.

解决:


webpack.prod.conf.js webpackConfig配置:
optimization: {
noEmitOnErrors: true,
concatenateModules: true,
splitChunks: {
 chunks: 'all',
 name: 'common',
},
runtimeChunk: {
 name: 'runtime'
}
}
webpack.prod.conf.js webpackConfig配置:
optimization:{
namedModules: true
},

Plugin could not be registered at ‘html-webpack-plugin-before-html-processing'. Hook was not found.

解决:


npm i preload-webpack-plugin@next -D

Tapable.plugin is deprecated. Use new API on .hooks instead

问题:extract-text-webpack-plugin兼容问题 ,功能:extract css into its own file

解决: 卸载extract-text-webpack-plugin,安装mini-css-extract-plugin


new MiniCssExtractPlugin({
 filename: utils.assetsPath('css/[name].[contenthash].css'),
 allChunks: false,
}),

build出错:ERROR in TypeError: Cannot read property ‘hash' of undefined

解决:


 去掉这段
 //打包计时
 const SpeedMeasurePlugin = require("speed-measure-webpack-plugin");
 const smp = new SpeedMeasurePlugin();

WARNING in configuration
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.
You can also set it to ‘none' to disable any default behavior. Learn more: https://webpack.js.org/concepts/mode/

解决:webpackConfig添加 mode: ‘production'

Tapable.plugin is deprecated. Use new API on .hooks instead

解决:


npm i --save-dev extract-text-webpack-plugin@next

会下载到 extract-text-webpack-plugin@4.0.0-beta

Error: webpack.optimize.CommonsChunkPlugin has been removed, please use config.o ptimization…

解决:

  • 去掉 webpack.optimize.CommonsChunkPlugin相关配置

  • webpackConfig中与plugins的同级添加


optimization: {
splitChunks: {
  cacheGroups: {
   commons: {
    name: "commons",
    chunks: "initial",
    minChunks: 2
   }
  }
 }
},

Unhandled rejection Error: “dependency” is not a valid chunk sort mode


new HtmlWebpackPlugin({
 filename: config.build.index,
 template: 'index.html',
 inject: true,
 minify: {
 removeComments: true,
 collapseWhitespace: true,
 removeAttributeQuotes: true
 },
 // necessary to consistently work with multiple chunks via CommonsChunkPlugin
 chunksSortMode: 'dependency'
}),

解决:chunksSortMode改为auto或者注释

至此:npm run build 正常
接下来调npm run dev,直接运行成功

4. 总结

开启cache的情况下,原来打包时间22s左右,现在build最快8s左右

升级思路:

  1. 卸载webpack旧版本、安装新版本webpack, webpack-cli

  2. 遇到loader、plugin报错,升级loader、plugin,有的在4.0不支持,需要换成新的

  3. 卸载 ExtractTextPlugin、删除对应配置,改用 mini-css-extract-plugin

  4. 配置环境mode

  5. 其它wenpack3.0优化配置兼容处理

来源:https://blog.csdn.net/z2516305651/article/details/105181029

标签:webpack3.0,升级,4.0
0
投稿

猜你喜欢

  • JS代码格式化和语法着色V2

    2023-07-02 05:18:27
  • php去掉数组的第一个值的两个函数:array_shift、array_splice

    2023-05-29 02:26:07
  • JavaScript实现淘宝网图片的局部放大功能

    2023-08-25 05:44:56
  • mysql清空表数据的两种方式和区别解析

    2024-01-18 09:40:40
  • djang常用查询SQL语句的使用代码

    2022-04-27 02:59:51
  • python 装饰器功能以及函数参数使用介绍

    2022-04-03 05:12:32
  • Go项目配置管理神器之viper的介绍与使用详解

    2023-09-17 01:05:52
  • python爬虫中采集中遇到的问题整理

    2022-10-17 03:32:23
  • Python实现一个转存纯真IP数据库的脚本分享

    2024-01-17 16:43:38
  • Python实现微信机器人的方法

    2023-01-20 17:33:26
  • Python一个简单的通信程序(客户端 服务器)

    2023-09-09 11:09:39
  • MySQL SQL 语法参考

    2024-01-17 15:50:05
  • .Net行为型设计模式之迭代器模式(Iterator)

    2024-06-05 09:27:47
  • Python reversed函数及使用方法解析

    2023-01-24 16:10:53
  • PyQt5实现简单数据标注工具

    2021-10-24 04:55:54
  • Jquery对数组的操作技巧整理

    2024-04-22 22:32:52
  • Python闭包与闭包陷阱举例详解

    2023-07-05 09:40:10
  • Dhtml 属性编程之原创笔记

    2014-07-23 12:51:54
  • 不完全HTML在线编辑器收集

    2007-11-08 12:20:00
  • 使用pandas实现连续数据的离散化处理方式(分箱操作)

    2023-08-28 21:45:51
  • asp之家 网络编程 m.aspxhome.com