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 語句這個功能只有在打包時才需要,所以我們可以加一個判斷,只在生產(chǎn)環(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 生產(chǎn)包去除console.log
不安裝插件去除console.log的方法
vue-cli3.0在打包過程中就使用了terser-webpack-plugin插件進行優(yōu)化,具體配置可以node_modules/@vue/cli-service/lib/config/prod.js中看到。
這里使用了環(huán)境變量進行控制,只有打生產(chǎn)包的時候才會調(diào)用這個插件進行打包優(yōu)化。
terser-webpack-plugin的具體配置在同一個文件夾下terserOptions.js中,只要在這個文件中compress對象中加入以下幾個屬性就可以了
warnings: false,drop_console: true,drop_debugger: true,pure_funcs: [’console.log’]
到此這篇關(guān)于Vue Cli3 打包配置并自動忽略console.log語句的方法的文章就介紹到這了,更多相關(guān)Vue Cli3打包并忽略console.log內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. Spring Boot和Thymeleaf整合結(jié)合JPA實現(xiàn)分頁效果(實例代碼)2. 詳解Android studio 動態(tài)fragment的用法3. 圖文詳解vue中proto文件的函數(shù)調(diào)用4. 解決Android studio xml界面無法預(yù)覽問題5. php模擬實現(xiàn)斗地主發(fā)牌6. 什么是python的自省7. vue 使用localstorage實現(xiàn)面包屑的操作8. Vue封裝一個TodoList的案例與瀏覽器本地緩存的應(yīng)用實現(xiàn)9. Vuex localStorage的具體使用10. .Net Core使用Coravel實現(xiàn)任務(wù)調(diào)度的完整步驟

網(wǎng)公網(wǎng)安備