欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vite結(jié)合Vue刪除指定環(huán)境的console.log問(wèn)題

 更新時(shí)間:2023年03月11日 16:51:40   作者:小火車(chē)況且況且  
這篇文章主要介紹了Vite結(jié)合Vue刪除指定環(huán)境的console.log問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Vite結(jié)合Vue刪除指定環(huán)境的console.log

Vite結(jié)合Vue刪除指定環(huán)境的console.log

1.可以通過(guò)配置build.minify和build.terserOptions

不推薦是因?yàn)榇虬俣葧?huì)變慢, 并且Vite3.x的版本配置了minify:'terser',需要下載yarn add terser -D

vite.config.ts文件

export default defineConfig(({ command, mode }) => {
return {
	...
	build: {
		minify: "terser",
		terserOptions: {
				compress: {
					drop_console : true	
				}
		}	
	}
	...
}
})

2.通過(guò)賦值為空函數(shù)

main.ts中配置

...
// 這里的 VITE_ENV 是自定義的環(huán)境變量 參考 
// https://cn.vitejs.dev/guide/env-and-mode.html#env-variables
if (import.meta.env.VITE_ENV !== 'development') {
  console.log = function () {}
  // 還可以添加 .info .warn .error
}
...

Vue項(xiàng)目打包并去掉所有的console.log輸出

  • npm run build 生成dist文件夾
  • 路由懶加載
  • 去掉所有的console 安裝插babel-plugin-remove-console(項(xiàng)目上線時(shí)要用到的插件) npm i babel-plugin-transform-remove-console -D
  • 移除打包的第三方包
// 項(xiàng)目在發(fā)布時(shí)需要用到的 babel 插件數(shù)組
const proPlugins = []
// 如果當(dāng)前是測(cè)試環(huán)境或者是生產(chǎn)環(huán)境,則使用去掉 console 的插件
if (process.env.NODE_ENV === 'test' || process.env.NODE_ENV === 'production') {
  proPlugins.push('transform-remove-console')
}
 
module.exports = {
  presets: [
    '@vue/cli-plugin-babel/preset'
  ],
  plugins: [
    [
      'component',
      {
        libraryName: 'element-ui',
        styleLibraryName: 'theme-chalk'
      }
    ],
    ...proPlugins
  ]
 
}

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論