// see http://vuejs-templates.github.io/webpack for documentation.
// path是node.js的路徑模塊,用來處理路徑統一的問題
var path = require('path')module.exports = {
// 下面是build也就是生產編譯環境下的一些配置build: {
// 導入prod.env.js配置文件,只要用來指定當前環境,詳細見(1)env: require('./prod.env'),
// 下面是相對路徑的拼接,假如當前跟目錄是config,那么下面配置的index屬性的屬性值就是dist/index.html
//path.resolve() 方法會把一個路徑或路徑片段的序列解析為一個絕對路徑。index: path.resolve(__dirname, '../dist/index.html'),adminIndex: path.resolve(__dirname, '../dist/admin.html'),
// 下面定義的是靜態資源的根目錄 也就是dist目錄assetsRoot: path.resolve(__dirname, '../dist'),
// 下面定義的是靜態資源根目錄的子目錄static,也就是dist目錄下面的staticassetsSubDirectory: 'static',
// 下面定義的是靜態資源的公開路徑,也就是真正的引用路徑assetsPublicPath: 'https://cdn.XXXX.cn/siteResource/pop-online-fund-vote-web/',
// 下面定義是否生成生產環境的sourcmap,sourcmap是用來debug編譯后文件的,通過映射到編譯前文件來實現productionSourceMap: true,// 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
// 下面是是否在生產環境中壓縮代碼,如果要壓縮必須安裝compression-webpack-pluginproductionGzip: 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
// 下面是用來開啟編譯完成后的報告,可以通過設置值為true和false來開啟或關閉
// 下面的process.env.npm_config_report表示定義的一個npm_config_report環境變量,可以自行設置bundleAnalyzerReport: process.env.npm_config_report},dev: {
// 引入當前目錄下的dev.env.js,用來指明開發環境,詳見(2)env: require('./dev.env'),
// 下面是dev-server的端口號,可以自行更改port: 443,
// 下面表示是否自定代開瀏覽器autoOpenBrowser: true,assetsSubDirectory: 'static',assetsPublicPath: '/',
// 下面是代理表,作用是用來,建一個虛擬api服務器用來代理本機的請求,只能用于開發模式 // 詳見(3)proxyTable: {'/api': {target: 'http://localhost:8082',pathRewrite: {'^/api': '/'}},'/pop-shared-web-components': {target: 'http://pop-shared-web-components.cn:3000',pathRewrite: {'^/pop-shared-web-components': '/'}}},// 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.
// 是否生成css,map文件,上面這段英文就是說使用這個cssmap可能存在問題,但是按照經驗,問題不大,可以使用
// 給人覺得沒必要用這個,css出了問題,直接控制臺不就完事了cssSourceMap: false}
}
這是index.js,再說一下config下的dev.env.js和prod.env.js文件。
(1)下面是prod.env.js的配置內容module.exports = {// 作用很明顯,就是導出一個對象,NODE_ENV是一個環境變量,指定production環境NODE_ENV: '"production"'}
(2)下面是dev.env.js的配置內容// 首先引入的是webpack的merge插件,該插件是用來合并對象,也就是配置文件用的,相同的選項會被后者覆蓋,至于這里為什么多次一舉,可能另有他圖吧var merge = require('webpack-merge')// 導入prod.env.js配置文件var prodEnv = require('./prod.env')// 將兩個配置對象合并,最終結果是 NODE_ENV: '"development"'module.exports = merge(prodEnv, {NODE_ENV: '"development"'})
(3)下面是proxyTable的一般用法//vue-cli使用這個功能是借助http-proxy-middleware插件,一般解決跨域請求apiproxyTable: {'/list': {target: 'http://api.xxxxxxxx.com', -> 目標url地址changeOrigin: true, -> 指示是否跨域pathRewrite: {'^/list': '/list' -> 可以使用 /list 等價于 api.xxxxxxxx.com/list}}}