Skip to content

fix(json, text, bytes modules): keep both default and namespace imports - #18648

Merged
JLHwung merged 1 commit into
mainfrom
jlhwung/fix-x-modules-default-and-namespace
Nov 19, 2026
Merged

JLHwung merged 1 commit into
mainfrom
jlhwung/fix-x-modules-default-and-namespace

Conversation

@JLHwung

@JLHwung JLHwung commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor
Q                       A
Fixed Issues? proposal-x-modules remove the default binding when transforming import value, * as ns from "./x.json" with { type: "json" }
Patch: Bug Fix? Yes
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
Need backport? 7.x
License MIT

In this PR we fix an issue reported by Claude Fable 5.2: Given the input

import value, * as ns from "./x.json" with { type: "json" };
use(value, ns);

The JSON module transform generates the output

const ns = {
  default: JSON.parse(_readFileSync(new URL(import.meta.resolve("./x.json"))))
};
use(value, ns);   // value is never declared

which will compile successfully but will throw a runtime error: "value is never declared".

The fix is to record the default and namespace identifier separately and add const ns = { default: value } right after the injected declaration.

The REPL reproduction is not available due to #18643.

Assisted-by: Claude Fable 5.2

@JLHwung
JLHwung added this pull request to stack #18587 November 17, 2026 15:19
@JLHwung JLHwung added the PR: Bug Fix 🐛 A type of pull request used for our changelog categories label Nov 17, 2026
@babel-bot

babel-bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/63411

@pkg-pr-new

pkg-pr-new Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

commit: 46d6a37

@liuxingbaoyu

liuxingbaoyu commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

The REPL is actually working, it’s just not displaying correctly. :)

@JLHwung

JLHwung commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

The REPL is actually working, it’s just not displaying correctly. :)

OK, to clarify, what I mean here is that I can not reproduce this issue on REPL using proposal-bytes-module following these steps:

Expected: REPL should load proposal-bytes-module
Actual: It throws an error and fail to load the plugin, because as an official babel plugin, it is not included in the standalone build.

Base automatically changed from jlhwung/fix-regenerator-using-declaration to main November 17, 2026 22:48
`import value, * as ns from "./x.json" with { type: "json" }` only
declared the last specifier, so `value` was left as a reference to an
undeclared variable. Both bindings are now declared.

Assisted-by: Claude Fable 5.2
@JLHwung
JLHwung force-pushed the jlhwung/fix-x-modules-default-and-namespace branch from 8c7e02b to 46d6a37 Compare November 17, 2026 22:48
@JLHwung
JLHwung added this pull request to the merge queue Nov 19, 2026
Merged via the queue into main with commit 7314a28 Nov 19, 2026
46 checks passed
@JLHwung
JLHwung deleted the jlhwung/fix-x-modules-default-and-namespace branch November 19, 2026 23:16
@babel-bot

Copy link
Copy Markdown
Collaborator

Backported to 7.x in #18668.

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

Labels

PR: Bug Fix 🐛 A type of pull request used for our changelog categories

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants