詳解項目升級到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')) }
二. 此時勉強能跑起來,但后續(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
里默認關(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ù)端進行數(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ù)端進行數(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)文章
element?tab標(biāo)簽管理路由頁面的項目實踐
本文主要介紹了element?tab標(biāo)簽管理路由頁面的項目實踐,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08使用兩種方式調(diào)用本地json文件(基于Vue-cli3腳手架)
這篇文章主要介紹了使用兩種方式調(diào)用本地json文件(基于Vue-cli3腳手架),具有很好的參考價值,希望對大家有所幫助,2023-10-10vue.js根據(jù)代碼運行環(huán)境選擇baseurl的方法
本篇文章主要介紹了vue.js根據(jù)代碼運行環(huán)境選擇baseurl的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02Vue移動端實現(xiàn)pdf/excel/圖片在線預(yù)覽
這篇文章主要為大家詳細介紹了Vue移動端實現(xiàn)pdf/excel/圖片在線預(yù)覽功能的相關(guān)方法,文中的示例代碼講解詳細,有需要的小伙伴可以參考下2024-04-04