webpack3升级webpack4遇到的各种问题汇总

embedded/2024/10/21 11:41:49/

webpack3webpack4_0">webpack3升级webpack4遇到的各种问题汇总

问题1

var outputName=compilation.mainTemplate.applyPluginWaterfull('asset-path',outputOptions.filename,{......)TypeError: compilation.mainTemplate.applyPluginsWaterfall is not a function

在这里插入图片描述

解决方法

html-webpack-plugin 版本不兼容问题
升级 npm i --save-dev html-webpack-plugin@next

问题2

Module build failed(from ./node_modules/stylus-loader/index.js):
TypeError:cannot read property 'stylus' of undefined 

在这里插入图片描述

解决方法

需要将stylus-loader的版本更新到3.0.2,就可以
npm uninstall stylus-loader
npm install stylus-loader@3.0.2 --save-dev

问题3

internal/modules/cjs/loader.js:934
Error:cannot find module 'webpack/bin/config-yargs'

在这里插入图片描述

解决方法

这个就是目前版本的webpack-dev-server@2.11.5 不支持 webpack@4以上,需要重装一个webpack-dev-server是3.0版本以上就兼容

问题4

\node_modules\wepbpack\lib\TemplatePathPlugin.js: 
throw new Error(
Error:Path variable[contenthash:8] not implemented in this context:static/css/[name].[contenthash:8].css

在这里插入图片描述

Error: Chunk.entrypoints: Use Chunks.groupsIterable and 
filter by instanceof Entrypoint instead 

在这里插入图片描述

解决方法

extract-text-webpack-plugin还不能支持webpack4.0.0以上的版本。

npm install –save-dev extract-text-webpack-plugin@next 
会下载到+ extract-text-webpack-plugin@4.0.0-beta.0 

注意修改如下配置

config.plugins.push(new ExtractPlugin('styles.[md5:contentHash:hex:8].css')//将[contentHash:8].css改成[md5:contentHash:hex:8].css)

问题5

npm i xxx ,遇到安装包,安装直接报错,如下图

在这里插入图片描述

解决方法

npm i xxx  -D  --force  // 末尾添加--force

问题6

Uncaught TypeError: Cannot assign to read only property ‘exports’ 
of object '#<Object>'

在这里插入图片描述

解决方法

1、npm install babel-plugin-transform-es2015-modules-commonjs -D
2、在 .babelrc 中增加一个plugins
{"plugins": ["transform-es2015-modules-commonjs"]
}

问题7

Insufficient number of arguments or no entry found.

在这里插入图片描述

解决方法

webpack.config.js中的entry入口文件写错,注意自行修改

问题8

Module build failed: TypeError: Cannot read property ‘vue’ of undefined at Object.module.exports (…\node_modules\vue-loader\lib\loader.js:61:18)

在这里插入图片描述

解决方法

安装vue-loader@14.2.2即可
npm install vue-loader@14.2.2

问题9

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.

在这里插入图片描述
解决方法

// 给package.json文件中的scripts设置mode
"dev": "webpack --mode development",
"build": "webpack --mode production"

配置webpack.config.js文件

onst path = require('path');
module.exports = {entry: path.join(__dirname, './src/index.js'),//被打包文件所在的路径output: {path: path.join(__dirname, './dist'),//打包文件保存的路径filename: 'main.js'},mode: 'development' // 设置mode
}

问题10

在这里插入图片描述
webpack3 打包用 CommonsChunkPlugin
webpack4 打包用 SplitChunkPlugin

解决方法

webpack3的代码分割修改为 webpack4的写法即可

示例代码:
chainWebpack(config) {// 使用速度分析config.plugin('speed-measure-webpack-plugin').use(SpeedMeasurePlugin).end()// 删除懒加载模块的 prefetch preload,降低带宽压力(使用在移动端)config.plugins.delete('prefetch')config.plugins.delete('preload')// 路径别名config.resolve.alias.set('@', resolve('src')).set('assets', resolve('src/assets')).set('components', resolve('src/components')).set('pages', resolve('src/pages')).set('utils', resolve('src/utils'))config.when(process.env.NODE_ENV !== 'development', config => {// 生产打包优化config.plugin('ScriptExtHtmlWebpackPlugin').after('html').use('script-ext-html-webpack-plugin', [{// `runtime` must same as runtimeChunk name. default is `runtime`inline: /runtime\..*\.js$/}]).end()// 打包分割config.optimization.splitChunks({chunks: 'all',cacheGroups: {libs: {name: 'chunk-libs',test: /[\\/]node_modules[\\/]/,priority: 10,chunks: 'initial' // only package third parties that are initially dependent},// axios: {//   name: 'chunk-axios', // split axios into a single package//   priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app//   test: /[\\/]node_modules[\\/]_?axios(.*)/ // in order to adapt to cnpm// },lodash: {name: 'chunk-lodash', // split lodash into a single packagepriority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or apptest: /[\\/]node_modules[\\/]_?lodash(.*)/ // in order to adapt to cnpm},vantUI: {name: 'chunk-vantUI', // split vantUI into a single packagepriority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or apptest: /[\\/]node_modules[\\/]_?vant(.*)/ // in order to adapt to cnpm},// vue: {//   name: 'chunk-vue', // split vue into a single package//   priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app//   test: /[\\/]node_modules[\\/]_?@vue(.*)/ // in order to adapt to cnpm// },commons: {name: 'chunk-commons',test: resolve('src/components'), // can customize your rulesminChunks: 3, //  minimum common numberpriority: 5,reuseExistingChunk: true}}})// https:// webpack.js.org/configuration/optimization/#optimizationruntimechunkconfig.optimization.runtimeChunk('single')config.optimization.minimize(true)// 配置删除 console.logconfig.optimization.minimizer('terser').tap(args => {// remove debuggerargs[0].terserOptions.compress.drop_debugger = true// 移除 console.logif (process.env.VUE_APP_BUILD_DROP_CONSOLE == 'true') {args[0].terserOptions.compress.pure_funcs = ['console.log']}// 去掉注释 如果需要看chunk-vendors公共部分插件,可以注释掉就可以看到注释了args[0].terserOptions.output = {comments: false}return args})})}

以上就是我从webpack3升级webpack4遇到的问题,大家又遇到其他什么问题么,有的话评论区来!!!


http://www.ppmy.cn/embedded/30711.html

相关文章

[方法] Unity 解决类《原神》角色移动方向问题

第三人称视角类的游戏有很多&#xff0c;比如《原神》、《崩坏:星穹铁道》、《剑星》、《绝地求生》等。这些游戏中&#xff0c;角色的移动方向取决于玩家的输入和相机的方向&#xff0c;例如玩家在键盘上按下D键&#xff0c;则角色会相对于相机方向向右移动&#xff0c;本篇文…

搭建一个基于Python的Django框架后端、MySQL数据库、Vue前端以及Element UI组件库的图书管理系统

搭建一个基于Python的Django框架后端、MySQL数据库、Vue前端以及Element UI组件库的图书管理系统是一个复杂的项目,但我们可以将其分解为几个步骤来简化这个过程。以下是一个基本的步骤指南: 步骤 1: 安装并配置Python和Django 安装Python: 前往Python官网下载并安装适合您操…

VBA 读取sheet页中的指定区域数据,生成CSV文件

⏹待生成数据的sheet页 ⏹VBA代码 CreateObject("ADODB.Stream")&#xff1a;Microsoft ActiveX Data Objects (ADO) 库中的一个对象&#xff0c;用来处理文件的读写操作。Application.PathSeparator&#xff1a;系统默认的分隔符。Const startRowNum 4&#xff1a…

【doghead】ubuntu构建libuv

按照官方的文档2024年3月的版本。首先构建libuv 最终构建的还得了test 构建过程 zhangbin@DESKTOP-1723CM1:/mnt/d/XTRANS/thunderbolt/ayame/zhb-bifrost$ ls Bifrost-202403 README.md draw player-only worker 大神的带宽估计.png zhangbin@DESKTOP-1723CM1:/mnt/d/XTRANS/…

数据库[类型,基本概念,生活实例],登录mysql数据库的三种方式,修改sql编辑器界面样式及字体样式

数据库是按照特定方式组织起来的数据集合&#xff0c;它允许用户对数据进行高效的存储、检索和管理。数据库系统通常由两部分组成&#xff1a;数据库本身&#xff08;数据的物理存储&#xff09;和数据库管理系统&#xff08;DBMS&#xff0c;用于创建和管理数据库的软件&#…

Oracle系统参数调整【数据库实例优化系列一】

Oracle实例是:内存组件和相关的后台进程组成。这些内存组件提高了数据库的运行,而后台进程负责管理系统和内存组件。 一、SGA和实例优化 Oracle的SGA是指的系统全局区。sga是数据库运行期间使用的一段公有内存,即数据库用户都可以访问这段内存,包括: 共享池、重做日志缓冲…

第二十八章:Java中,`stream()`方法使用汇总

Java中&#xff0c;stream()方法使用汇总 目标 通过示例学习&#xff0c;掌握Javastream()方法的实践应用 在Java中&#xff0c;stream()方法用于将集合&#xff08;如List、Set等&#xff09;或数组转换为Stream流对象&#xff0c;以便进行各种流式操作。流&#xff08;Str…

http实现post请求时本地没问题,线上报413错误、nginx配置免费https、nginx反向代理

MENU 错误原因解决其他方式关于nginx的文章 错误原因 前端发送请求以后后端没有收到请求 而客户端却报了413错误 是请求实体过大的异常 如果请求夹带着文件就可能造成请求实体过大 那这里是什么原因造成的呢 在基础的后端开发中 都会用到nginx反向代理 默认大小为1M 超过1M都会…