vue中的config目錄下index.js解讀
vue的config目錄下index.js
'use strict'
// Template version: 1.3.1
// see http://vuejs-templates.github.io/webpack for documentation.
// 用于處理路徑統(tǒng)一的問(wèn)題
const path = require('path')
module.exports = {
// 開(kāi)發(fā)環(huán)境的配置
dev: {
// Paths
assetsSubDirectory: 'static', // 靜態(tài)資源文件夾
assetsPublicPath: '/', // 發(fā)布路徑
// 一般解決跨域請(qǐng)求api
proxyTable: {
'/api': {
target: 'http://api.douban.com/v2', // 目標(biāo)url
changeOrigin: true, // 是否跨域
pathRewrite: {
'^/api': '' // 可以使用 /api 等價(jià)于 http://api.douban.com/v2
}
}
},
// Various Dev Server settings
host: 'localhost', // can be overwritten by process.env.HOST
port: 8080, // dev-server的端口號(hào),可以自行更改
autoOpenBrowser: false, // 是否自定代開(kāi)瀏覽器
errorOverlay: true, // 查詢(xún)錯(cuò)誤
notifyOnErrors: true, // 通知錯(cuò)誤
poll: false, // poll輪詢(xún),webpack為我們提供devserver是可以監(jiān)控文件改動(dòng)的,有些情況下卻不能工作,可以設(shè)置一個(gè)輪詢(xún)解決
/**
* Source Maps
*/
// https://webpack.js.org/configuration/devtool/#development
devtool: 'cheap-module-eval-source-map', // webpack用于方便調(diào)試的配置
// 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, // devtool的配置當(dāng)文件名插入新的hash導(dǎo)致清除緩存時(shí)是否生成source maps,默認(rèn)為true
cssSourceMap: true // 是否開(kāi)啟cssSourceMap
},
// 生產(chǎn)編譯環(huán)境下的一些配置
build: {
// 下面是相對(duì)路徑的拼接
index: path.resolve(__dirname, '../dist/index.html'),
// 下面定義的是靜態(tài)資源的根目錄 也就是dist目錄
assetsRoot: path.resolve(__dirname, '../dist'),
assetsSubDirectory: 'static',
assetsPublicPath: '/', // 下面定義的是靜態(tài)資源的公開(kāi)路徑,也就是真正的引用路徑
/**
* 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, // 是否在生產(chǎn)環(huán)境中壓縮代碼,如果要壓縮必須安裝compression-webpack-plugin
productionGzipExtensions: ['js', 'css'], // 定義要壓縮哪些類(lèi)型的文件
// 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 // 是否開(kāi)啟打包后的分析報(bào)告
}
}
config中的 index.js配置項(xiàng)
config中index.js文件是用來(lái)配置開(kāi)發(fā)環(huán)境和生產(chǎn)環(huán)境的配置參數(shù)
index.js:
const path = require('path')
?
module.exports = {
? build: { ? ? ? // production 環(huán)境
? ? env: require('./prod.env'), // 使用 config/prod.env.js 中定義的編譯環(huán)境
? ? index: path.resolve(__dirname, '../dist/index.html'),//編譯輸入的 index.html 文件, __dirname 是node的一個(gè)全局變量,獲得當(dāng)前文件所在目錄的完整目錄名
? ? assetsRoot: path.resolve(__dirname, '../dist'),// 編譯輸出的靜態(tài)資源路徑
? ? assetsSubDirectory: 'static',// 編譯輸出的二級(jí)目錄
? ? assetsPublicPath: '/',// 編譯發(fā)布的根目錄,可配置為資源服務(wù)器域名或 CDN 域名
? ? productionSourceMap: false,// 是否開(kāi)啟 cssSourceMap
? ? // 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,// 是否開(kāi)啟 gzip
? ? productionGzipExtensions: ['js', 'css'],// 需要使用 gzip 壓縮的文件擴(kuò)展名
? ? // 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
? },
? dev: {// dev 環(huán)境
? ? env: require('./dev.env'),// 使用 config/dev.env.js 中定義的編譯環(huán)境
? ? port: process.env.PORT || 8080,// 運(yùn)行測(cè)試頁(yè)面的端口
? ? autoOpenBrowser: true,//自動(dòng)打開(kāi)瀏覽器
? ? assetsSubDirectory: 'static',// 編譯輸出的二級(jí)目錄
? ? assetsPublicPath: '/', // 編譯發(fā)布的根目錄,可配置為資源服務(wù)器域名或 CDN 域名
? ? proxyTable: {}, // 需要 proxyTable 代理的接口(可跨域)
? ? // 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 // 是否開(kāi)啟 cssSourceMap
? }
}config/prod.env.js :
module.exports = {
? NODE_ENV: '"production"'
}config/dev.env.js
onst merge = require('webpack-merge')
const prodEnv = require('./prod.env')
?
module.exports = merge(prodEnv, {
? NODE_ENV: '"development"'
})關(guān)于cssSourceMap的作用是,隨著代碼增多,我們需要對(duì)代碼進(jìn)行壓縮。
代碼壓縮后進(jìn)行調(diào)bug定位將非常困難,于是引入sourcemap記錄壓縮前后的位置信息記錄,當(dāng)產(chǎn)生錯(cuò)誤時(shí)直接定位到未壓縮前的位置,將大大的方便我們調(diào)試。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue微信分享的實(shí)現(xiàn)(在當(dāng)前頁(yè)面分享其他頁(yè)面)
這篇文章主要介紹了vue微信分享,在當(dāng)前頁(yè)面分享其他頁(yè)面,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-04-04
從0到1構(gòu)建vueSSR項(xiàng)目之node以及vue-cli3的配置
這篇文章主要介紹了從0到1構(gòu)建vueSSR項(xiàng)目之node以及vue-cli3的配置,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-03-03
Vue選項(xiàng)之propsData傳遞數(shù)據(jù)方式
這篇文章主要介紹了Vue選項(xiàng)之propsData傳遞數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
Vue動(dòng)態(tài)構(gòu)建混合數(shù)據(jù)Treeselect選擇樹(shù)及巨樹(shù)問(wèn)題的解決
這篇文章主要介紹了Vue動(dòng)態(tài)構(gòu)建混合數(shù)據(jù)Treeselect選擇樹(shù)及巨樹(shù)問(wèn)題的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
rem實(shí)現(xiàn)響應(yīng)式布局的思路詳解
這篇文章主要為大家介紹了rem實(shí)現(xiàn)響應(yīng)式布局的思路詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03

