index.js
用于定義開發(fā)環(huán)境和生產(chǎn)環(huán)境所需要的參數(shù)
'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ā)布路徑
// 一般解決跨域請(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, // 是否自定代開瀏覽器
errorOverlay: true, // 查詢錯(cuò)誤
notifyOnErrors: true, // 通知錯(cuò)誤
poll: false, // poll輪詢,webpack為我們提供devserver是可以監(jiān)控文件改動(dòng)的籍铁,有些情況下卻不能工作堡距,可以設(shè)置一個(gè)輪詢解決
/**
* 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 // 是否開啟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)資源的公開路徑,也就是真正的引用路徑
/**
* 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 // 是否開啟打包后的分析報(bào)告
}
}
dev.env.js
'use strict'
// 該插件是用來合并對(duì)象中燥,也就是配置文件用的
const merge = require('webpack-merge')
const prodEnv = require('./prod.env')
// 將兩個(gè)配置對(duì)象合并導(dǎo)出,NODE_ENV是一個(gè)環(huán)境變量塘偎,指定development環(huán)境
module.exports = merge(prodEnv, {
NODE_ENV: '"development"'
})
prod.env.js
// 導(dǎo)出一個(gè)對(duì)象疗涉,NODE_ENV是一個(gè)環(huán)境變量拿霉,指定production環(huán)境
'use strict'
module.exports = {
NODE_ENV: '"production"'
}