-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
re-exporting generates unneeded code #3032
Comments
Simple rule: when you're capturing the namespace object (* as vec), do not pass it outside of "current file", in which case esbuild cannot analyze too deep to "de-construct" it. For each file, esbuild is smart enough to treat namespace references as named imports: import * as vec from './vec'
vec.add(3, 5)
// is converted to
import {add} from './vec'
add(3, 5) ..but not cross files. |
@hyrious thank you for the reply. does this mean that any kind of "flattening" files like this: https://github.com/toji/gl-matrix/blob/master/src/index.js will pretty much break treeshaking and cause hickups unless you rewrite code to avoid the flatten file completely? |
Yes but no -- And yes the whole module "captured" by the namespace object is always bundled in if been used. Note that you can export // lib.js
export * from './foo'
export * from './bar'
// main.js
import * as lib from './lib'
lib.foo() This trick is also been used in TypeScript's source code. |
The reason why esbuild does this is that the "module namespace exotic object" (the thing you are exporting with There's an existing request for tree shaking to be able to unwrap multiple levels of indirection like this: #1420. So I'm closing this issue as a duplicate of that issue. The workaround for now is to either not write code like this or to use another tool instead of esbuild. |
when using any kind of
export
of code that has been loaded viaimport
, some code that is in some cases unused will be generated. is this avoidable?example:
The generated bundle code is:
however, the code rollup will produce is:
Question: Is there any way I can rewrite my code to end up with the result i get from rollup without changing the file structure?
The text was updated successfully, but these errors were encountered: