babel7.x和webpack4.x配置vue項(xiàng)目的方法步驟
很偶然的今天想開個(gè)自己的小項(xiàng)目,記錄一下最近項(xiàng)目工程上實(shí)現(xiàn)的一個(gè)小交互。按照之前運(yùn)行非常流暢的配置走一遍,打包遇到各種坑。只好根據(jù)命令行的報(bào)錯(cuò)逐個(gè)排查,發(fā)現(xiàn)babel升級(jí)了一個(gè)大版本,已經(jīng)到7.x了??磥砻咳粘撩皂?xiàng)目,已經(jīng)跟不上節(jié)奏了。這里記錄一下遇到的問題以及解決方案。
1.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 提取單獨(dú)打包c(diǎn)ss文件時(shí)報(bào)錯(cuò),官方安裝部分的文檔只寫到了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 升級(jí) 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"]
(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文件
具體的包名可以在 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) 總結(jié)
babel 舍棄了以前的 babel-*-* 的命名方式,改成了 @babel/*-*
修改依賴和 .babelrc 文件后就能正常啟動(dòng)項(xiàng)目了。
3.vue-loader 15.x vueLoaderPlugin
vue-loader was used without the corresponding plugin. Make sure to include VueLoaderPlugin in your webpack config.
//兩個(gè)方式都可以的,隨便用一個(gè) const VueLoaderPlugin = require('vue-loader/lib/plugin'); // 或者 const { VueLoaderPlugin } = require('vue-loader'); plugins: [ // make sure to include the plugin for the magic new VueLoaderPlugin() ]
詳細(xì) https://github.com/vuejs/vue-loader/issues/1238
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
babel7.x和webpack4.x配置vue項(xiàng)目的方法步驟
這篇文章主要介紹了babel7.x和webpack4.x配置vue項(xiàng)目的方法步驟,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-05-05element-ui封裝一個(gè)Table模板組件的示例
這篇文章主要介紹了element-ui封裝一個(gè)Table模板組件的示例,幫助大家更好的理解和學(xué)習(xí)vue框架的使用,感興趣的朋友可以了解下2021-01-01VUEX 數(shù)據(jù)持久化,刷新后重新獲取的例子
今天小編就為大家分享一篇VUEX 數(shù)據(jù)持久化,刷新后重新獲取的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11vue+animation動(dòng)畫實(shí)現(xiàn)跑馬燈效果
這篇文章主要為大家詳細(xì)介紹了vue+animation動(dòng)畫實(shí)現(xiàn)跑馬燈效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04vue3+ant?design的form數(shù)組表單校驗(yàn)方法
這篇文章主要介紹了vue3+ant?design的form數(shù)組表單,如何校驗(yàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-09-09詳解解決Vue相同路由參數(shù)不同不會(huì)刷新的問題
這篇文章主要介紹了詳解解決Vue相同路由參數(shù)不同不會(huì)刷新的問題,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-10-10element表單el-form的label自適應(yīng)寬度的實(shí)現(xiàn)
本文主要介紹了element表單el-form的label自適應(yīng)寬度的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08