vue config配置的詳細說明

//config目錄下index.js配置文件
// 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
        index: path.resolve(__dirname, '../dist/index.html'),
        // 下面定義的是靜態資源的根目錄 也就是dist目錄
        assetsRoot: path.resolve(__dirname, '../dist'),
        // 下面定義的是靜態資源根目錄的子目錄static,也就是dist目錄下面的static
        assetsSubDirectory: 'static',
        // 下面定義的是靜態資源的公開路徑,也就是真正的引用路徑
        assetsPublicPath: '/',
        // 下面定義是否生成生產環境的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-plugin
        productionGzip: 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: 8080,
        // 下面表示是否自定代開瀏覽器
        autoOpenBrowser: true,
        assetsSubDirectory: 'static',
        assetsPublicPath: '/',
        // 下面是代理表,作用是用來,建一個虛擬api服務器用來代理本機的請求,只能用於開發模式
        // 詳見(3)
        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.
        // 是否生成css,map文件,上面這段英文就是說使用這個cssmap可能存在問題,但是按照經驗,問題不大,可以使用
        // 給人覺得沒必要用這個,css出了問題,直接控制檯不就完事了
        cssSourceMap: false
    }
}
(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插件,一般解決跨域請求api
    proxyTable: {
        '/list': {
            target: 'http://api.xxxxxxxx.com', -> 目標url地址
            changeOrigin: true, -> 指示是否跨域
            pathRewrite: {
            '^/list': '/list' -> 可以使用 /list 等價於 api.xxxxxxxx.com/list
            }
        }
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章