Skip to content

fix: preserve createRequire().resolve with literal import.meta.url when requireResolve is disabled#14491

Open
JSerFeng wants to merge 1 commit into
mainfrom
fy/clever-sammet-433c80
Open

fix: preserve createRequire().resolve with literal import.meta.url when requireResolve is disabled#14491
JSerFeng wants to merge 1 commit into
mainfrom
fy/clever-sammet-433c80

Conversation

@JSerFeng

@JSerFeng JSerFeng commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Summary

When module.parser.javascript.requireResolve is false, a created require's .resolve(...) is preserved to run at runtime instead of being rewritten to a module id.

The created require is treated as a real require object whenever it is used as one — decided per declaration:

  • .resolve(...) (incl. the 2-arg require.resolve(request, options) form), any other member (r.main, r.resolve.paths(...), ...), or used as a bare value (helper(r), export { r }, return r, ...) → the declaration is kept and rendered via rspack's injected __rspack_createRequire helper, and those accesses hit the real require. (webpack instead rewrites value-use to a context-require + "Critical dependency" warning, and non-cache/resolve members to undefined + "unsupported" warning — this is the improvement.)
  • A plain invoke r("./x") is still bundled (__webpack_require__), and r.cache still maps to the bundler module cache.
  • So a created require used only as bundled invokes (or unused) is cleared to /* createRequire() */ undefined — no dead import.meta.url leaks and CommonJS output stays valid.

The keep/clear decision is deferred to the parser's finish() (the callee is left un-walked so a clear doesn't collide with an import-specifier rewrite; the kept case re-renders the callee as __rspack_createRequire). The kept literal import.meta.url honors a customized output.importMetaName.

Since literal import.meta.url is only valid in ESM output, an Unsupported feature warning is emitted when a kept createRequire(import.meta.url) would land in a CommonJS bundle.

const r = createRequire(import.meta.url);
r("./x");               // bundled (__webpack_require__)
r.resolve("./x");       // preserved, runtime-resolved
r.resolve.paths("./x"); // real Node API (was unsupported/undefined)
export const x = r;     // real created require (was a context-require + warning)

Tests

  • esmOutputCases/create-require/import-meta-url-resolve-disabled — ESM keep via helper, variable + inline forms, 2-arg resolve, extra-arg side effect; snapshot.
  • configCases/require/create-require-kept-for-access — value-position use + non-resolve member access keep the real created require (no context-require/unsupported rewrite, no warning); invoke still bundled.
  • configCases/require/create-require-no-resolve-no-leak — used only as invokes → cleared to undefined, valid CJS, no warning.
  • configCases/require/create-require-resolve-disabled-cjs — CommonJS warning + artifact.
  • configCases/require/create-require-resolve-custom-import-meta-name — kept argument honors output.importMetaName.
  • configCases/require/module-require-disable-resolve — dynamic-argument dependency preservation.

Related links

Checklist

  • Tests updated (or not required).
  • Documentation updated (or not required).

