Vite 如何过滤 console.log

1、需求:console.log一般都是在开发环境下使用的,在生产环境下需要去除

2、思路:vite构建时默认使用Esbuild,打包速度是其他打包工具的十几倍,但是缺点也很明显,不具备操作AST的能力,vite也补充了terser来解决这个问题,通过terser的api可以轻松去除console.log,就是打包速度会降下来,果然鱼和熊掌不可兼得;

3、实现:

先安装terser,官网:Vite 官方中文文档 ---- 构建选项

npm add -D terser

再去vite.config.ts里面配置

import { defineConfig } from 'vite'
export default defineConfig( { 
...
    build : {
        minify : 'terser' ,
        terserOptions : {
            compress : {
                drop_console : true ,
                drop_debugger : true ,
            } ,
        } , 
    } ,
...
} )

大功告成!!!

猜你喜欢

转载自blog.csdn.net/weixin_48594833/article/details/134145608