1. 程式人生 > >vue-cli配置檔案——config資料夾

vue-cli配置檔案——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後這個檔案又引入了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 :是否開啟打包後的分析報告