Vue Cli3 打包配置并自動忽略console.log語句的方法
下載插件
npm i -D uglifyjs-webpack-plugin
在 vue.config.js 引入使用
const UglifyJsPlugin = require(’uglifyjs-webpack-plugin’)module.exports = { configureWebpack: { plugins: [ new UglifyJsPlugin({ uglifyOptions: { compress: { drop_console: true, }, }, }), ], }, devServer: { proxy: { ’/xxx’: { target: ’http://192.168.150.17:8080/’, changeOrigin: true, ws: true, pathRewrite: { ’^/xxx’: ’xxx’, }, }, }, }, publicPath: ’./’,}
這時執(zhí)行 npm run build 打包后的文件就沒有 console.log 語句了。
不過這時會有一個問題,就是在開發(fā)環(huán)境的時候編譯會非常慢。例如修改了一個變量的值,我的電腦要編譯 10 秒才能重新刷出來頁面,一直卡在 92% chunk asset optimization。
由于去掉 console.log 語句這個功能只有在打包時才需要,所以我們可以加一個判斷,只在生產環(huán)境時才把上述配置代碼加上。
所以正確的配置如下:
const UglifyJsPlugin = require(’uglifyjs-webpack-plugin’)const config = { devServer: { proxy: { ’/xxx’: { target: ’http://192.168.150.17:8080/’, changeOrigin: true, ws: true, pathRewrite: { ’^/xxx’: ’xxx’, }, }, }, }, publicPath: ’./’,}if (process.env.NODE_ENV === ’production’) { config.configureWebpack = { plugins: [ new UglifyJsPlugin({ uglifyOptions: { compress: { drop_console: true, }, }, }), ], }}module.exports = config
vue-cli3.0 生產包去除console.log
不安裝插件去除console.log的方法
vue-cli3.0在打包過程中就使用了terser-webpack-plugin插件進行優(yōu)化,具體配置可以node_modules/@vue/cli-service/lib/config/prod.js中看到。
這里使用了環(huán)境變量進行控制,只有打生產包的時候才會調用這個插件進行打包優(yōu)化。
terser-webpack-plugin的具體配置在同一個文件夾下terserOptions.js中,只要在這個文件中compress對象中加入以下幾個屬性就可以了
warnings: false,drop_console: true,drop_debugger: true,pure_funcs: [’console.log’]
到此這篇關于Vue Cli3 打包配置并自動忽略console.log語句的方法的文章就介紹到這了,更多相關Vue Cli3打包并忽略console.log內容請搜索好吧啦網以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
