詳解項目升級到vue-cli3的正確姿勢
一. 原以為升級vue-cli3的路線是這樣的:
創(chuàng)建vue-cli3項目,按原有項目的配置選好各項配置

遷移目錄
src->src static->public
對比新舊 package.json ,然后 yarn install ,完畢。

然鵝... 運行項目,報錯 You are using the runtime-only build of Vue...... :

然后去查了下舊項目的相關(guān)字眼文件:

噢,原來是vue-cli3的webpack相關(guān)文件都得自己寫。于是乎根據(jù)官網(wǎng)的指引,在根目錄創(chuàng)建了 vue.config.js
此時粗略配置:
chainWebpack: config => {
config.module
.rule('vue')
.use('vue-loader')
.loader('vue-loader')
.tap(options => {
options.compilerOptions.preserveWhitespace = false
return options
})
config.resolve.alias
.set('vue$', 'vue/dist/vue.esm.js')
.set('@', resolve('src'))
}
二. 此時勉強(qiáng)能跑起來,但后續(xù)遇到了這些坑:
#1 public 靜態(tài)資源不加載
```
const CopyWebpackPlugin = require('copy-webpack-plugin')
// ....
// 確保靜態(tài)資源
config.resolve.extensions = ['.js', '.vue', '.json', '.css']
config.plugins.push(
new CopyWebpackPlugin([{ from: 'public/', to: 'public' }]),
)
```
#2 Chrome 查看樣式時無法找到源文件

原因: vue-cli3 里默認(rèn)關(guān)閉 sourceMap,樣式都會被打包到首頁。 解決: 需要自己配置打開
// 讓樣式找到源
css: {
sourceMap: true
},
#3 生產(chǎn)環(huán)境的 debuger 和 console 無法通過 uglifyjs-webpack-plugin 和 uglify-es 剔除
原因:不支持 es6 , 需要配置 babel ( uglify-es 按配置填會顯示不存在選項)
解決:插件terser
```
const TerserPlugin = require('terser-webpack-plugin')
if (process.env.NODE_ENV === 'production') {
// 為生產(chǎn)環(huán)境修改配置...
new TerserPlugin({
cache: true,
parallel: true,
sourceMap: true, // Must be set to true if using source-maps in production
terserOptions: {
compress: {
drop_console: true,
drop_debugger: true
}
}
})
} else {
// 為開發(fā)環(huán)境修改配置...
}
```
#4 無法在 config 目錄下配置不同環(huán)境的 API_URL ,用于跨域請求
原因: vue-cli3 中需要遵循變量規(guī)則,使用 VUE_APP 前綴
官方規(guī)則: 在客戶端側(cè)代碼中使用環(huán)境變量
解決:于是你需要創(chuàng)建如下幾個文件:

.local 也可以加在指定模式的環(huán)境文件上,比如 .env.development.local 將會在 development 模式下被載入,且被 git 忽略。
文件內(nèi)容:
// env.development.local NODE_ENV = development VUE_APP_URL = http://xxx.x.xxx/
#5 vue-cli代理轉(zhuǎn)發(fā)控制臺反復(fù)打印 "WebSocket connection to'ws://localhost..."

