-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
How to remove console.log ,when vite build use esbuild? #7920
-
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
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 5 -
❤️ 2
export default defineConfig({ esbuild: { drop: ['console', 'debugger'], }, });
Replies: 5 comments 22 replies
-
Have the same confusion, can anyone provide a solution?
Beta Was this translation helpful? Give feedback.
All reactions
-
use minify: 'terser' to build is slower then minify: 'esbuild' 44 seconds
terser build cost 78.73s
==========
esbuild cost 34.86s.
Beta Was this translation helpful? Give feedback.
All reactions
-
export default defineConfig({ esbuild: { drop: ['console', 'debugger'], }, });
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 66 -
🎉 22 -
❤️ 16 -
🚀 5 -
👀 7
-
@blinpete Thanks a lot! It works for me
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
-
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?
Beta Was this translation helpful? Give feedback.
All reactions
-
❤️ 2
-
@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 ;)
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
-
@bigsnowballhehe yeah, at first look it might seem confusing. but
viteusesesbuildfor building and pre-bundling, and thenrollupcomes in for bundling.in shorts, don't worry the config will take effect ;)
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
-
@blinpete if I want to remove console.log and keep console.error, so how shall we do?
Beta Was this translation helpful? Give feedback.
All reactions
-
@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'] : [], } } }
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 40
-
This doesn't work in my config. Vite ^5.1.4 for console logs coming from external deps that are bundled. Any tips?
Beta Was this translation helpful? Give feedback.
All reactions
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
pure: ['console.log']并不是单纯移除,可能会导致严重的后果,请谨慎使用
Beta Was this translation helpful? Give feedback.
All reactions
-
you can try this plugin vite-plugin-remove-console
Beta Was this translation helpful? Give feedback.
All reactions
-
👎 5
-
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)
Beta Was this translation helpful? Give feedback.