fix: compatibility __non_webpack_require__ with es modules#12052
fix: compatibility __non_webpack_require__ with es modules#12052LingyuCoder merged 2 commits intomainfrom
__non_webpack_require__ with es modules#12052Conversation
✅ Deploy Preview for rspack canceled.
|
__non_webpack_require__ with es modules
There was a problem hiding this comment.
Pull Request Overview
This PR optimizes the createRequire usage in ESM output by creating a single reusable variable instead of calling __WEBPACK_EXTERNAL_createRequire(import.meta.url) repeatedly. The change introduces __WEBPACK_EXTERNAL_createRequire_require as a cached result of the createRequire() call, which is then reused throughout the module.
Key changes:
- Introduces
__WEBPACK_EXTERNAL_createRequire_requirevariable to cache the result ofcreateRequire(import.meta.url) - Updates all usage sites to reference the cached variable instead of calling
createRequire()repeatedly - Ensures concatenated modules properly propagate the
need_create_requireflag
Reviewed Changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| crates/rspack_plugin_javascript/src/plugin/api_plugin.rs | Generates the cached createRequire variable with environment-aware const/var keyword |
| crates/rspack_plugin_javascript/src/parser_plugin/api_plugin.rs | Updates non-webpack require references to use the cached variable |
| crates/rspack_core/src/external_module.rs | Updates external module generation to create and use the cached variable |
| crates/rspack_core/src/concatenated_module.rs | Propagates need_create_require flag from nested modules |
| tests/rspack-test/configCases/module/issue-17971/test.filter.js | Enables previously disabled test (fixed by this change) |
| tests/rspack-test/configCases/module/issue-19767/test.filter.js | Removes test filter (test now passes) |
| tests/rspack-test/configCases/module/entries-runtime-false/test.filter.js | Removes test filter (test now passes) |
| tests/rspack-test/configCases/module/dependOn-runtime-false/test.filter.js | Removes test filter (test now passes) |
| tests/rspack-test/esmOutputCases/dynamic-import/magic-comment/snapshots/esm.snap.txt | Updates snapshot to reflect new cached variable usage |
| tests/rspack-test/configCases/library/modern-module-force-concaten/snapshot/f.js.txt | Updates snapshot to reflect new cached variable usage |
| tests/rspack-test/configCases/library/esm-external/index.js | Updates test expectations to match new variable name |
| tests/rspack-test/configCases/externals/module-import/index.js | Updates test expectations to match new variable name |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
📦 Binary Size-limit
❌ Size increased by 256bytes from 47.84MB to 47.84MB (⬆️0.00%) |
CodSpeed Performance ReportMerging #12052 will not alter performanceComparing Summary
|
Summary
port webpack/webpack#17308 from webpack
Related links
Checklist