-
-
Notifications
You must be signed in to change notification settings - Fork 9.2k
fix: replace inactive harmony import specifiers with undefined to avoid global reference (#20151) #20276
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
fix: replace inactive harmony import specifiers with undefined to avoid global reference (#20151) #20276
Conversation
…id global reference (webpack#20151)
|
CodSpeed Performance ReportMerging #20276 will not alter performanceComparing Summary
|
evenstensberg
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work here. Good that you also added tests. Could you set up a repro repo with the bug in one branch and this fix in another? Thanks!
I have created a reproduction repository here: https://github.com/samarthsinh2660/issue-20151-repro It has two branches: bug: Reproduces the issue using the current webpack version. In dist/main.js, you can see the unused function still contains a reference to the untransformed i18n identifier. |
|
Hmm, I’d prefer to add variable decl to represent the import binding, rather than replacing those with undefined. I think this keeps the bundle more readable. Also feel free to improve the test case to ensure it have solved the issue. Thank you. |
…ad of inline undefined
Thanks for the feedback! I have updated the PR to use variable declarations for inactive imports instead of inline replacement. This keeps the code readable as suggested. I also improved the test case to verify that the var declaration is correctly added and the original identifier is preserved in the code. The reproduction repository (issue-20151-repro) has also been updated on the fix branch. |
|
@samarthsinh2660 Hi, I’ve created a PR to fix this—would you mind if I took it over? Thanks again for your help. |
Hi @hai-x , absolutely! I’m happy for you to take it over. Glad I could help with the initial reproduction and the logic. If you have a moment, I would also be very grateful if you could point out any mistakes I made or better patterns I should have used. I'm eager to learn and improve my contributions to Webpack! Thanks again! |
Summary*
Fixes Issue #20151: An unused function in ESM code incorrectly referencing a global variable.
When Webpack's
usedExportsoptimization marks a harmony import specifier as inactive (meaning it's not used), the code containing the reference is still preserved in the bundle (to be later removed by a minimizer). However, the import specifier itself was not being transformed, leaving an untransformed identifier that leaked as a global reference.This fix ensures that inactive harmony import specifiers are replaced with
undefinedinstead of being left untransformed, preventing global variable clashes and incorrect references.What kind of change does this PR introduce?
fix
Did you add tests for your changes?
Yes, added a regression test in
test/configCases/issues/issue-20151/that verifies unused harmony imports are properly transformed.Does this PR introduce a breaking change?
No
If relevant, what needs to be documented once your changes are merged or what have you already documented?
None