vue.config.js詳細配置總結

1,基於vue-CLI3下的vue.config.js詳細配置

const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const cdn = {
    css: [],
    js: [
        'https://xxx-cdn-path/vue.runtime.min.js',
        'https://xxx-cdn-path/vue-router.min.js',
        'https://xxx-cdn-path/vuex.min.js',
        'https://xxx-cdn-path/axios.min.js',
    ]
}

module.exports = {
    // 部署應用時的基本 URL
    publicPath: process.env.NODE_ENV === 'production' 
         ? '192.168.60.110:8080' : './',

    // build時構建文件的目錄 構建時傳入 --no-clean 可關閉該行爲
    outputDir: 'dist',

    // build時放置生成的靜態資源 (js、css、img、fonts) 的 (相對於 outputDir 的) 目錄
    assetsDir: 'static',

    // 指定生成的 index.html 的輸出路徑 (相對於 outputDir)。也可以是一個絕對路徑。
    indexPath: 'index.html',

    // 默認在生成的靜態資源文件名中包含hash以控制緩存
    filenameHashing: true,

    // 構建多頁面應用,頁面的配置
    pages: {
        index: {
            // page 的入口
            entry: 'src/index/main.js',
            // 模板來源
            template: 'public/index.html',
            // 在 dist/index.html 的輸出
            filename: 'index.html',
            // 當使用 title 選項時,
            // template 中的 title 標籤需要是 <title><%= htmlWebpackPlugin.options.title %></title>
            title: 'Index Page',
            // 在這個頁面中包含的塊,默認情況下會包含
            // 提取出來的通用 chunk 和 vendor chunk。
            chunks: ['chunk-vendors', 'chunk-common', 'index']
        },
        // 當使用只有入口的字符串格式時,
        // 模板會被推導爲 `public/subpage.html`
        // 並且如果找不到的話,就回退到 `public/index.html`。
        // 輸出文件名會被推導爲 `subpage.html`。
        subpage: 'src/subpage/main.js'
    },

    // 是否在開發環境下通過 eslint-loader, `ture` | `false` | `"error"`
    // 在每次保存時 lint 代碼 (在生產構建時禁用 eslint-loader)
    lintOnSave: process.env.NODE_ENV !== 'production',

    // 是否使用包含運行時編譯器的 Vue 構建版本
    runtimeCompiler: false,

    // Babel 顯式轉譯列表
    transpileDependencies: [],

    // 如果你不需要生產環境的 source map,可以將其設置爲 false 以加速生產環境構建
    productionSourceMap: true,

    // 設置生成的 HTML 中 <link rel="stylesheet"> 和 <script> 標籤的 crossorigin 屬性(注:僅影響構建時注入的標籤)
    crossorigin: '',

    // 在生成的 HTML 中的 <link rel="stylesheet"> 和 <script> 標籤上啓用 Subresource Integrity (SRI)
    integrity: false,

    // 如果這個值是一個對象,則會通過 webpack-merge 合併到最終的配置中
    // 如果你需要基於環境有條件地配置行爲,或者想要直接修改配置,那就換成一個函數 (該函數會在環境變量被設置之後懶執行)。該方法的第一個參數會收到已經解析好的配置。在函數內,你可以直接修改配置,或者返回一個將會被合併的對象
    configureWebpack: config => { 
        if (process.env.NODE_ENV === 'production') {
            // 爲生產環境修改配置...
            config.plugins.push(
                //添加代碼壓縮工具,及設置生產環境自動刪除console
                new UglifyJsPlugin({
                    uglifyOptions: {
                        compress: {
                            warnings: false,
                            drop_debugger: true,
                            drop_console: true,
                        },
                    },
                    sourceMap: false,
                    parallel: true,
                })
            );
            // 用cdn方式引入,分離第三方插件
            config.externals = {
                'vue': 'Vue',
                'vuex': 'Vuex',
                'vue-router': 'VueRouter',
                'axios': 'axios'
                } 
        } else {
            // 爲開發環境修改配置...
        }
    },

    // 對內部的 webpack 配置(比如修改、增加Loader選項)(鏈式操作)
    chainWebpack: config => {
        // 引入babel-polyfill
        config
            .entry('index')
            .add('babel-polyfill')
            .end();
        // 添加文件路徑別名
        config.resolve.alias.set("@", resolve("src"));
        if (isProduction) {
            // 生產環境注入cdn
            config.plugin('html')
                .tap(args => { 
                    args[0].cdn = cdn;
                    return args;
                });
        }
    },

    // css的處理
    css: {
        // 當爲true時,css文件名可省略 module 默認爲 false
        modules: true,
        // 是否將組件中的 CSS 提取至一個獨立的 CSS 文件中,當作爲一個庫構建時,你也可以將其設置爲 false 免得用戶自己導入 CSS
        // 默認生產環境下是 true,開發環境下是 false
        extract: false,
        // 是否爲 CSS 開啓 source map。設置爲 true 之後可能會影響構建的性能
        sourceMap: false,
        //向 CSS 相關的 loader 傳遞選項(支持 css-loader postcss-loader sass-loader less-loader stylus-loader)
        loaderOptions: {
            css: {},
            less: {},
            sass: {
                // 自動注入全局變量樣式
                data: ` @import "src/你的全局scss文件路徑"; `
            }
        }
    },

    // 所有 webpack-dev-server 的選項都支持
    // webpack-dev-server 相關配置
    devServer: { // 設置代理
        hot: true, //熱加載
        host: '0.0.0.0', //ip地址
        port: 8080, //端口
        https: false, //false關閉 https,true 爲開啓
        open: true, //自動打開瀏覽器
                // 設置讓瀏覽器 overlay 同時顯示警告和錯誤
        overlay: {
            warnings: true,
            errors: true
        },
        proxy: {
            '/api': { //本地                                        
            target: 'localhost:8080/',
            // 如果要代理 websockets
            ws: true,
            changeOrigin: true
      },
      '/test': { //測試
        target: 'localhost:8082/'
      },
      '/pre-release': {  //預發佈
        target: 'http://XXX.com/'
      },
      '/production': { //正式
        target: 'http://XXX.com/'
      }
        }
    },

    // 是否爲 Babel 或 TypeScript 使用 thread-loader
    // 該選項在系統的 CPU 有多於一個內核時自動啓用,僅作用於生產構建。
    parallel: require('os').cpus().length > 1,

    // 向 PWA 插件傳遞選項
    pwa: {},

    // 可以用來傳遞任何第三方插件選項
    pluginOptions: { }
}

2,package.json 配置

  • json文件不能用//註釋,

  • 以下各種build命令適用於不同環境

  "scripts": {
    "serve": "vue-cli-service serve",
    
   //  "zzzzzzzzz": "打包默認會刪除build 目錄及文件,可以增加 --no-clean 每次不刪除",
    "build": "vue-cli-service build",          
    "build:pre": "vue-cli-service build --mode pre-release",  
    "build:test": "vue-cli-service build --mode test", 
    "build:prod": "vue-cli-service build --mode production", 
    "lint": "vue-cli-service lint",
    "test:unit": "vue-cli-service test:unit"
  },

3,前端調試工具—Source Map

SourceMap 使用教程

第一次見Source Map這個專業詞語…

還是參考阮一峯大神的博客!!!!

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