国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

vue-cli配置文件――config篇

2019-11-19 14:36:00
字體:
來源:轉載
供稿:網友

最近一直在研究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.jsconst 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的內容還遠不止這些,想了解更多關于這個模塊的朋友請訪問 也是可以的,難道是為了優雅降級?)

還有一點需要注意是的, 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,不過文檔中還寫了一句話:“Turning this off can help with source map debugging.”額。。。

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文件夾下的內容基本上就過完了,正如名字告訴我們的,這三個文件僅僅是寫死的配置文件,截止目前還沒遇到太多

總結

以上所述是小編給大家介紹的vue-cli配置文件――config篇,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 镇康县| 清丰县| 西盟| 出国| 东港市| 吴川市| 桂阳县| 慈利县| 汾西县| 闸北区| 临颍县| 酒泉市| 永清县| 柞水县| 巴林右旗| 安阳县| 宣恩县| 齐齐哈尔市| 福贡县| 全椒县| 乐清市| 兖州市| 宿州市| 松阳县| 桃源县| 枝江市| 林周县| 克东县| 石景山区| 南宁市| 峨边| 江陵县| 和硕县| 长春市| 马边| 吉隆县| 万宁市| 即墨市| 丰都县| 营口市| 普安县|