博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
babel 7.x 和 webpack 4.x 配置vue项目
阅读量:4509 次
发布时间:2019-06-08

本文共 4219 字,大约阅读时间需要 14 分钟。

很偶然的今天想开个自己的小项目,记录一下最近项目工程上实现的一个小交互。按照之前运行非常流畅的配置走一遍,打包遇到各种坑。只好根据命令行的报错逐个排查,发现babel升级了一个大版本,已经到7.x了。看来每日沉迷项目,已经跟不上节奏了。这里记录一下遇到的问题以及解决方案。

 

1.webpack 4.x 插件 extract-text-webpack-plugin

(node:2628) DeprecationWarning: Tapable.plugin is deprecated. Use new API on `.hooks` insteadi 「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 (
) 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),
: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),
:9:1) at _err1 (eval at create (F:\private\plugin-insert\node_modules\tapable\lib\HookCodeFactory.js:32:10),
: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

2.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-corenpm 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"]

另,

(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文件
具体的包名可以在 里找

(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 舍弃了以前的 babel-*-* 的命名方式,改成了 @babel/*-*

修改依赖和 .babelrc 文件后就能正常启动项目了。


3.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() ]

参考资料

 

转载于:https://www.cnblogs.com/bill89/p/10489174.html

你可能感兴趣的文章
Mac MySQL启动不了解决办法(MySQL卸载重新安装教程)
查看>>
连通块
查看>>
servlet.txt笔记
查看>>
jquery设置select选中
查看>>
今天说一下DML触发器的顺序
查看>>
Memcached学习(一)--网络模型
查看>>
FragmentTransaction add 和 replace 区别 转
查看>>
jQuery 效果方法
查看>>
STM32物联网通信WIFI
查看>>
java反射案例详解
查看>>
MAGENTO 与 reindexer
查看>>
数字,字符串,列表及其内置方法
查看>>
iOS遍历数组的同时删除元素
查看>>
小强的HTML5移动开发之路(16)——神奇的拖放功能
查看>>
zookeeper FastLeaderElection
查看>>
Jquery AJAX如何使用Promise/Deferred实现顺序执行?
查看>>
进度条
查看>>
maven 常用命令
查看>>
用户画像
查看>>
HTTP报文(面试会问开发时常用的报文头格式)
查看>>