手撕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配置的厲害之處!
相關推薦
手撕vue-cli配置檔案——config篇
最近一直在研究webpack,突然想看看vue-cli中的webpack是如何配置,查閱了很多相關的文章,所以也想出幾篇關於vue-cli配置的東西。正所謂“工欲善其事必先利其器”嘛!這一篇主要是分析vue中關於config資料夾中的相關程式碼; 首先我們先看一下config的檔案結構: |-config
手撕vue-cli配置檔案——check-versions.js篇
check-versions.js,vue-cli中檢查版本的js檔案。 'use strict' const chalk = require('chalk') const semver = require('semver') const packageConfig = require('../packag
手撕vue-cli配置——utils.js篇
utils.js檔案主要是用來處理各種css loader的,比如css-loader,less-loader等。 //引入path模組 const path = require('path') //引入之前的config模組 const config = require('../config') //
手撕vue-cli配置文件——check-versions.js篇
pac 更多 分代 常用 改變 engines shell. 檢測 emp check-versions.js,vue-cli中檢查版本的js文件。 ‘use strict‘ const chalk = require(‘chalk‘) const semver = re
手撕vue-cli配置——webpack.prod.conf.js篇
'use strict' const path = require('path') const utils = require('./utils') const webpack = require('webpack') const config = require('../config') const me
手撕vue-cli配置——webpack.dev.conf.js篇
const utils = require('./utils') const webpack = require('webpack') const config = require('../config') const merge = require('webpack-merge') const path
手撕vue-cli配置——webpack.base.conf.js篇
在開始寫webpack.base.conf.js(簡稱base)之前,我們先來看一下vue-loader.conf.js這個檔案,畢竟在base中我們還會用到: 'use strict' //引入前一篇文章的utils檔案 const utils = require('./utils') //引入conf
vue-cli配置檔案——config資料夾
首先我們先看一下config的檔案結構: |-config |—dev.env.js |—index.js |—prod.env.js 開啟我們的vue專案資料夾我們可以清楚的看到資料夾下的三個檔案,“dev.env.js”,“index.js”,“prod.e
VUE——在配置檔案config/index.js中配置統一請求介面
在配置檔案config/index.js找到 module.exports={ dev:{ proxyTable:{} } } 更改為: proxyTable:{ "/api":
vue-cli配置檔案解釋
{ "name": "vue-lesson", "description": "A Vue.js project", "version": "1.0.0", "author": "wan
vuejs第二篇 vue-cli配置移動端自適配(引入flexible)
原文網址 http://hjingren.cn/2017/06/16/%E5%9F%BA%E4%BA%8Evue-cli%E9%85%8D%E7%BD%AE%E7%A7%BB%E5%8A%A8%E7%AB%AF%E8%87%AA%E9%80%82%E5%BA%94/ 1.
Flask 配置檔案config之from_object 篇
Flask Web開發看到第7章,硬著頭皮感覺看不下去 回過頭去補基礎 這裡講到 app.config.from_object() 這個載入配置的方式 首先,app是通過app=Flask(__name__)來實現的 那麼,app的config方法肯定是通過Flask類裡
vue-cli配置postcss
fig plugins mod all port exp gin com css 1. npm install postcss-cssnext -D npm install postcss-import -D npm install postcss-loader-D 2.
vue-cli 配置項目名和域名
bsp cal mage ges run 圖片 反向 http image 默認運行 npm run dev 域名為localhost:8080 但是需要配置反向代理,這時候就需要去配置域名跟項目名 vue-cli 配置項目名和域名
2、配置檔案config.properties及瀏覽器啟動
配置檔案如下: 瀏覽器啟動選擇: 新建BrowserEngine.java檔案,程式碼如下: package framework;import java.io.FileInputStream;import java.io.IOException;import java.io.InputSt
vue-cli配置axios,並基於axios進行後臺請求函式封裝
文章https://www.cnblogs.com/XHappyness/p/7677153.html已經對axios配置進行了說明,後臺請求時可直接this.$axios直接進行。這裡的缺點是後端請求會散亂在各個元件中,導致複用和維護艱難。 升級:將請求封裝在一個檔案中並加上型別宣告 步驟: &nbs
vue-cli配置多頁面專案
背景:vue-cli版本3.0 省略初始化專案;預設的App.vue和index.html檔案可以刪掉 頁面結構如上圖,新加的home可以不要,注意下面的檔案路徑就行 接下來修改配置檔案: 1、修改build/utils.js /* 這裡是新
tp3.2不能自動載入公共配置檔案config.php
原因在: 1:ThinkPHP.php 呼叫了Think.class.php類檔案start()的靜態方法 2: 開啟ThinkPHP3.2的入口檔案ThinkPHP.php,找到33行
讀取配置檔案config.properties的方法
引入的jar包: 這兩個jar包版本是配套的,建議使用maven 使用的jar版本需要與spring整體版本適應 commons-configuration-1.8.jar commons-lang-2.6.jar(commons-configuration的依賴
SpringBoot專欄-配置檔案終結篇_知識點彙總(第四講)
配置檔案YML SpringBoot使用一個全域性的配置檔案,配置檔名是固定的; application.properties 、application.yml 配置檔案的作用:修改SpringBoot自動配置的預設值;SpringBoot在底層都給我們自動配