esbuild icon indicating copy to clipboard operation
esbuild copied to clipboard

Not removing / tree shaking unused imports with sideEffects: false when define is used

Open natew opened this issue 2 years ago • 3 comments

Here's a small repro:

https://esbuild.github.io/try/#dAAwLjE5LjUALS1kZWZpbmU6cHJvY2Vzcy5lbnYuVEVTVD0nIjAiJyAtLW1pbmlmeQBpbXBvcnQgeyBqb2luIH0gZnJvbSAncGF0aCcKCmlmIChwcm9jZXNzLmVudi5URVNUID09PSAnMScpIHsKICBqb2luKCcnLCAnJykKfQ

Edit: So it works with bundle mode, is this meant to only work in bundle mode?

Though not perfect in the sandbox because you'd want a separate module with sideEffects: false to show it.

Basically esbuild will leave behind imports, which then doesn't play along nicely with esbuild later on in your build which will complain:

▲ [WARNING] Ignoring this import because "../../packages/xyz/dist/index.js" was marked as having no side effects [ignored-bare-import

The combination of the two behaviors makes for unavoidable warnings, ideally esbuild should be able to remove imports like the above.

natew avatar Oct 17 '23 14:10 natew

The transform API and the build API with bundling disabled just pass through all imports unmodified. You need to enable bundling for esbuild to traverse into import paths.

evanw avatar Oct 17 '23 14:10 evanw

I think either adding a way to tell esbuild to ignore the warnings, or to allow shaking of imports in build mode would be very useful.

I can't bundle things in this case because it's a library that needs to let consumers of it tree-shake it. But without removing the side-effect-free imports, esbuild then outputs tons of warnings to end users as they run their bundler.

How hard would bringing the import analysis / removal portion from bundling into build be?

natew avatar Oct 17 '23 15:10 natew

tell esbuild to ignore the warnings

This is the way: https://esbuild.github.io/api/#log-override. Pass the log message name (in [...] at the end of the warning) to log override to adjust the log level.

evanw avatar Oct 17 '23 15:10 evanw