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

devtools/2024/9/23 9:35:35/

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/devtools/25327.html

相关文章

使用 Docker 自建一款怀旧游戏之 - 扫雷

1&#xff09;扫雷 简介 扫雷 是一种经典的单人电脑游戏&#xff0c;最初由微软公司在 1990 年代开发并内置在 Windows 操作系统中。游戏的目标是在一个由方块组成的网格上揭开所有非地雷的方块&#xff0c;而不触发地雷。每个方块上都标有数字&#xff0c;表示周围 8 个方块中…

通过AI助手实现一个nas定时任务更新阿里云域名解析

一.通过AI助手实现一个ip-domain.py的脚本 起一个Python脚本&#xff0c;ip-domain.py&#xff1b;注意已安装Python3.的运行环境&#xff1b;将下面阿里云相关配置添加&#xff0c;注意这里引用了两个包&#xff0c;requests和alibabacloud_alidns20150109&#xff1b;执行前…

Linux安装Maven

目录 传送门前言一、下载二、安装三、配置 传送门 SpringMVC的源码解析&#xff08;精品&#xff09; Spring6的源码解析&#xff08;精品&#xff09; SpringBoot3框架&#xff08;精品&#xff09; MyBatis框架&#xff08;精品&#xff09; MyBatis-Plus SpringDataJPA Spr…

Java23种设计模式-行为型模式之责任链模式

责任链模式&#xff08;Chain of Responsibility Pattern&#xff09;:允许将多个对象连接成一条链&#xff0c;并沿着这条链传递一个请求&#xff0c;直到链上的某个对象能够处理该请求为止。这种模式的目的是解耦请求的发送者和接收者&#xff0c;同时允许多个对象有机会处理…

大数据面试题 —— Spark数据倾斜及其解决方案

目录 1 调优概述2 数据倾斜发生时的现象3 数据倾斜发生的原理4 如何定位导致数据倾斜的代码4.1 某个 task 执行特别慢的情况4.2 某个 task 莫名其妙内存溢出的情况5 查看导致数据倾斜的 key 的数据分布情况6 数据倾斜的解决方案6.1 使用 Hive ETL 预处理数据6.2 过滤少数导致倾…

灵魂面甲Soulmask免费加速器 灵魂面甲加速器

加入《灵魂面甲》&#xff0c;你将披上炫目的面甲盔甲&#xff0c;手执利刃神兵&#xff0c;运用你的独门技艺去征服这个世界。每一件面甲不仅是防护的象征&#xff0c;更赋予你不同的能力加成&#xff0c;炫酷非凡。无论你是热爱探索新世界的冒险家&#xff0c;是沉浸于作物栽…

SQL数据更新

插入 插入单独的元组 INSERT INTO 表名 &#xff08;列名&#xff0c;列名&#xff0c;列名&#xff09; VALUES(值&#xff0c;值&#xff0c;值) 记忆&#xff1a;宾语倒装 若没有指定列名&#xff0c;则默认插入的数值是完整的&#xff0c;与列的个数一致 eg:将一…

三丰云免费云服务器

背景 在2020年的双十一买过华为云的打折服务器&#xff0c;属于最低配的个人使用的服务器&#xff0c;当时价格为80~90元/年&#xff0c;由本人亲测&#xff0c;使用体验很差&#xff0c;最差的时候连后台都不一定能登上去&#xff0c;猜测应该是没有提供标明的服务器性能&…