解決方法:
vue.config.js 中配置 devServer.proxy 的 ws 為 false
結(jié)合上述兩步,相對應(yīng)的 vue.config.js ,需要這么寫:
const env = process.env.NODE_ENV
let target = process.env.VUE_APP_URL
const devProxy = ['/api', '/'] // 代理
// 生成代理配置對象
let proxyObj = {};
devProxy.forEach((value, index) => {
proxyObj[value] = {
ws: false,
target: target,
// 開啟代理:在本地會創(chuàng)建一個虛擬服務(wù)端,然后發(fā)送請求的數(shù)據(jù),并同時接收請求的數(shù)據(jù),這樣服務(wù)端和服務(wù)端進(jìn)行數(shù)據(jù)的交互就不會有跨域問題
changeOrigin: true,
pathRewrite: {
[`^${value}`]: value
}
};
})
// ....
devServer: {
open: true,
host: 'localhost',
port: 8080,
proxy: proxyObj
}
最后貼上我的 vue.config.js :
const CopyWebpackPlugin = require('copy-webpack-plugin')
const TerserPlugin = require('terser-webpack-plugin')
const path = require('path')
const env = process.env.NODE_ENV
let target = process.env.VUE_APP_URL
const devProxy = ['/api', '/'] // 代理
// 生成代理配置對象
let proxyObj = {};
devProxy.forEach((value, index) => {
proxyObj[value] = {
ws: false,
target: target,
// 開啟代理:在本地會創(chuàng)建一個虛擬服務(wù)端,然后發(fā)送請求的數(shù)據(jù),并同時接收請求的數(shù)據(jù),這樣服務(wù)端和服務(wù)端進(jìn)行數(shù)據(jù)的交互就不會有跨域問題
changeOrigin: true,
pathRewrite: {
[`^${value}`]: value
}
};
})
function resolve (dir) {
return path.join(__dirname, dir)
}
module.exports = {
publicPath: '/',
// 讓樣式找到源
css: {
sourceMap: true
},
configureWebpack: config => {
// 確保靜態(tài)資源
config.resolve.extensions = ['.js', '.vue', '.json', '.css']
config.plugins.push(
new CopyWebpackPlugin([{ from: 'public/', to: 'public' }]),
)
if (process.env.NODE_ENV === 'production') {
// 為生產(chǎn)環(huán)境修改配置...
new TerserPlugin({
cache: true,
parallel: true,
sourceMap: true, // Must be set to true if using source-maps in production
terserOptions: {
compress: {
drop_console: true,
drop_debugger: true
}
}
})
} else {
// 為開發(fā)環(huán)境修改配置...
}
},
chainWebpack: config => {
config.module
.rule('vue')
.use('vue-loader')
.loader('vue-loader')
.tap(options => {
options.compilerOptions.preserveWhitespace = false
return options
})
config.resolve.alias
.set('vue$', 'vue/dist/vue.esm.js')
.set('@', resolve('src'))
},
devServer: {
open: true,
host: 'localhost',
port: 8080,
proxy: proxyObj
}
}
三. Eslint相關(guān)報錯及配置

module.exports = {
root: true,
env: {
node: true
},
'extends': [
'plugin:vue/essential',
'@vue/standard'
],
rules: {
'generator-star-spacing': 'off',
'object-curly-spacing': 'off',
// 最常出現(xiàn)的錯誤
'no-unused-vars': 'off',
// 最常出現(xiàn)的錯誤
"vue/no-use-v-if-with-v-for": ["error", {
"allowUsingIterationVar": true
}],
'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off',
'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off'
},
parserOptions: {
parser: 'babel-eslint'
}
}
最后的最后,跑個項目
yarn serve

yarn build


以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue3中使用Element?Plus時el-icon無法顯示的問題解決
我們的Vue前端一般都是用的ElementUI,其中按鈕可能用到的比較多,官方里面有自帶的一些默認(rèn)圖標(biāo),下面這篇文章主要給大家介紹了關(guān)于Vue3中使用Element?Plus時el-icon無法顯示的問題解決,需要的朋友可以參考下2022-03-03
Vue如何整合mavon-editor編輯器(markdown編輯和預(yù)覽)
這篇文章主要介紹了Vue整合mavon-editor編輯器(markdown編輯和預(yù)覽)的相關(guān)知識,mavon-editor是目前比較主流的markdown編輯器,重點介紹它的使用方法,需要的朋友可以參考下2022-10-10
vue elementui el-form rules動態(tài)驗證的實例代碼詳解
在使用elementUI el-form 中,對于業(yè)務(wù)不同的時候可能會產(chǎn)生不同表單結(jié)構(gòu),但是都是存在同一個表單控件el-form中。這篇文章主要介紹了vue elementui el-form rules動態(tài)驗證的實例代碼,需要的朋友可以參考下2019-05-05
vant 解決tab切換插件標(biāo)題樣式自定義的問題
這篇文章主要介紹了vant 解決tab切換插件標(biāo)題樣式自定義的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11

