1.terser-webpack-plugin 插件的配置
a、首先安裝開發依賴terser-webpack-plugin
b、之后增加vue.config.js文件,並寫入代碼
1 // 打包去掉console 2 const TerserPlugin = require('terser-webpack-plugin') 3 module.exports = { 4 // 打包后默認使用相對路徑,否則會出現首頁空白的情況 5 publicPath: './', 6 // 關閉源代碼映射,當為false時,打包后的文件在瀏覽器源碼目錄將不再顯示webpack://相關的源碼 7 // 不設置此選項,相當於把代碼給別人白嫖 8 productionSourceMap: false, 9 configureWebpack: (config) => { 10 if (process.env.NODE_ENV === 'production') { 11 // 為生產環境修改配置 12 config.mode = 'production' 13 // 將每個依賴包打包成單獨的js文件 14 const optimization = { 15 // 啟用最小化壓縮 16 minimize: true, 17 minimizer: [new TerserPlugin({ 18 // 和productionSourceMap一樣 19 // sourceMap: false, 20 terserOptions: { 21 compress: { 22 // 移除所有console相關的代碼,比如console.log,console.error 23 drop_console: true, 24 // 關閉自動斷點功能,vue代碼里插入debugger指令后,執行到對應位置會自動斷線,此選項是移除debugger指令 25 drop_debugger: true, 26 // pure_funcs數組是用來配置移除指定的指令,比如console.log alert等等 27 // 移除console.log,需要配合.eslintrc.js文件里的如下設置,不然打包會出警告 28 // rules: { 29 // 'no-console': 'off', 30 // } 31 pure_funcs: ['console.log', 'console.error'] 32 } 33 } 34 })] 35 } 36 // 將optimization的所有屬性合並到config里 37 Object.assign(config, { 38 optimization 39 }) 40 } else { 41 // 為開發環境修改配置 42 config.mode = 'development' 43 } 44 } 45 }