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)文章
vue3實(shí)現(xiàn)動(dòng)態(tài)路由及菜單
這篇文章主要介紹了vue3實(shí)現(xiàn)動(dòng)態(tài)路由及菜單,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03關(guān)于vue路由緩存清除在main.js中的設(shè)置
今天小編就為大家分享一篇關(guān)于vue路由緩存清除在main.js中的設(shè)置,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11Vuejs學(xué)習(xí)筆記之使用指令v-model完成表單的數(shù)據(jù)雙向綁定
表單類(lèi)控件承載了一個(gè)網(wǎng)頁(yè)數(shù)據(jù)的錄入與交互,本章將介紹如何使用指令v-model完成表單的數(shù)據(jù)雙向綁定功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值。感興趣的朋友跟隨小編一起看看吧2019-04-04Vue組件設(shè)計(jì)-滾動(dòng)置頂設(shè)計(jì)案例
這篇文章主要介紹了Vue組件設(shè)計(jì)-滾動(dòng)置頂設(shè)計(jì)案例,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04Vue3+Vue Router實(shí)現(xiàn)動(dòng)態(tài)路由導(dǎo)航的示例代碼
隨著單頁(yè)面應(yīng)用程序(SPA)的日益流行,前端開(kāi)發(fā)逐漸向復(fù)雜且交互性強(qiáng)的方向發(fā)展,在這個(gè)過(guò)程中,Vue.js及其生態(tài)圈的工具(如Vue Router)為我們提供了強(qiáng)大的支持,本文將介紹如何在Vue 3中使用Vue Router實(shí)現(xiàn)動(dòng)態(tài)路由導(dǎo)航,需要的朋友可以參考下2024-08-08Vue通過(guò)axios發(fā)送ajax請(qǐng)求基礎(chǔ)演示
這篇文章主要介紹了Vue通過(guò)axios發(fā)送ajax請(qǐng)求基礎(chǔ)演示,包括了axios發(fā)送簡(jiǎn)單get請(qǐng)求,axios get傳參,axios發(fā)送post請(qǐng)求等基礎(chǔ)代碼演示需要的朋友可以參考下2023-02-02使用mint-ui開(kāi)發(fā)項(xiàng)目的一些心得(分享)
下面小編就為大家?guī)?lái)一篇使用mint-ui開(kāi)發(fā)項(xiàng)目的一些心得(分享)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09Vue?watch中監(jiān)聽(tīng)值的變化,判斷后修改值方式
這篇文章主要介紹了Vue?watch中監(jiān)聽(tīng)值的變化,判斷后修改值方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04element-plus/element-ui走馬燈配置圖片及圖片自適應(yīng)的最簡(jiǎn)便方法
走馬燈功能在展示圖片時(shí)經(jīng)常用到,下面這篇文章主要給大家介紹了關(guān)于element-plus/element-ui走馬燈配置圖片及圖片自適應(yīng)的最簡(jiǎn)便方法,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03