麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > 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篇,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 看免费毛片 | 久久精品伊人网 | 2021狠狠操| 精品国产一区二区三区四 | 国产精品久久久久久影院8一贰佰 | 日韩黄色一级视频 | 视频一区二区三区在线观看 | 色七七亚洲 | 欧美日韩在线免费观看 | 91精品国产777在线观看 | 欧美精选一区二区 | 国产女王女m视频vk 中文日韩 | 黄色视屏免费在线观看 | 久久经典视频 | 黄网站免费观看视频 | 日本在线观看视频网站 | 中文日韩欧美 | 久久亚洲激情 | 精品成人国产在线观看男人呻吟 | 欧美片一区二区 | 麻豆视频网| 免费在线看黄 | 91精品国产福利尤物免费 | 免费国产视频在线观看 | 国产一区二区视频观看 | 精品国产一区二区三区蜜殿 | 国产午夜精品理论片a级探花 | 欧美精品欧美 | 舌头伸进添的我好爽高潮网站 | 日本网站一区二区三区 | 草莓福利社区在线 | 免费激情视频网站 | 中文字幕视频在线播放 | 亚州综合一区 | 国产v综合v亚洲欧美久久 | 国产激情精品一区二区三区 | 91香草视频| 久久亚洲春色中文字幕久久 | 亚洲男人一区 | 毛片a级毛片免费播放100 | 黄色网址在线视频 |