fix: walk define arguments in harmony modules (fixes #17063)#20542
fix: walk define arguments in harmony modules (fixes #17063)#20542aryanraj45 wants to merge 1 commit intowebpack:mainfrom
Conversation
|
|
Wrong solution, no |
|
yeah yeah totally Agree, Would it make sense to just walk the arguments without treating it as AMD at all? |
|
We should not just handle it |
|
okay how about we would a build warning like "define is not supported in EcmaScript modules" work? So webpack actively flags it instead of silently emitting broken output |
|
We should output nothing, define is just a function in ECMA modules, and we should not break code too |
7601dc9 to
ac46443
Compare
|
Thanks for the suggestion Alexander Since define behaves like a regular function in ESM, I’ve removed the Stage -1 hack and updated the logic to simply walk through its arguments so any import bindings inside it don’t break, and then return true to prevent the AMD plugin from processing it. |
| optimization: { | ||
| minimize: false | ||
| } | ||
| }; |
There was a problem hiding this comment.
Please avoid pushing not related things
There was a problem hiding this comment.
so so sorry actually webworker one was my another test for the upcoming PR by mistake it came under this branch and pushed in this commit .will surely take care of this in future also I have fixed it and kept only [test/cases/parsing/issue-17063/]
ac46443 to
8311efc
Compare
|
@alexander-akait can u please review once again whenever u have a moment |
|
Tests are failed, we need improve logic here |
I think the CI is failing because we're blocking the AMD plugin from processing define. Would it be okay if I just stop skipping define in ESM and let the AMD plugin handle it naturally? |
8311efc to
bd8eb04
Compare
|
I don't know, that is why it is a bug, it requires investigate |
|
I know we usually avoid define in ESM, but blocking it actually breaks about 25+ existing tests that seem to mix both. Letting the AMD plugin handle it naturally fixes the bug (#17063) and keeps the CI green (coz i have tested locally both 45/45 test passed and even the new test case which I added in my latest push). Can we trigger a re-run to check? Thanks! |
bd8eb04 to
e061c66
Compare
There was a problem hiding this comment.
Pull request overview
Fixes a Harmony (ESM) parsing edge case where define(...) calls were bailing out before their arguments were walked, preventing imported bindings used inside define() callbacks from being renamed and causing runtime ReferenceErrors.
Changes:
- Add a pre-bail (stage
-1)definecall hook inHarmonyDetectionParserPluginto walkexpression.argumentsin Harmony mode. - Add regression tests covering an ESM import referenced inside a
define()callback. - Add a configCase covering the same scenario in the configCases harness.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| lib/dependencies/HarmonyDetectionParserPlugin.js | Walks define() call arguments in Harmony mode before the existing “skip in harmony” bail-out runs. |
| test/cases/parsing/issue-17063/index.js | Regression test ensuring an imported binding used inside define() callback is correctly renamed. |
| test/cases/parsing/issue-17063/foo.js | Fixture module returning a value used by the regression test. |
| test/configCases/amd/define-harmony-import/index.js | ConfigCase reproduction asserting the imported default binding is usable inside define() callback. |
| test/configCases/amd/define-harmony-import/lib.js | Fixture default export used by the configCase. |
| test/configCases/amd/define-harmony-import/webpack.config.js | Minimal config for the new configCase. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| parser.hooks.call | ||
| .for("define") | ||
| .tap({ name: PLUGIN_NAME, stage: -1 }, (expression) => { | ||
| if (HarmonyExports.isEnabled(parser.state) && expression.arguments) { | ||
| parser.walkExpressions(expression.arguments); | ||
| } | ||
| }); |
There was a problem hiding this comment.
PR description says it also adds test/configCases/target/web-webworker/ and test/configCases/target/web-webworker-auto-public-path/, but those directories/files don't appear in the changes. Please either include those missing test additions or update the PR description so it matches what’s actually in the diff.
Fixes #17063
Summary
In harmony (ESM) mode,
HarmonyDetectionParserPluginblocks AMD processing by returningtruefrom askipInHarmonytap onhooks.call.for("define"). This bail-out happens before the parser walksexpression.arguments, so import bindings insidedefine()callbacks are never renamed causing aReferenceErrorat runtime.Example that was broken:
Webpack correctly renames foo everywhere else, but skips it here because skipInHarmony bails walkCallExpression before it reaches the arguments.
Fix: add a stage: -1 tap on hooks.call.for("define") that walks expression.arguments before skipInHarmony (stage 0) fires. This ensures bindings are renamed, while AMD processing is still correctly skipped in harmony mode.
What kind of change does this PR introduce?
fix
Did you add tests for your changes?
Yes. Added test/cases/parsing/issue-17063/ — an ESM file that imports a binding and calls it inside define(). Without the fix this throws ReferenceError. With the fix it passes.
Also added test/configCases/target/web-webworker/ and test/configCases/target/web-webworker-auto-public-path/ which were previously missing coverage for the ["web", "webworker"] target combination.
Does this PR introduce a breaking change?
No. The fix only activates when HarmonyExports.isEnabled() is true (ESM files). Plain CJS files are unaffected.
If relevant, what needs to be documented once your changes are merged?
No documentation needed. This is a silent runtime bug fix with no API or config changes.]