vite过滤console.log

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

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

3、实现:

先安装terser,官网:https://cn.vitejs.dev/config/build-options.html#build-minify

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 ,
            } ,
        } , 
    } ,
...
} )

 

posted @ 2022-07-14 17:19  Pavetr  阅读(2612)  评论(0编辑  收藏  举报