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)一的問題
const path = require('path')
module.exports = {
// 開發(fā)環(huán)境的配置
dev: {
// Paths
assetsSubDirectory: 'static', // 靜態(tài)資源文件夾
assetsPublicPath: '/', // 發(fā)布路徑
// 一般解決跨域請求api
proxyTable: {
'/api': {
target: 'http://api.douban.com/v2', // 目標(biāo)url
changeOrigin: true, // 是否跨域
pathRewrite: {
'^/api': '' // 可以使用 /api 等價于 http://api.douban.com/v2
}
}
},
// Various Dev Server settings
host: 'localhost', // can be overwritten by process.env.HOST
port: 8080, // dev-server的端口號,可以自行更改
autoOpenBrowser: false, // 是否自定代開瀏覽器
errorOverlay: true, // 查詢錯誤
notifyOnErrors: true, // 通知錯誤
poll: false, // poll輪詢,webpack為我們提供devserver是可以監(jiān)控文件改動的,有些情況下卻不能工作,可以設(shè)置一個輪詢解決
/**
* 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)致清除緩存時是否生成source maps,默認(rèn)為true
cssSourceMap: true // 是否開啟cssSourceMap
},
// 生產(chǎn)編譯環(huán)境下的一些配置
build: {
// 下面是相對路徑的拼接
index: path.resolve(__dirname, '../dist/index.html'),
// 下面定義的是靜態(tài)資源的根目錄 也就是dist目錄
assetsRoot: path.resolve(__dirname, '../dist'),
assetsSubDirectory: 'static',
assetsPublicPath: '/', // 下面定義的是靜態(tà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'], // 定義要壓縮哪些類型的文件
// 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 // 是否開啟打包后的分析報告
}
}
config中的 index.js配置項
config中index.js文件是用來配置開發(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的一個全局變量,獲得當(dāng)前文件所在目錄的完整目錄名
? ? assetsRoot: path.resolve(__dirname, '../dist'),// 編譯輸出的靜態(tài)資源路徑
? ? assetsSubDirectory: 'static',// 編譯輸出的二級目錄
? ? assetsPublicPath: '/',// 編譯發(fā)布的根目錄,可配置為資源服務(wù)器域名或 CDN 域名
? ? productionSourceMap: false,// 是否開啟 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,// 是否開啟 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)行測試頁面的端口
? ? autoOpenBrowser: true,//自動打開瀏覽器
? ? assetsSubDirectory: 'static',// 編譯輸出的二級目錄
? ? 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 // 是否開啟 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的作用是,隨著代碼增多,我們需要對代碼進(jìn)行壓縮。
代碼壓縮后進(jìn)行調(diào)bug定位將非常困難,于是引入sourcemap記錄壓縮前后的位置信息記錄,當(dāng)產(chǎn)生錯誤時直接定位到未壓縮前的位置,將大大的方便我們調(diào)試。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于vue路由緩存清除在main.js中的設(shè)置
今天小編就為大家分享一篇關(guān)于vue路由緩存清除在main.js中的設(shè)置,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11
Vuejs學(xué)習(xí)筆記之使用指令v-model完成表單的數(shù)據(jù)雙向綁定
表單類控件承載了一個網(wǎng)頁數(shù)據(jù)的錄入與交互,本章將介紹如何使用指令v-model完成表單的數(shù)據(jù)雙向綁定功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值。感興趣的朋友跟隨小編一起看看吧2019-04-04
Vue3+Vue Router實(shí)現(xiàn)動態(tài)路由導(dǎo)航的示例代碼
隨著單頁面應(yīng)用程序(SPA)的日益流行,前端開發(fā)逐漸向復(fù)雜且交互性強(qiáng)的方向發(fā)展,在這個過程中,Vue.js及其生態(tài)圈的工具(如Vue Router)為我們提供了強(qiáng)大的支持,本文將介紹如何在Vue 3中使用Vue Router實(shí)現(xiàn)動態(tài)路由導(dǎo)航,需要的朋友可以參考下2024-08-08
Vue通過axios發(fā)送ajax請求基礎(chǔ)演示
這篇文章主要介紹了Vue通過axios發(fā)送ajax請求基礎(chǔ)演示,包括了axios發(fā)送簡單get請求,axios get傳參,axios發(fā)送post請求等基礎(chǔ)代碼演示需要的朋友可以參考下2023-02-02
Vue?watch中監(jiān)聽值的變化,判斷后修改值方式
這篇文章主要介紹了Vue?watch中監(jiān)聽值的變化,判斷后修改值方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04
element-plus/element-ui走馬燈配置圖片及圖片自適應(yīng)的最簡便方法
走馬燈功能在展示圖片時經(jīng)常用到,下面這篇文章主要給大家介紹了關(guān)于element-plus/element-ui走馬燈配置圖片及圖片自適應(yīng)的最簡便方法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03

