最近一直在研究webpack,忽然想看看vue-cli中的webpack是如何配置,查閱了不少相關的文章,因此也想出幾篇關於vue-cli配置的東西。正所謂「工欲善其事必先利其器」嘛!這一篇主要是分析vue中關於config文件夾中的相關代碼;css
首先咱們先看一下config的文件結構:html
|-config |---dev.env.js |---index.js |---prod.env.js
打開咱們的vue項目文件夾咱們能夠清楚的看到文件夾下的三個文件,「dev.env.js」,「index.js」,「prod.env.js」,咱們先打開prod.env.js的文件,看裏面的內容:vue
'use strict' module.exports = { NODE_ENV: '"production"' }
prod.env.js的內容很是簡單,僅僅是導出了一個對象,裏面寫明瞭執行環境是「production(生產環境)」;咱們接下來看與之對應的「dev.env.js」文件:node
'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();webpack
vue-cli中將一些通用的配置抽出來放在一個文件內,在對不一樣的環境配置不一樣的代碼,最後使用webpack-merge來進行合併,減小重複代碼,正如文檔中所說,「webpack遵循不重複原則(Don't repeat yourself - DRY),不會再不一樣的環境中配置相同的代碼」git
固然,關於webpack-merge的內容還遠不止這些,想了解更多關於這個模塊的朋友請訪問 https://www.npmjs.com/package/webpack-mergees6
好,讓咱們接着回到代碼中來,引入webpack-merge後這個文件又引入了prod.env.js,接着就將prod.env.js的配置和新的配置,即指明開發環境(development)進行了merge。(我有點兒不太理解爲何要這樣作,若是不merge直接寫module.exports={NODE_ENV:'"development'}也是能夠的,難道是爲了優雅降級?)github
還有一點須要注意是的,development和production必定要加雙引號,否則會報錯!!!web
最後,咱們來看index.js:vue-cli
'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下的配置內容:
assetsSubDirectory指的是靜態資源文件夾,默認「static」, assetsPublicPath指的是發佈路徑, proxyTable是咱們經常使用來配置代理API的地方,後面的host和port相信你們都知道,我就不細說了, autoOpenBrowser是否自動打開瀏覽器 errorOverlay查詢錯誤 notifyOnErrors通知錯誤 , poll是跟devserver相關的一個配置,webpack爲咱們提供的devserver是能夠監控文件改動的,但在有些狀況下卻不能工做,咱們能夠設置一個輪詢(poll)來解決 useEslint是否使用eslint showEslintErrorsInOverlay是否展現eslint的錯誤提示 devtool webpack提供的用來方便調試的配置,它有四種模式,能夠查看webpack文檔瞭解更多 cacheBusting 一個配合devtool的配置,當給文件名插入新的hash致使清楚緩存時是否生成souce maps,默認在開發環境下爲true cssSourceMap 是否開啓cssSourceMap
咱們再來看build下的配置內容:
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文件夾下的內容基本上就過完了,正如名字告訴咱們的,這三個文件僅僅是寫死的配置文件,截止目前還沒遇到太多關於webpack的東西,不過隨着咱們接下來對build文件夾得內容進行分析後,咱們就會對vue的配置有了更深的瞭解,也更能體會到vue配置的厲害之處!