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

Unify logic for pure and non-pure enums #5

Merged
merged 2 commits into from Mar 9, 2023

Conversation

nicolo-ribaudo
Copy link

Q                       A
Fixed Issues? Fixes #1, Fixes #2
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

This is similar to what I suggested in babel#15467 (comment): it unifies the code path for the "pure" and "non pure" versions, so that we avoid having two different but very similar cases.

Instead of always generating the variable declaration followed by the IIFE, which as you pointed out doesn't work, I took the opposite approach and always generate the declaration with the IIFE as the initializer. It has the unfortunate side effect that let X = (function () {})(X || {}) is not valid (because X is in TDZ), so we must instead generate let X = (function () {})({}) in this case.

@@ -3,7 +3,7 @@ var E = /*#__PURE__*/function (E) {
E[E["y"] = 2] = "y";
return E;
}(E || {});
E = /*#__PURE__*/function (E) {
E = function (E) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was not needed, terser doesn't remove used variables (E) anyway.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm inclined to keep it, maybe in the future terser can be implemented or other compressors can be used?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

(function (E) {})(E || (E = {}));
let E = /*#__PURE__*/function (E) {
return E;
}({});
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now terser can remove this :)

@liuxingbaoyu
Copy link
Owner

Thanks! It's all really good!

@liuxingbaoyu liuxingbaoyu merged commit 4679f40 into liuxingbaoyu:pure-enum Mar 9, 2023
12 checks passed
@nicolo-ribaudo nicolo-ribaudo deleted the pure-enum-pr branch March 9, 2023 16:13
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
2 participants