Skip to content
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

Statements generated from syntax lowering or keepNames block treeshaking #3965

Closed
bluwy opened this issue Nov 5, 2024 · 3 comments
Closed

Comments

@bluwy
Copy link

bluwy commented Nov 5, 2024

When using the transform API with syntax lowering (e.g. for classes) or with keepNames (--keep-names), the code generated often look something like this:

const _Foo = class _Foo {
};
__name(_Foo, "Foo");
__publicField(_Foo, "prop", "value");
export let Foo = _Foo;

Playground link

Because the __publicField and __name aren't marked as pure (/* @__PURE__ */), even if _Foo is unused, bundlers are not able to assume it's safe to treeshake it. Should esbuild be prepending the /* @__PURE__ */ comments to __publicField and __name?

The __publicField and __name statements are holding a value on _Foo, which prevents it from being treeshaken if the exports are unused in bundlers.


I noticed for function expressions and keepNames, e.g. with export const foo = () => {}, esbuild is already transforming it to export const _foo = /* @__PURE__ */ __name(() => {}, "foo"). This has allowed esbuild to treeshake it if unused.

Perhaps something similar can be done for non-expressions and for the lowering code.

@hyrious
Copy link

hyrious commented Nov 5, 2024

FYI, pure annotations don't work on statements. The only way to mark the Foo as pure in your case is:

export let Foo = /* @__PURE__ */ (() => {
	const _Foo = class _Foo {
	};
	__name(_Foo, "Foo");
	__publicField(_Foo, "prop", "value");
	return _Foo;
})();

@bluwy
Copy link
Author

bluwy commented Nov 5, 2024

Ah that pretty much makes this feature request moot then. I don't know if esbuild is up to transforming to that way to solve the treeshaking issue, or if there's some other solution to this.

@bluwy bluwy changed the title Add pure comment for expressions generated from syntax lowering or keepNames Statements generated from syntax lowering or keepNames block treeshaking Nov 5, 2024
@bluwy
Copy link
Author

bluwy commented Nov 28, 2024

Going to close this for now as the issue is kinda broad and is probably worth as a better feature request to resolve the issue generically.

@bluwy bluwy closed this as not planned Won't fix, can't repro, duplicate, stale Nov 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants