-
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
Support tree-shaking unused properties on an object literal #2855
Comments
Are you saying if you do |
There will be cases where analyzing the code would be complicated enough, if not impossible, but I'd argue it would still be useful to implement this, if there's a reasonable mechanism for opting into this that doesn't break the world. Maybe somebody should just make a userland plugin for this. In you example specifically if |
If you only supported a strict form for this feature, where For example, if I built the namespace like this: lib/public.ts import foo from './foo';
import bar from './bar';
import baz from './baz';
export {foo, bar, baz};
// export * as qux from "./qux"; // [1] and maybe also if the file only contains imports/exports? lib/index.ts export * as Lib from "./public"; main.ts import { Lib } from "./lib";
Lib.foo();
Lib.baz();
// Lib.qux.quux(); |
I don't think that would make this easier to implement as that's pretty much a completely separate feature from what is being requested here. The |
It'd be interesting if default exports could be tree-shaken too, perhaps on a global or per-module opt-in basis.
The problem is that, for example, I want the DX that the following allows, where I can then just do a super clean
import Lib from 'lib'
, rather than having to write* as
or having to import each little thing individually.But I want the tree-shaking that this allows:
Basically I think there should be some mechanism for treating simple trivially analyzable default exports as tree-shakeable. Maybe if an ESM package says explicitly that it has
"sideEffects": false
this optimization could be performed? Maybe some other flag or#PURE
-like annotation could be introduced?Alternatively the simplest workaround I can think about is to have two entry points of your library, one exports things individually and the other imports everything from that and exports them together as the default export, then you import the default export, but rewrite the code with a simple transform to actually import everything from the other entry point with
* as
. It should work, and it's simple enough to be practical as a one-off, but if I need to modify every library entry point and account for each of them with a transform it gets impractical pretty quick, it'd be nice if this Just Worked™️The text was updated successfully, but these errors were encountered: