Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/ast/import_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ bitflags::bitflags! {
/// `import()` / `require()` whose value nothing reads: the linker bound
/// every name read off it to an export, so it evaluates to `{}`.
const NAMESPACE_UNUSED = 1 << 17;

/// A split `require()` whose target is CommonJS at link time: the
/// chunk's namespace is `{ default: module.exports }`, so the call
/// reads `.default` to return `module.exports`.
const CROSS_CHUNK_REQUIRE_DEFAULT = 1 << 18;
}
}

Expand Down
18 changes: 18 additions & 0 deletions src/bundler/linker_context/scanImportsAndExports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,24 @@ pub(crate) fn scan_imports_and_exports(
runtime_require_uses += 1;
}

// A split `require()` whose target is CommonJS at link
// time (for example a lifted `module.exports = require()`
// file the linker wrapped again): the chunk exports only
// `default: module.exports`, so the printed call reads
// `.default` to return `module.exports`, the same value
// an unsplit `require()` returns.
if kind == ImportKind::Require
&& is_external_dyn
&& rec_source_index.is_valid()
&& col_ref!(exports_kind)[rec_source_index.get() as usize]
== ExportsKind::Cjs
{
col!(import_records_list)[id].as_mut_slice()
[import_record_index as usize]
.flags
.insert(ImportRecordFlags::CROSS_CHUNK_REQUIRE_DEFAULT);
}

// If this wasn't originally a "require()" call, then we may need
// to wrap this in a call to the "__toESM" wrapper to convert from
// CommonJS semantics to ESM semantics.
Expand Down
9 changes: 9 additions & 0 deletions src/js_printer/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2961,6 +2961,15 @@ pub(crate) mod __gated_printer {
self.print_import_record_path(record);
self.print(b")");

// A split `require()` of a module that is CommonJS at link
// time: the chunk's only export is `default: module.exports`.
if record
.flags
.contains(ImportRecordFlags::CROSS_CHUNK_REQUIRE_DEFAULT)
{
self.print(b".default");
}

if wrap_with_to_esm {
self.print_to_esm_suffix();
}
Expand Down
68 changes: 68 additions & 0 deletions test/bundler/bundler_splitting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2395,6 +2395,74 @@ describe("bundler", () => {
run: { file: "/out/main.js", stdout: "cjs value called" },
});

// A split require() of a lifted `module.exports = require()` file that the
// linker wraps again because its target is CommonJS: the chunk's only
// export is `default: module.exports`, so the call reads `.default` and
// returns `module.exports`, not the chunk namespace.
// https://github.com/oven-sh/bun/issues/41236
itBundled("splitting/SplitRequireOfRewrappedLiftedCommonJS#41236", {
files: {
"/main.ts": /* ts */ `
let m;
try {
m = require("react-dom");
} catch {}
console.log(typeof m, m.version, m.default());
`,
"/node_modules/react-dom/index.js": /* js */ `
console.log('side effect');
module.exports = require('./impl');
`,
"/node_modules/react-dom/impl.js": /* js */ `
module.exports = function render() { return "rendered"; };
module.exports.version = "19.0.0";
`,
},
entryPoints: ["/main.ts"],
splitting: true,
target: "bun",
outdir: "/out",
format: "esm",
onAfterBundle(api) {
const chunk = chunkContaining(api, "side effect");
api.expectFile("/out/main.js").toContain(`import.meta.require("./${chunk}").default`);
},
run: { file: "/out/main.js", stdout: "side effect\nobject 19.0.0 rendered" },
});

// When the lift target is itself lifted, the chunk stays an ES module and
// the split require() returns its namespace with no `.default` read.
itBundled("splitting/SplitRequireOfLiftedCommonJSStaysEsm", {
files: {
"/main.ts": /* ts */ `
let m;
try {
m = require("react-dom");
} catch {}
console.log(m.version, m.render());
`,
"/node_modules/react-dom/index.js": /* js */ `
console.log('side effect');
module.exports = require('./impl');
`,
"/node_modules/react-dom/impl.js": /* js */ `
exports.render = function render() { return "rendered"; };
exports.version = "19.0.0";
`,
},
entryPoints: ["/main.ts"],
splitting: true,
target: "bun",
outdir: "/out",
format: "esm",
onAfterBundle(api) {
const chunk = chunkContaining(api, "side effect");
api.expectFile("/out/main.js").toContain(`import.meta.require("./${chunk}")`);
api.expectFile("/out/main.js").not.toContain(".default");
},
run: { file: "/out/main.js", stdout: "side effect\n19.0.0 rendered" },
});

// A top-level require() in a module that the required chunk imports back
// (the registry ↔ tool shape): the chunk is evaluated while the entry is
// still evaluating and sees the entry's hoisted functions through live
Expand Down
Loading