Copilot AI review requested due to automatic review settings June 18, 2026 08:45

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes createRequire(import.meta.url).resolve(...) semantics when module.parser.javascript.requireResolve is disabled by preserving the literal import.meta.url (instead of baking it to a build-time file:// URL), so runtime require.resolve behaves relative to the emitted output module rather than the source path. It also adds a targeted warning when this preserved literal would land in CommonJS output (where import.meta is a runtime syntax error).

Changes:

  • Preserve createRequire(...) arguments verbatim when requireResolve: false, avoiding build-time file:// baking for cases like import.meta.url.
  • Emit an Unsupported feature warning when preserved createRequire(import.meta.url) would be emitted into a CJS bundle.
  • Add/adjust test coverage: new ESM snapshot case for runtime-preserved .resolve, plus a CJS warning errorCase; simplify the older configCase accordingly.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated no comments.

Show a summary per file
File Description
crates/rspack_plugin_javascript/src/parser_plugin/common_js_imports_parse_plugin.rs Changes createRequire argument replacement behavior under requireResolve: false and adds a CJS-only warning for preserved import.meta.url.
tests/rspack-test/esmOutputCases/create-require/import-meta-url-resolve-disabled/rspack.config.js Enables createRequire and disables requireResolve for the new ESM output test case.
tests/rspack-test/esmOutputCases/create-require/import-meta-url-resolve-disabled/index.js Verifies literal import.meta.url is preserved and .resolve remains runtime-resolved while require() is still bundled.
tests/rspack-test/esmOutputCases/create-require/import-meta-url-resolve-disabled/dep.js Simple dependency used to confirm require() calls are still bundled.
tests/rspack-test/esmOutputCases/create-require/import-meta-url-resolve-disabled/snapshots/esm.snap.txt Snapshot asserting the emitted ESM output contains preserved createRequire(import.meta.url) and runtime .resolve(...).
tests/rspack-test/errorCases/create-require-resolve-disabled-cjs.js New errorCase asserting the CJS-output warning is emitted (and no errors).
tests/rspack-test/fixtures/errors/create-require-resolve-disabled-cjs.js Fixture that triggers preserved createRequire(import.meta.url) under CJS output to validate warning behavior.
tests/rspack-test/configCases/require/module-require-disable-resolve/index.js Refocuses the existing configCase on dependency/side-effect preservation for non-statically-analyzable arguments.
tests/rspack-test/configCases/require/module-require-disable-resolve/a.js Removed (no longer needed by the refocused configCase).
tests/rspack-test/configCases/require/module-require-disable-resolve/foo/a.js Removed (no longer needed by the refocused configCase).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f1ef5a3b0d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@github-actions

github-actions Bot commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

📦 Binary Size-limit

Comparing ae6d86c to chore: release v2.1.0 (#14584) by Gengkun

❌ Size increased by 12.00KB from 67.82MB to 67.84MB (⬆️0.02%)

@codspeed-hq

codspeed-hq Bot commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 41 untouched benchmarks
⏩ 47 skipped benchmarks1


Comparing fy/clever-sammet-433c80 (ae6d86c) with main (2343c7e)

Open in CodSpeed

Footnotes

  1. 47 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@JSerFeng JSerFeng force-pushed the fy/clever-sammet-433c80 branch 2 times, most recently from d502c34 to b9f0132 Compare June 18, 2026 16:47

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b9f0132f93

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@github-actions

github-actions Bot commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Rsdoctor Bundle Diff Analysis

Found 5 projects in monorepo, 5 projects with changes.

📊 Quick Summary
Project Total Size Gzip Size Change Gzip Change
popular-libs 1.7 MB 551.5 KB - -
react-10k 5.6 MB 1.3 MB - -
react-1k 822.8 KB 218.3 KB - -
ui-components 4.9 MB 1.4 MB - -
react-5k 2.7 MB 669.1 KB - -
📋 Detailed Reports (Click to expand)

📁 popular-libs

Path: ../build-tools-performance/cases/popular-libs/dist/rsdoctor-data.json

⚠️ No baseline data found - Unable to perform comparison analysis

Metric Current Baseline Change
📊 Total Size 1.7 MB - -
🗜️ Gzip Size 551.5 KB - -
📄 JavaScript 1.7 MB - -
🎨 CSS 0 B - -
🌐 HTML 289.0 B - -
📁 Other Assets 0 B - -

📁 react-10k

Path: ../build-tools-performance/cases/react-10k/dist/rsdoctor-data.json

⚠️ No baseline data found - Unable to perform comparison analysis

Metric Current Baseline Change
📊 Total Size 5.6 MB - -
🗜️ Gzip Size 1.3 MB - -
📄 JavaScript 5.6 MB - -
🎨 CSS 21.0 B - -
🌐 HTML 328.0 B - -
📁 Other Assets 0 B - -

📁 react-1k

Path: ../build-tools-performance/cases/react-1k/dist/rsdoctor-data.json

⚠️ No baseline data found - Unable to perform comparison analysis

Metric Current Baseline Change
📊 Total Size 822.8 KB - -
🗜️ Gzip Size 218.3 KB - -
📄 JavaScript 822.4 KB - -
🎨 CSS 0 B - -
🌐 HTML 328.0 B - -
📁 Other Assets 0 B - -

📁 ui-components

Path: ../build-tools-performance/cases/ui-components/dist/rsdoctor-data.json

⚠️ No baseline data found - Unable to perform comparison analysis

Metric Current Baseline Change
📊 Total Size 4.9 MB - -
🗜️ Gzip Size 1.4 MB - -
📄 JavaScript 4.8 MB - -
🎨 CSS 111.9 KB - -
🌐 HTML 328.0 B - -
📁 Other Assets 0 B - -

📁 react-5k

Path: ../build-tools-performance/cases/react-5k/dist/rsdoctor-data.json

⚠️ No baseline data found - Unable to perform comparison analysis

Metric Current Baseline Change
📊 Total Size 2.7 MB - -
🗜️ Gzip Size 669.1 KB - -
📄 JavaScript 2.7 MB - -
🎨 CSS 21.0 B - -
🌐 HTML 328.0 B - -
📁 Other Assets 0 B - -

Generated by Rsdoctor GitHub Action

@github-actions

github-actions Bot commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

📝 Rspack Ecosystem CI: Open

suite result
rspress ✅ success
modernjs ✅ success
devserver ✅ success
lynx-stack ✅ success
rslib ❌ failure
rsdoctor ✅ success
plugin ❌ failure
rsbuild-rsc-plugin ✅ success
rstest ❌ failure
rsbuild ❌ failure
examples ✅ success

@JSerFeng

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b9f0132f93

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@JSerFeng JSerFeng force-pushed the fy/clever-sammet-433c80 branch 2 times, most recently from d2a55d9 to 950a0f6 Compare June 22, 2026 08:41

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 950a0f616c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@JSerFeng JSerFeng force-pushed the fy/clever-sammet-433c80 branch from 950a0f6 to d8e910c Compare June 23, 2026 03:26

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d8e910c2e4

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@JSerFeng JSerFeng force-pushed the fy/clever-sammet-433c80 branch 2 times, most recently from a472962 to a9aae53 Compare June 24, 2026 08:47

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a9aae53321

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@JSerFeng JSerFeng force-pushed the fy/clever-sammet-433c80 branch from a9aae53 to 1336a8a Compare June 25, 2026 06:42

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1336a8aaa4

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@JSerFeng JSerFeng force-pushed the fy/clever-sammet-433c80 branch from 1336a8a to 6182710 Compare June 25, 2026 06:59

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6182710f0d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@JSerFeng JSerFeng force-pushed the fy/clever-sammet-433c80 branch 2 times, most recently from 5826e44 to 7552125 Compare June 25, 2026 07:23

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7552125e52

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@JSerFeng JSerFeng force-pushed the fy/clever-sammet-433c80 branch from 7552125 to 69b21ce Compare June 25, 2026 11:18

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 69b21ce9c5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@JSerFeng JSerFeng force-pushed the fy/clever-sammet-433c80 branch from 69b21ce to e888c24 Compare June 26, 2026 03:11

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e888c2444b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@JSerFeng JSerFeng force-pushed the fy/clever-sammet-433c80 branch from e888c24 to 1ce3cff Compare June 26, 2026 06:13

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1ce3cff97d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@JSerFeng JSerFeng force-pushed the fy/clever-sammet-433c80 branch from 1ce3cff to 2fc1e3c Compare June 26, 2026 06:35

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2fc1e3cdb0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@JSerFeng JSerFeng force-pushed the fy/clever-sammet-433c80 branch from 2fc1e3c to cfefb56 Compare June 26, 2026 07:35

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: cfefb564d6

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@JSerFeng JSerFeng force-pushed the fy/clever-sammet-433c80 branch from cfefb56 to b6f2e5e Compare June 26, 2026 08:05

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b6f2e5eb50

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

…en requireResolve is disabled

When `module.parser.javascript.requireResolve` is `false`, a created require's
`.resolve(...)` is preserved to run at runtime instead of being rewritten to a
module id.

A created require that is used as a real require object — `.resolve(...)`, any
other member (`r.main`, `r.resolve.paths(...)`, ...), or as a bare value
(`helper(r)`, `export { r }`, ...) — is kept and rendered via rspack's injected
`__rspack_createRequire` helper, so the literal `import.meta.url` resolves
relative to the runtime output module and those accesses hit the real require
(instead of a context-require / unsupported-member rewrite). A plain invoke
`r("./x")` is still bundled and `r.cache` still maps to the module cache, so a
created require used only that way is cleared to `undefined` — no dead
`import.meta.url` leaks and CommonJS output stays valid. The keep/clear decision
is deferred to `finish()`.

Since literal `import.meta.url` is only valid in ESM output, an `Unsupported
feature` warning is emitted when a kept `createRequire(import.meta.url)` would
land in a CommonJS bundle.
@JSerFeng JSerFeng force-pushed the fy/clever-sammet-433c80 branch from b6f2e5e to ae6d86c Compare June 26, 2026 08:23
@JSerFeng

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: ae6d86cfab

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants