webpack4 升级

本文记录了从webpack3到webpack4升级过程中遇到的问题和解决方案,包括webpack-dev-server、html-webpack-plugin、vue-loader的升级,以及webpack配置中VueLoaderPlugin的引入、mode的设置、CommonsChunkPlugin替换为splitChunks、extract-text-webpack-plugin替换为mini-css-extract-plugin等。最终成功完成升级。
摘要由CSDN通过智能技术生成

webpack4升级之坑

webpack4出来已经好久,看到之前的vue-cli项目还是基于"webpack": “^3.6.0”,所以参考了一些资料,对webpack3做了升级。

webpack版本升级

"webpack": "^4.29.6"

cnpm i 或者 npm i ,依赖安装,npm run dev 看下效果,出现一堆的错误。不难发现是webpack-dev-server 导致:

webpack-dev-server --inline --progress --config build/webpack.dev.conf.js

module.js:491
    throw err;
    ^

Error: Cannot find module 'webpack/bin/config-yargs'
    at Function.Module._resolveFilename (module.js:489:15)
    at Function.Module._load (module.js:439:25)
    at Module.require (module.js:517:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/Users/xumingxing/Desktop/h5-vue/node_modules/webpack-dev-server/bin/webpack-dev-server.js:54:1)
    at Module._compile (module.js:573:30)
    at Object.Module._extensions..js (module.js:584:10)
    at Module.load (module.js:507:32)
    at tryModuleLoad (module.js:470:12)
    at Function.Module._load (module.js:462:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! h5-vue@1.0.0 dev: `webpack-dev-server --inline --progress --config build/webpack.dev.conf.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the h5-vue@1.0.0 dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:

紧接着,我们就应该继续升级webpack-dev-server ,我将其升级为最新版本,npm run dev 也出错了。主要是compilation.mainTemplate.applyPluginsWaterfall报错,查了资料是 html-webpack-plugin 版本不兼容导致。同样也可将其升级为最新版:

"webpack-dev-server": "^3.2.1",

//报错主要信息
...es/_punycode@1.4.1@punycode/punycode.js/Users/xumingxing/Desktop/h5-vue/node_modules/html-webpack-plugin/lib/compiler.js:81
        var outputName = compilation.mainTemplate.applyPluginsWaterfall('asset-path', outputOptions.filename, {
   
                                                  ^

TypeError: compilation.mainTemplate.applyPluginsWaterfall is not a function
    at /Users/xumingxing/Desktop/h5-vue/node_modules/html-webpack-plugin/lib/compiler.js:81:51
    at compile (/Users/xumingxing/Desktop/h5-vue/node_modules/_webpack@4.29.6@webpack/lib/Compiler.js:306:11)
    at hooks.afterCompile.callAsync.err 

升级html-webpack-plugin

"html-webpack-plugin": "^3.2.0",

升级完成后,我们再继续npm run dev 看下效果:

Module build failed (from ./node_modules/vue-loader/index.js):
TypeError: Cannot read property 'vue' of undefined
    at Object.module.exports (/Users/xumingxing/Desktop/h5-vue/node_modules/vue-loader/lib/loader.js:61:17)

 @ ./src/main.js 4:0-24 13:16-19
 @ multi ./node_modules/_webpack-dev-server@3.2.1@webpack-dev-server/client?http://localhost:8081 (webpack)/hot/dev-server.js ./src/main.js

页面依旧报错,查询资料后是继续要升级vue-loader,这里将其升级为"15.7.0":

"vue-loader": "^15.7.0"

升级完成后,npm run dev ,又是一大堆错误:

 webpack-
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值