Skip to content

bundler: pass isNodeMode to __toESM when the importee is a data file - #46736

Closed
robobun wants to merge 3 commits into
mainfrom
robobun/10e92eff/data-file-default-node-mode
Closed

robobun wants to merge 3 commits into
mainfrom
robobun/10e92eff/data-file-default-node-mode

Conversation

@robobun

@robobun robobun commented Oct 24, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

  • In a bundle, import json from "./data.json" is 5 for { "__esModule": true, "default": 5 } when another file loads it with require() or import(). bun run and Node give the whole object.
  • Cause: that file gets the CommonJS form, and __toESM (src/runtime.js:58) applies the CommonJS __esModule rule to the data.

Fix

  • to_esm_flags (src/bundler/LinkerContext.rs) marks each ES import record of such a file with TO_ESM_NODE_MODE. The printer prints __toESM(require_data(), 1), so default is the whole value.
  • Data loaders: json, jsonc, json5, toml, yaml. Other loaders print the same bytes.
  • Verified: test/bundler/bundler_loader.test.ts (33 new cases, all fail on main) and all of test/bundler/.
  • Self-reviewed: 30 concerns raised, 28 addressed (Notes).

Background

  • __toESM(mod, isNodeMode) builds the namespace object of a CommonJS module. Without isNodeMode, a truthy mod.__esModule makes default be mod.default.
  • Until now isNodeMode came from the importer: the output format, then its syntax (1.3.1), then its module type (1.4.1, bundler: pick the __toESM interop from the importer's module type #41150). Each rule left some importers at 5.
  • Considered binding the default import to require_data(), without __toESM: a separate output-size change.

Downsides

  • This differs from esbuild and rolldown on purpose: both give 5. No issue reports it.
  • One shape now differs from bun run (5): an import of a file that is only module.exports = require("./data.json") (Notes).
  • Each changed call is 3 bytes longer. A bundle that also has CommonJS interop makes 2 WeakMaps, not 1. Release text: +1,536 bytes.
Notes

Repro

// data.json: { "__esModule": true, "default": 5, "x": 1 }
// entry.js
import json from "./data.json";
console.log(JSON.stringify(json), json === require("./data.json"));
bun build entry.js --outfile=out.js && bun out.js
main:     5 false                                      var import_data = __toESM(require_data());
this PR:  {"__esModule":true,"default":5,"x":1} true   var import_data = __toESM(require_data(), 1);

bun entry.js prints the whole object. Node v26.3.0 prints the whole object for the import, require() and import() (with with { type: "json" }). webpack 5.112 prints the whole object. esbuild 0.21.5 and 0.28.2 print 5. rolldown 1.2.15 prints 5 for the require() shape.

No issue reports this. It was found by a read of the code after #41150. The trigger is a data file with a truthy __esModule key and a default key, which is rare.

History, by importer shape

Value of the default import of that data.json, from the released binaries. "whole" is the whole object.

Shape 1.2.0, 1.3.0 1.3.13, 1.4.0 1.4.1, 1.4.2, main This PR
.js importer with ES syntax only, plus a require() whole whole 5 whole
The same with --format=cjs 5 whole 5 whole
Importer with import and module.exports whole 5 5 whole
Non-split import() from a .cjs importer whole 5 5 whole
File that is only module.exports = require("./data.json"), ES module output whole whole 5 whole
.mjs importer whole whole whole whole

Rows 1, 2 and 5 gave the whole object in 1.4.0 and give 5 from 1.4.1 (#41150). Rows 3 and 4 have given 5 since 1.3.1 (#23803). With this PR the value does not depend on the importer.

What the rule covers

  • Covered: every ES import of a file that one of the five data loaders parsed, when that file has the CommonJS form. That is a default import, a named import, import * as, export ... from, export *, a non-split import(), and an import() of the file's own chunk. The loader is the one the file was parsed with, so with { type: "json" } and --loader .x:json are covered.
  • Covered, and different from bun run: in ES module output a file that is only module.exports = require("./data.json") is linked as the data file. Main already gives the whole object for an import of it when nothing else requires the data file. With --format=cjs, or with a second statement in the file, it is a CommonJS module and keeps 5.
  • Not changed: what an onLoad plugin returns. A result with loader: "object" is the exports of a module, and bun run gives exports.default for it. It reaches the linker as JSON, so the linker cannot tell it from JSON text that a plugin returns. That text keeps 5 too. Bun.build: bind default import to exports.default for loader: "object" plugins #36401 is open on the loader: "object" contract.
  • Not changed: a require() that the parser turns into an import for a package on the CommonJS unwrap list. That is require("react/data.json"), and module.exports = require("./data.json") in a file of such a package. require() of that file gives 5 for .default, as Node does. bundler: skip __toESM for an unwrapped require() of a package that keeps its CommonJS wrapper #46215 is open on the value of these calls.
  • Not changed: the xml loader (a document has one root element, so its value has one key), a .node addon, a CSS module, an HTML import, sqlite, and the string loaders (text, file, md, wasm, base64, dataurl). A string gives the same namespace in both modes. 10 probes (an empty .html file, two server HTML imports, a CSS module, a .node file, a text file, an XML file and an empty .cjs file, each with a require() or an import()) are byte-identical to main.
  • Not changed: import json from "./data.json" left external under --format=cjs still prints __toESM(require("./data.json")). With isNodeMode there, a plain { "a": 1 } reads as { "a": 1, "default": { "a": 1 } } under bun when the file was imported as an ES module first, because require() then returns the module namespace (Share one value between import and require() of a data file, per loader #35973). The dev server format is not changed.
  • Still different between the two forms of a data file: views other than default, for example Object.keys(ns) and ns.__esModule of import * as ns.

Question for a maintainer: is a data file a value in every bundle shape, and should that include the one-statement module.exports = require() file? If that file must keep 5, the record that the redirect points at the data file needs a marker.

Measurements

Release builds with LTO, Linux x64: main 179664e against this PR at 840dd23. The commit after it edits comments and splits one condition into two locals.

What main This PR
Outputs with no ES import of a CommonJS-form data file (5,000 ES modules, import-only JSON, require-only JSON, 2,000 CommonJS modules, each plain, --minify and --format=cjs) 12 of 12 byte-identical (cmp)
import json from "./data.json" plus require("./data.json") (wc -c) 1,613 bytes, 782 minified 1,616 bytes, 784 minified
500 data files, each imported and required from a .js file 193,397 bytes 194,897 bytes (+3 per call)
new WeakMap at start-up, bundle with a default import of a CommonJS module and an import of a data file 1 2
defineProperty and bind calls at start-up for that repro 3 and 3 3 and 2
bun build instructions:u, 2,000 CommonJS modules (median of 9, interleaved) 440,609,185 440,684,260 (+0.02%, two runs of main differ by 0.04%)
bun build instructions:u, 5,000 ES modules 397,526,592 398,202,204 (+0.17%, spread 0.9%)
bun build instructions:u, 500 data files imported and required 60,874,931 60,709,754 (-0.3%, spread 2.7%)
Release binary text (size) 107,556,528 107,558,064 (+1,536)
ImportRecordFlags bits in use 19 of 32 20 of 32
Existing test expectation lines edited 0

Tests

  • bun bd test test/bundler/bundler_loader.test.ts: 95 pass. The 33 new cases fail on a build of main.
  • Debug build: bundler_cjs 62 pass, bundler_cjs2esm 113, bundler_plugin 68, esbuild/loader 10, bundler_npm 2, bundler_dynamic_import_dce 333.
  • Release build, all of test/bundler/ (108 files): 7,812 pass. Two results fail with and without this change in this container: bundler_bytecode_portable "encoder output is identical on every platform" (the same hash on both builds) and its libraries.js case.
  • test/internal/source-lints: 183 pass.

Self-review

The first shape of this change also bound a default-only import to require_data() with no __toESM call, and printed ({ default: require_data() }) for an import() that reads only default. The review said to cut it to the record flag, and this PR is that cut. What the review found in the larger shape:

  • Its test for "data file" was the lazy-export flag, which an empty .html file, the server HTML import, a CSS module and an empty .js file also have. Without the __toESM import, a bundle of an empty .html import threw ReferenceError: __commonJS is not defined, and a server HTML import gave two different outputs across builds. The test is now the loader, with a match that lists every loader.
  • The history above: the value was 5 for some importers before 1.4.1.
  • The loader: "object" and module.exports = require() shapes, which the first body did not name.

Not done, as a follow-up if it is wanted: skip the __toESM call where an importer reads only default. Measured on that shape: the repro goes from 1,613 to 445 bytes, and a default import plus require() of a 10,000-key JSON goes from 10,001 defineProperty calls at start-up to 0. The 2 concerns not addressed here ask for a wider key for that skip.

Related open PRs that touch the same lines: #46215 and #46386 (the unwrap-list require()), #41953 and #46294 (print_to_esm_suffix). Bit of TO_ESM_NODE_MODE in ImportRecordFlags: 12 open PRs add a flag at 1 << 19 (2026-10-26), and one of them lands first and keeps that bit. This PR takes 1 << 25 at its next push. Check first that main and no other open PR use that bit by then. Two different flags with one bit compile with no error and no warning, and each then reads as the other.

A JSON, JSONC, JSON5, TOML, YAML or XML file that something loads with
require() or a non-split import() gets the CommonJS form. Each ES import
of it goes through __toESM, which applies the __esModule interop of
CommonJS modules to the data: a default import of
{ "__esModule": true, "default": 5 } was 5.

Such a file is a value, not a CommonJS module. The linker marks each ES
import record of it, and the printer passes isNodeMode, so default is the
whole value whatever the importer is.
@robobun

robobun commented Oct 24, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 9:08 AM PT - Oct 24th, 2026

✅ @robobun, your commit 4b74b07e9a1fdff27f5453ecaeef560cc24d9e14 passed in Build #132014! 🎉


🧪   To try this PR locally:

bunx bun-pr 46736

That installs a local version of the PR into your bun-46736 executable, so you can run:

bun-46736 --bun

@robobun

robobun commented Oct 24, 2026 •

Copy link
Copy Markdown
Collaborator Author

Status: ready for review. CI passed on the head commit 4b74b07 (build 132014, 181 of 181 jobs).

Reproduced on main (5c6d4e2) and on the 1.4.1 and 1.4.2 releases:

// data.json: { "__esModule": true, "default": 5, "x": 1 }
// entry.js
import json from "./data.json";
console.log(JSON.stringify(json), json === require("./data.json"));

bun build entry.js --outfile=out.js && bun out.js prints 5 false. With this change it prints {"__esModule":true,"default":5,"x":1} true.

The 33 new cases in test/bundler/bundler_loader.test.ts fail on a build of main and pass with this change.

@coderabbitai

coderabbitai Bot commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 179664e and e3adc5d.

Files selected for processing: 6
  • src/ast/import_record.rs
  • src/bundler/LinkerContext.rs
  • src/bundler/linker_context/README.md
  • src/bundler/linker_context/scanImportsAndExports.rs
  • src/js_printer/lib.rs
  • test/bundler/bundler_loader.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 0 remain after this review.

Walkthrough

The linker now marks eligible CommonJS data-file imports for Node-mode interop. The printer uses the import record to emit the corresponding __toESM argument. Bundler tests cover JSON, JSONC, JSON5, TOML, and YAML imports across multiple import forms and output configurations.

Changes

Data-file interop

Layer / File(s) Summary
Classify data files and assign import flags
src/ast/import_record.rs, src/bundler/LinkerContext.rs, src/bundler/linker_context/scanImportsAndExports.rs, src/bundler/linker_context/README.md
The linker identifies CommonJS-exported JSON, JSONC, JSON5, TOML, YAML, and XML files. It assigns TO_ESM_NODE_MODE to applicable imports, except imports converted from require().
Use import flags when printing interop
src/js_printer/lib.rs
The printer checks the import record when deciding whether to emit the Node-mode argument to __toESM. Internal import and require paths now pass the import record to the suffix printer.
Validate data-file import behavior
test/bundler/bundler_loader.test.ts
Tests cover default values and identity across imports, requires, re-exports, chunks, output formats, and dynamic imports for data-file fixtures.

Suggested reviewers: jarred-sumner, dylan-conway, alii

Priority: Normal

Merge Risk: Minimal · up to e3adc

No actionable merge-blocking issue was established; the change is ready for normal checks.

Pre-merge checks: Passed 4
Check name Status Explanation
Title check Passed The title clearly and concisely describes the main change: passing isNodeMode to __toESM when the importee is a data file.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description states the problem, the fix, verification, and downsides, and gives reproduction steps, measurements, and tests. It fills the template's two sections, "What does this PR do?" (as Probl…
Full details: Description check

Explanation

The description states the problem, the fix, verification, and downsides, and gives reproduction steps, measurements, and tests. It fills the template's two sections, "What does this PR do?" (as Problem/Fix/Background) and "How did you verify your code works?" (as Verified/Tests). The description is thorough and specific, and it matches the code changes in the raw summary, which cover the new TO_ESM_NODE_MODE flag, is_commonjs_data_file, to_esm_flags, print_to_esm_suffix, and the new tests.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@claude claude Bot 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.

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

Beyond the inline findings, I also checked the print_to_esm_suffix signature change — all four printer call sites (including the .then((m)=>__toESM(m.default path) still emit ,1 for an ESM-typed importer because the old input_module_type == Esm condition is OR'd into to_esm_is_node_mode, and the exports_kind reads in to_esm_flags during step 6 come after the only writes to that column (step 1, scanImportsAndExports.rs:207-383), so there is no torn read.

Extended reasoning...

The change adds a TO_ESM_NODE_MODE import-record flag set by the linker for ES imports of CommonJS-form data files and consumed by the printer to emit __toESM(x, 1); it touches the bundler linker and JS printer only, with no security-sensitive surface. Four inline findings remain (two intentional behavioral divergences from bun run that need a maintainer call, an untested Xml loader arm, and the unchanged external-CJS sibling site), so a human review is needed.

Comment thread src/bundler/LinkerContext.rs Resolved
Comment thread test/bundler/bundler_loader.test.ts Resolved
…ata file rule

A file of a lifted CommonJS package that is module.exports = require(x)
prints that statement through the record of an export star. With
isNodeMode, require() of that file gave the whole data as .default, where
Node gives the default key of the data. A statement of a lifted CommonJS
file was a require(), so it keeps the namespace it has.

What an onLoad plugin returns is not changed: a result with
loader: "object" is the exports of a module and reaches the linker as
JSON.

An XML value has one key, so it cannot have both __esModule and default.
The xml loader is not in the rule.
Comment thread src/ast/import_record.rs Resolved
Comment thread src/bundler/LinkerContext.rs Resolved
Comment thread src/bundler/LinkerContext.rs Resolved
Comment thread src/bundler/LinkerContext.rs Resolved
Comment thread src/bundler/LinkerContext.rs Resolved
Comment thread src/bundler/LinkerContext.rs Resolved
Comment thread src/js_printer/lib.rs Resolved

@claude claude Bot 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.

Code review completed

Nothing new to post: everything this review found is already covered by existing comments on this pull request or didn't merit a separate one.

@robobun

robobun commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator Author

Closing as stale: this PR conflicts with main, and its last commit is from 2026-10-24.

The branch stays, so the work is not lost. To pick it up again, rebase the branch on main and reopen this PR.

@robobun robobun closed this Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant