Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to remove console.log ,when vite build use esbuild? #7920

Answered by tatsunix
tomatobybike asked this question in Q&A
Discussion options

vite.config.js

export default defineConfig({
 base: './',
 build:{
 sourcemap: false,
 minify: 'esbuild',
 // minify: 'terser',
 // terserOptions: {
 // compress: {
 // warnings: false,
 // drop_console: true,
 // drop_debugger: true,
 // pure_funcs: ['console.log']
 // }
 // }
 }})

===============================
I want to use esbuild to build,because because esbuild so faster then terser,but how can i remove console.log ,use esbuild

You must be logged in to vote
export default defineConfig({
 esbuild: {
 drop: ['console', 'debugger'],
 },
});

Replies: 5 comments 22 replies

Comment options

Have the same confusion, can anyone provide a solution?

You must be logged in to vote
0 replies
Comment options

use minify: 'terser' to build is slower then minify: 'esbuild' 44 seconds

terser build cost 78.73s

==========
esbuild cost 34.86s.

You must be logged in to vote
0 replies
Comment options

export default defineConfig({
 esbuild: {
 drop: ['console', 'debugger'],
 },
});
You must be logged in to vote
15 replies
Comment options

@blinpete Thanks a lot! It works for me

Comment options

export default defineConfig({
 esbuild: {
 drop: ['console', 'debugger'],
 },
});

But..vite use rollup to build. https://vitejs.dev/guide/why.html#why-not-bundle-with-esbuild So this configuration will not take effect?

Comment options

@bigsnowballhehe
yeah, at first look it might seem confusing. but vite uses esbuild for building and pre-bundling, and then rollup comes in for bundling.

in shorts, don't worry the config will take effect ;)

Comment options

@bigsnowballhehe yeah, at first look it might seem confusing. but vite uses esbuild for building and pre-bundling, and then rollup comes in for bundling.

in shorts, don't worry the config will take effect ;)

Thanks!

Comment options

Maybe I don't understand smth, but is it okay that in bundle files I still have my console statements? But in running app there is no logs.

Answer selected by tomatobybike
Comment options

@blinpete if I want to remove console.log and keep console.error, so how shall we do?

You must be logged in to vote
6 replies
Comment options

@fengxinming use pure configuration like this:

export default defineConfig(({ mode }) => {
 return {
 // other configuration
 esbuild: {
 pure: ['console.log'],
 }
 }
}

if you just want to remove console.log when build production dist (but keep it in development env or staging env), you can use pure configuration like this:

export default defineConfig(({ mode }) => {
 return {
 // other configuration
 esbuild: {
 pure: mode === 'production' ? ['console.log'] : [],
 }
 }
}
Comment options

This doesn't work in my config. Vite ^5.1.4 for console logs coming from external deps that are bundled. Any tips?

Comment options

The problem was my deps had changed console to some other minified name l.warn so I had to add a manual pure for that.

Comment options

Note that using pure may keep function calls in the arguments part.

For example console.assert(isValid(v), "Invalid value", v) will be replaced with isValid(v) if console.assert is defined as "pure", but isValid is not.

Unfortunately there is no fine grained built-in way to drop only some methods of console with their arguments, for example dropping console.log, but keeping console.error.

Comment options

pure: ['console.log']并不是单纯移除,可能会导致严重的后果,请谨慎使用

Comment options

you can try this plugin vite-plugin-remove-console

You must be logged in to vote
1 reply
Comment options

error during build:
TypeError: (0 , w1.default)(...).find is not a function
 at P1 (file:///home/user/project/node_modules/vite-plugin-remove-console/dist/index.mjs:277:13210)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

AltStyle によって変換されたページ (->オリジナル) /