Webpack 4与Babel 7升级Vue项目的完整配置指南
本文详细记录了将Vue项目从Webpack 3和Babel 6升级至Webpack 4与Babel 7过程中遇到的核心问题及解决方案。主要涵盖CSS提取插件替换为mini-css-extract-plugin、Babel包名规范变更(@babel/*)、.babelrc配置调整以及vue-loader 15.x必须引入VueLoaderPlugin等关键步骤,帮助开发者顺利解决升级报错。
近期尝试启动个人新项目,旨在记录近期工程中实现的一个小交互功能。按照以往流畅的配置流程进行操作时,打包过程遭遇诸多阻碍。通过命令行报错逐一排查,发现Babel已大幅升级至7.x版本,意识到长期专注于业务开发导致对工具链更新关注不足。此处整理记录升级过程中遇到的具体问题及对应的解决策略。
Webpack 4.x 中 extract-text-webpack-plugin 插件的替代方案
一、Webpack 4.x 中 extract-text-webpack-plugin 插件的替代方案
(node:2628) DeprecationWarning: Tapable.plugin is deprecated. Use new API on `.hooks` instead
i 「wds」: Project is running at http://localhost:8300/
i 「wds」: webpack output is served from /
i 「wds」: Content not from webpack is served from F:\private\plugin-insert\dist
F:\private\plugin-insert\node_modules\webpack\lib\Chunk.js:838
throw new Error(
^
Error: Chunk.entrypoints: Use Chunks.groupsIterable and filter by instanceof Entrypoint instead
at Chunk.get (F:\private\plugin-insert\node_modules\webpack\lib\Chunk.js:838:9)
at F:\private\plugin-insert\node_modules\extract-text-webpack-plugin\dist\index.js:176:48
at Array.forEach (<anonymous>)
at F:\private\plugin-insert\node_modules\extract-text-webpack-plugin\dist\index.js:171:18
at AsyncSeriesHook.eval [as callAsync] (eval at create (F:\private\plugin-insert\node_modules\tapable\lib\HookCodeFactory.js:32:10), <anonymous>:7:1)
at AsyncSeriesHook.lazyCompileHook (F:\private\plugin-insert\node_modules\tapable\lib\Hook.js:154:20)
at Compilation.seal (F:\private\plugin-insert\node_modules\webpack\lib\Compilation.js:1231:27)
at hooks.make.callAsync.err (F:\private\plugin-insert\node_modules\webpack\lib\Compiler.js:541:17)
at _done (eval at create (F:\private\plugin-insert\node_modules\tapable\lib\HookCodeFactory.js:32:10), <anonymous>:9:1)
at _err1 (eval at create (F:\private\plugin-insert\node_modules\tapable\lib\HookCodeFactory.js:32:10), <anonymous>:32:22)
在使用 extract-text-webpack-plugin 进行CSS单独打包时出现报错。官方文档仅支持至Webpack 3,目前该插件暂无适配Webpack 4的稳定版本。解决此问题可选用 extract-text-webpack-plugin@next 版本,或者采用 mini-css-extract-plugin 插件。
mini-css-extract-plugin 的具体配置用法如下所示:
const MiniCssExtractPlugin = require("mini-css-extract-plugin") ;
const config = module.exports = {
plugins: [
new MiniCssExtractPlugin({
filename: "[name].[chunkhash:8].css",
chunkFilename: "[id].css"
})
],
module: {
rules: [
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
"css-loader"
]
}
]
}
}
module.exports = config
Babel 从 6.x 升级至 7.x 的配置变更
二、Babel 从 6.x 升级至 7.x 的配置变更
(1)处理 @babel/core 依赖
Module build failed (from ./node_modules/babel-loader/lib/index.js):
Error: Cannot find module '@babel/core'
babel-loader@8 requires Babel 7.x (the package '@babel/core').
If you'd like to use Babel 6.x ('babel-core'), you should install 'babel-loader@7'.
若项目中未找到 @babel/core,需先卸载原有的 babel-core,随后安装 @babel/core 以完成核心依赖的替换。
npm un babel-core
npm i -D @babel/core
(2)处理 @babel/preset-* 预设包
Module build failed (from ./node_modules/babel-loader/lib/index.js):
Error: Plugin/Preset files are not allowed to export objects, only functions.
卸载原有的 babel-preset-* 系列包,重新安装对应的 @babel/preset-* 包,并同步更新 .babelrc 文件中的 presets 配置项。
npm:
- babel-preset-env
+ @babel/perset-env
.babelrc:
- "presets": ["env"]
+ "presets": ["@babel/preset-env"]
需要注意的是,stage-* 预设包已被官方弃用,不再推荐使用。
(3)处理 @babel/plugin-* 插件包
Module build failed (from ./node_modules/babel-loader/lib/index.js):
TypeError: this.setDynamic is not a function
at PluginPass.pre
...
针对插件部分,同样需要将原有的 babel-plugin-* 卸载,并重新安装 @babel/plugin-* 系列插件。
安装完成后,需相应修改 .babelrc 文件中的 plugins 配置。
具体的插件包名称可在 npm 仓库中查询获取。
(4)最终配置文件示例
.babelrc
{
"presets": ["@babel/preset-env"],
"plugins": [
"@babel/plugin-transform-runtime"
]
}
package.json
"devDependencies": {
"@babel/core": "^7.1.2",
"@babel/plugin-transform-runtime": "^7.1.0",
"@babel/preset-env": "^7.1.0",
"babel-loader": "^8.0.4",
...
},
"dependencies": {
"@babel/runtime": "^7.1.2",
"vue": "^2.5.17",
"vue-loader": "^15.4.2",
"vue-router": "^3.0.1",
"vuex": "^3.0.1",
"webpack": "^4.22.0",
"webpack-cli": "^3.1.2",
"webpack-dev-server": "^3.1.10",
"webpack-merge": "^4.1.4"
...
}
(5)升级总结
Babel 7 废弃了以往 babel-*-* 的命名规范,统一调整为 @babel/*-* 的作用域命名方式。
完成依赖包的更新及 .babelrc 文件的修改后,项目即可正常启动运行。
vue-loader 15.x 中 VueLoaderPlugin 的引入
三、vue-loader 15.x 中 VueLoaderPlugin 的引入
vue-loader was used without the corresponding plugin.
Make sure to include VueLoaderPlugin in your webpack config.
//两个方式都可以的,随便用一个
const VueLoaderPlugin = require('vue-loader/lib/plugin');
// 或者
const { VueLoaderPlugin } = require('vue-loader');
plugins: [
// make sure to include the plugin for the magic
new VueLoaderPlugin()
]
关于 vue-loader 15.x 的详细配置问题,可参考 GitHub 相关 Issue:https://github.com/vuejs/vue-loader/issues/1238
以上内容为本文的全部分享,希望对各位的学习有所帮助,也欢迎大家继续支持本站。
Mock.js 与 MSW、YApi 对比:前端接口模拟工具怎么选
2026-11-04 22:55:05
Turf.js 安装与引入:NPM 打包及 CDN 快速上手指南
2026-11-03 23:23:32
JS代码混淆后无法调试?Source Map配置与浏览器断点技巧
2026-11-03 14:12:17
前端JS发送HTTP请求遇到跨域(CORS)报错怎么解决
2026-11-02 10:30:07
Three.js怎么安装:npm引入与CDN加载方法
2026-11-02 09:23:52








