vue-cli配置文件——config篇
最近一直在研究webpack,突然想看看vue-cli中的webpack是如何配置,查閱了很多相關的文章,所以也想出幾篇關于vue-cli配置的東西。正所謂“工欲善其事必先利其器”嘛!這一篇主要是分析vue中關于 config文件夾中的相關代碼 ;
首先我們先看一下config的文件結(jié)構(gòu):
|-config |---dev.env.js |---index.js |---prod.env.js
打開我們的vue項目文件夾我們可以清楚的看到文件夾下的三個文件, “dev.env.js” , “index.js” , “prod.env.js” ,我們先打開prod.env.js的文件,看里面的內(nèi)容:
'use strict'
module.exports = {
NODE_ENV: '"production"'
}
prod.env.js的內(nèi)容非常簡單,僅僅是導出了一個對象,里面寫明了執(zhí)行環(huán)境是“production(生產(chǎn)環(huán)境)”;我們接下來看與之對應的“dev.env.js”文件:
'use strict'
//引入webpack-merge模塊
const merge = require('webpack-merge')
//引入剛才打開的prod.env.js
const prodEnv = require('./prod.env')
module.exports = merge(prodEnv, {
NODE_ENV: '"development"'
})
在“dev.env.js”中,先引入了webpack-merge這個模塊。這個模塊的作用是來合并兩個配置文件對象并生成一個新的配置文件,有點兒類似于es6的object.assign();
vue-cli中將一些通用的配置抽出來放在一個文件內(nèi),在對不同的環(huán)境配置不同的代碼,最后使用webpack-merge來進行合并,減少重復代碼,正如文檔中所說,“ webpack遵循不重復原則(Don't repeat yourself - DRY),不會再不同的環(huán)境中配置相同的代碼 ”
當然,關于webpack-merge的內(nèi)容還遠不止這些,想了解更多關于這個模塊的朋友請訪問 https://www.npmjs.com/package/webpack-merge
好,讓我們接著回到代碼中來,引入webpack-merge后這個文件又引入了prod.env.js,接著就將prod.env.js的配置和新的配置,即指明開發(fā)環(huán)境(development)進行了merge。(我有點兒不太理解為什么要這樣做,如果不merge直接寫module.exports={NODE_ENV:'"development'}也是可以的,難道是為了優(yōu)雅降級?)
還有一點需要注意是的, development和production一定要加雙引號,不然會報錯?。?!
最后,我們來看index.js:
'use strict'
// Template version: 1.2.4
// see http://vuejs-templates.github.io/webpack for documentation.
const path = require('path')
module.exports = {
dev: {
// Paths
assetsSubDirectory: 'static',
assetsPublicPath: '/',
proxyTable: {},
// Various Dev Server settings
host: 'localhost', // can be overwritten by process.env.HOST
port: 8080, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined
autoOpenBrowser: false,
errorOverlay: true,
notifyOnErrors: true,
poll: false, // https://webpack.js.org/configuration/dev-server/#devserver-watchoptions-
// Use Eslint Loader?
// If true, your code will be linted during bundling and
// linting errors and warnings will be shown in the console.
useEslint: true,
// If true, eslint errors and warnings will also be shown in the error overlay
// in the browser.
showEslintErrorsInOverlay: false,
/**
* Source Maps
*/
// https://webpack.js.org/configuration/devtool/#development
devtool: 'eval-source-map',
// If you have problems debugging vue-files in devtools,
// set this to false - it *may* help
// https://vue-loader.vuejs.org/en/options.html#cachebusting
cacheBusting: true,
// CSS Sourcemaps off by default because relative paths are "buggy"
// with this option, according to the CSS-Loader README
// (https://github.com/webpack/css-loader#sourcemaps)
// In our experience, they generally work as expected,
// just be aware of this issue when enabling this option.
cssSourceMap: false,
},
build: {
// Template for index.html
index: path.resolve(__dirname, '../dist/index.html'),
// Paths
assetsRoot: path.resolve(__dirname, '../dist'),
assetsSubDirectory: 'static',
assetsPublicPath: '/',
/**
* Source Maps
*/
productionSourceMap: true,
// https://webpack.js.org/configuration/devtool/#production
devtool: '#source-map',
// Gzip off by default as many popular static hosts such as
// Surge or Netlify already gzip all static assets for you.
// Before setting to `true`, make sure to:
// npm install --save-dev compression-webpack-plugin
productionGzip: false,
productionGzipExtensions: ['js', 'css'],
// Run the build command with an extra argument to
// View the bundle analyzer report after build finishes:
// `npm run build --report`
// Set to `true` or `false` to always turn it on or off
bundleAnalyzerReport: process.env.npm_config_report
}
}
開頭引入了node中的path模塊,
然后我們先來看dev下的配置內(nèi)容:
assetsSubDirectory指的是靜態(tài)資源文件夾,默認“static”,
assetsPublicPath指的是發(fā)布路徑,
proxyTable是我們常用來配置代理API的地方,后面的host和port相信大家都知道,我就不細說了,
autoOpenBrowser是否自動打開瀏覽器
errorOverlay查詢錯誤
notifyOnErrors通知錯誤
,poll是跟devserver相關的一個配置,webpack為我們提供的devserver是可以監(jiān)控文件改動的,但在有些情況下卻不能工作,我們可以設置一個輪詢(poll)來解決
useEslint是否使用eslint
showEslintErrorsInOverlay是否展示eslint的錯誤提示
devtool webpack提供的用來方便調(diào)試的配置,它有四種模式,可以查看webpack文檔了解更多
cacheBusting 一個配合devtool的配置,當給文件名插入新的hash導致清楚緩存時是否生成souce maps,默認在開發(fā)環(huán)境下為true,不過文檔中還寫了一句話:“Turning this off can help with source map debugging.”額。。。
cssSourceMap 是否開啟cssSourceMap
我們再來看build下的配置內(nèi)容:
index 編譯后index.html的路徑,path.resolve(__dirname, '../dist')中
path.resolve(__dirname)指的是index.js所在的絕對路徑,再去找“../dist”這個路徑(node相關的知識),
assetsRoot打包后的文件根路徑,至于assetsSubDirectory和assetsPublicPath跟dev中的一樣,
productionSourceMap是否開啟source-map,
devtool同dev,
productionGzip是否壓縮,
productionGzipExtensions gzip模式下需要壓縮的文件的擴展名,設置后會對相應擴展名的文件進行壓縮
bundleAnalyzerReport 是否開啟打包后的分析報告
截止到這兒,config文件夾下的內(nèi)容基本上就過完了,正如名字告訴我們的,這三個文件僅僅是寫死的配置文件,截止目前還沒遇到太多
總結(jié)
以上所述是小編給大家介紹的vue-cli配置文件——config篇,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關文章
vue本地打開build后生成的dist文件夾index.html問題
這篇文章主要介紹了vue本地打開build后生成的dist文件夾index.html問題,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友參考下吧2019-09-09
詳解Vue+axios+Node+express實現(xiàn)文件上傳(用戶頭像上傳)
這篇文章主要介紹了詳解Vue+axios+Node+express實現(xiàn)文件上傳(用戶頭像上傳),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08
vue啟動報錯‘vue-cli-service serve‘問題及解決
這篇文章主要介紹了vue啟動報錯‘vue-cli-service serve‘問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10
vue路由跳轉(zhuǎn)攜帶參數(shù)的方式總結(jié)
我們知道在vue中每個頁面都需要在路由中聲明,下面這篇文章主要給大家介紹了關于vue路由跳轉(zhuǎn)攜帶參數(shù)的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-10-10

