fix: remove unnecessary __webpack_require__ in ESM library output#21032
Conversation
🦋 Changeset detectedLatest commit: bd64378 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
|
|
This PR is packaged and the instant preview is available (cc4035b). Install it locally:
npm i -D webpack@https://pkg.pr.new/webpack@cc4035b
yarn add -D webpack@https://pkg.pr.new/webpack@cc4035b
pnpm add -D webpack@https://pkg.pr.new/webpack@cc4035b |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #21032 +/- ##
==========================================
- Coverage 91.58% 91.57% -0.02%
==========================================
Files 573 573
Lines 59541 59555 +14
Branches 16077 16084 +7
==========================================
+ Hits 54532 54536 +4
- Misses 5009 5019 +10
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
When a chunk contains non-JS modules (CSS, assets) alongside a concatenated ESM entry, the runtime cleanup hook in ModuleLibraryPlugin bailed out because getNumberOfChunkModules > 1, leaving unused __webpack_require__.d and __webpack_require__.o helpers in the output. - HarmonyExportInitFragment: only add definePropertyGetters requirement when exportMap is non-empty - ModuleLibraryPlugin: replace coarse getNumberOfChunkModules check with per-module requirement inspection Closes #21029
5dd30f5 to
3bc5646
Compare
Merging this PR will not alter performance
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing Footnotes
|
Summary
When an ESM library chunk (
output.library.type = "module") contains non-JS modules (CSS viaexperiments.css,MiniCssExtractPlugin, asset modules, etc.) alongside a concatenated entry,__webpack_require__runtime helpers (var __webpack_require__ = {},__webpack_require__.d,__webpack_require__.o) are emitted even though they are never referenced in the output.Root cause:
ModuleLibraryPlugin's cleanup hook bailed out whengetNumberOfChunkModules(chunk) !== 1, but CSS/asset modules increase the count without actually needing those JS helpers.Two fixes:
HarmonyExportInitFragment.getContent()— only adddefinePropertyGettersandexportsruntime requirements whenexportMapis non-empty. Previously these were added unconditionally even when the fragment produced no output.ModuleLibraryPlugincleanup hook — replace the coarsegetNumberOfChunkModules !== 1guard with a per-module check: iterate chunk modules and only retain helpers that another module actually requires.Fixes #21029
What kind of change does this PR introduce?
fix
Did you add tests for your changes?
Yes. Added
entry4.js+style.cssto the existingtest/configCases/library/module-useless-export-requirement/test case, covering the ESM library + CSS module combination. Updated 4 HTML snapshot files that previously contained the unnecessary helpers.Does this PR introduce a breaking change?
No. This only removes dead runtime code from the output.
If relevant, what needs to be documented once your changes are merged or what have you already documented?
n/a
Use of AI
Claude Code was used to investigate the root cause, draft the implementation, and write the test case, under human review.