How Vite filters console.log

1. Requirements:console.log is generally used in the development environment and needs to be removed in the production environment

2. Idea:Vite uses Esbuild by default when building. The packaging speed is more than ten times that of other packaging tools. However, the shortcomings are also obvious. It does not have the ability to operate AST. Vite Terser has also been added to solve this problem. Console.log can be easily removed through Terser's API, but the packaging speed will slow down. Sure enough, you can't have your cake and eat it too;

3. Implementation:

Install terser first, official website:Vite official Chinese document ---- Build options

npm add -D terser

Then go to vite.config.ts to configure

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

You're done! ! !

Guess you like

Origin blog.csdn.net/weixin_48594833/article/details/134145608