手撕vue-cli配置文件——config篇


最近一直在研究webpack,突然想看看vue-cli中的webpack是如何配置,查閱了很多相關的文章,所以也想出幾篇關於vue-cli配置的東西。正所謂“工欲善其事必先利其器”嘛!這一篇主要是分析vue中關於config文件夾中的相關代碼

首先我們先看一下config的文件結構:

|-config
|---dev.env.js
|---index.js
|---prod.env.js

打開我們的vue項目文件夾我們可以清楚的看到文件夾下的三個文件,“dev.env.js”“index.js”“prod.env.js”,我們先打開prod.env.js的文件,看里面的內容:

'use strict'
module.exports = {
  NODE_ENV: '"production"'
}

prod.env.js的內容非常簡單,僅僅是導出了一個對象,里面寫明了執行環境是“production(生產環境)”;我們接下來看與之對應的“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中將一些通用的配置抽出來放在一個文件內,在對不同的環境配置不同的代碼,最后使用webpack-merge來進行合並,減少重復代碼,正如文檔中所說,“webpack遵循不重復原則(Don't repeat yourself - DRY),不會再不同的環境中配置相同的代碼

當然,關於webpack-merge的內容還遠不止這些,想了解更多關於這個模塊的朋友請訪問 https://www.npmjs.com/package/webpack-merge

好,讓我們接着回到代碼中來,引入webpack-merge后這個文件又引入了prod.env.js,接着就將prod.env.js的配置和新的配置,即指明開發環境(development)進行了merge。(我有點兒不太理解為什么要這樣做,如果不merge直接寫module.exports={NODE_ENV:'"development'}也是可以的,難道是為了優雅降級?)

還有一點需要注意是的,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下的配置內容:

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配置的厲害之處!
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM