vue打包terser壓縮去除控制臺(tái)打印和斷點(diǎn)過(guò)程
情況一
1、vue-cli搭建
代碼壓縮工具terser在vue-cli里面是自動(dòng)支持的,所以直接在vue.config.js里面加入下面配置:
const {defineConfig} = require('@vue/cli-service')
module.exports=defineConfig({
transpileDependencies:true,
terser:{
terserOptions: {
compress: {
drop_console: true, // 移除 console
drop_debugger: true, // 移除 debugger
},
},
}
})2、Vite 搭建
如果你使用的是 Vite 來(lái)構(gòu)建 Vue 3 項(xiàng)目,Terser 已經(jīng)作為默認(rèn)的壓縮工具被內(nèi)置。
你可以通過(guò) vite.config.js 文件來(lái)自定義 Terser 的配置,所以直接加入下面配置即可:
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import { terser } from 'rollup-plugin-terser';
export default defineConfig({
plugins: [
vue(),
terser({
format: {
comments: false, // 不保留注釋
},
compress: {
drop_console: true, // 移除 console
drop_debugger: true, // 移除 debugger
},
}),
],
});3、配置補(bǔ)充說(shuō)明
Terser 提供了許多配置選項(xiàng),以下是一些常用的配置:
- drop_console:移除所有的 console 語(yǔ)句。
- drop_debugger:移除所有的 debugger 語(yǔ)句。
- format:定義輸出格式,例如是否保留注釋。
- compress:一個(gè)對(duì)象,包含多個(gè)壓縮選項(xiàng),如死代碼消除、變量提升等。
情況二
如果用腳手架vue-cli沒(méi)有默認(rèn)安裝這個(gè)插件,可以手動(dòng)安裝,具體步驟如下:
1、安裝插件
npm install terser-webpack-plugin --save-dev
2、vue.config.js里面加入配置
const TerserPlugin = require('terser-webpack-plugin');
module.exports = {
configureWebpack: {
optimization: {
minimizer: [
new TerserPlugin({
terserOptions: {
compress: {
drop_console: true, // 移除 console
drop_debugger: true, // 移除 debugger
},
},
}),
],
},
},
};3、效果對(duì)比
(1)壓縮前打包

并且打包后的代碼里有控制臺(tái)打印相關(guān)的代碼

(2)壓縮打包后

控制臺(tái)打印相關(guān)的代碼也被屏蔽了

4、vue-cli搭建的vue3 完整參考文件
配置如下:
const TerserPlugin = require('terser-webpack-plugin');
module.exports = {
// publicPath: "/zhaopin",
chainWebpack: config => {
config.plugins.delete("fork-ts-checker"); // 禁用fork-ts-checker
},
configureWebpack: //插件配置
{
// plugins:
// [new CopyWebpackPlugin(
// { patterns: [{ from: path.resolve(__dirname, 'static'), to: 'server', }] }
// )
// ]
optimization: {
minimizer: [
new TerserPlugin({
terserOptions: {
compress: {
drop_console: true, // 移除 console
drop_debugger: true, // 移除 debugger
},
},
}),
],
},
},
devServer: {
port: 8080, // 端口號(hào)
// 如果外網(wǎng)想ip訪(fǎng)問(wèn) 屏蔽掉host
// host: 'localhost',
https: false, // https:{type:Boolean}
open: false, // 配置自動(dòng)啟動(dòng)瀏覽器
// proxy: 'http://localhost:3000' // 配置跨域處理,只有一個(gè)代理
proxy: {
'sysApi/': {
// target: 'http://localhost:8088',
target: 'http://1.94.47.xxx:8021/sysApi',
ws: true,
changeOrigin: true,
pathRewrite: {
'^/sysApi': '' // 通過(guò)pathRewrite重寫(xiě)地址,將前綴/api轉(zhuǎn)為/
}
}
} // 配置多個(gè)代理
},
assetsDir: 'static',
lintOnSave: false,
};
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
淺談Vue內(nèi)置component組件的應(yīng)用場(chǎng)景
這篇文章主要介紹了淺談Vue內(nèi)置component組件的應(yīng)用場(chǎng)景,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
Vue項(xiàng)目使用localStorage+Vuex保存用戶(hù)登錄信息
這篇文章主要為大家詳細(xì)介紹了Vue項(xiàng)目使用localStorage+Vuex保存用戶(hù)登錄信息,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05
vue實(shí)現(xiàn)直播間點(diǎn)贊飄心效果的示例代碼
這篇文章主要介紹了vue實(shí)現(xiàn)直播間點(diǎn)贊飄心效果的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
vue+threejs寫(xiě)物體動(dòng)畫(huà)之物體縮放動(dòng)畫(huà)效果
最近在vue中安裝Three.js,無(wú)聊順便研究一些關(guān)于3D圖形化庫(kù),下面這篇文章主要給大家介紹了關(guān)于vue+threejs寫(xiě)物體動(dòng)畫(huà)之物體縮放動(dòng)畫(huà)效果的相關(guān)資料,需要的朋友可以參考下2022-10-10
vue中{{}},v-text和v-html區(qū)別與應(yīng)用詳解
這篇文章主要介紹了vue中{{}},v-text和v-html區(qū)別與應(yīng)用詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09

