Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 25 additions & 3 deletions src/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,18 @@
/*__PURE__*/
var __moduleCache;

// When you do know the module is CJS
export var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
// When you do know the module is CJS.
// Node removes a module from require.cache when its evaluation throws, so a
// later require() re-evaluates it instead of returning the partial exports.
export var __commonJS = (cb, mod) => () => {
if (!mod)
try {
cb((mod = { exports: {} }).exports, mod);
} catch (e) {
throw ((mod = 0), e);
}
return mod.exports;
};

Check failure on line 120 in src/runtime.js

View check run for this annotation

Claude / Claude Code Review

npm/ReactSSR test not updated for larger __commonJS helper

The `npm/ReactSSR` test in `test/bundler/bundler_npm.test.ts` was not updated for the larger `__commonJS` helper. It pins `expectExactFilesize: { "out/entry.js": 221969 }` and hard-coded line-1 generated columns (`"1:5623:nt"`, `'1:7685:...'`) that shift when the minified runtime preamble grows, so it will fail on Linux/macOS CI. Re-run the test and update the byte count and the two line-1 column offsets, like you did for `mappingsExactMatch` in `bundler_edgecase.test.ts`.
Comment thread
robobun marked this conversation as resolved.
Resolved

export var __name = (target, name) => {
Object.defineProperty(target, "name", {
Expand Down Expand Up @@ -318,7 +328,19 @@
);
};

export var __esm = (fn, res) => () => (fn && (res = fn((fn = 0))), res);
// Lazily-evaluated ESM module body. ECMA-262 memoizes a failed evaluation:
// re-importing must re-throw the same error, never succeed with a partial
// namespace. `err` boxes the value so falsy throws still count as failed.
export var __esm = (fn, res, err) => () => {
if (err) throw err[0];
if (fn)
try {
res = fn((fn = 0));
} catch (e) {
throw ((err = [e]), e);
}
return res;
};

// This is used for JSX inlining with React.
export var $$typeof = /* @__PURE__ */ Symbol.for("react.element");
Expand Down
52 changes: 51 additions & 1 deletion test/bundler/bundler_edgecase.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1160,7 +1160,7 @@ describe("bundler", () => {
snapshotSourceMap: {
"entry.js.map": {
files: ["../node_modules/react/index.js", "../entry.js"],
mappingsExactMatch: "2lBACA,WAAW,IAAQ,EAAE,ICDrB,eACA,QAAQ,IAAI,CAAK",
mappingsExactMatch: "8nBACA,WAAW,IAAQ,EAAE,ICDrB,eACA,QAAQ,IAAI,CAAK",
},
},
});
Expand Down Expand Up @@ -2168,6 +2168,56 @@ describe("bundler", () => {
},
});

// ECMA-262 memoizes a module evaluation that threw: every later import of it
// must reject with the same error, never succeed with a partial namespace.
itBundled("edgecase/ESMEvaluationThrowIsMemoized", {
files: {
"/entry.js": /* js */ `
const errors = [];
for (let i = 1; i <= 2; i++)
await import("./thrower.js").then(
ns => console.log(i, "fulfilled", JSON.stringify(ns)),
e => (errors.push(e), console.log(i, "rejected", e.message)),
);
console.log("same error:", errors.length === 2 && errors[0] === errors[1]);
console.log("evaluations:", globalThis.evaluations);
`,
"/thrower.js": /* js */ `
export let v = 1;
globalThis.evaluations = (globalThis.evaluations ?? 0) + 1;
throw new Error("boom");
`,
},
run: {
stdout: "1 rejected boom\n2 rejected boom\nsame error: true\nevaluations: 1",
},
});

// Node removes a CJS module from require.cache when its evaluation throws, so
// a later require() re-evaluates it instead of seeing the partial exports.
itBundled("edgecase/CommonJSEvaluationThrowIsNotCached", {
files: {
"/entry.cjs": /* js */ `
for (let i = 1; i <= 2; i++) {
try {
console.log(i, "loaded", JSON.stringify(require("./thrower.cjs")));
} catch (e) {
console.log(i, "threw", e.message);
}
}
console.log("evaluations:", globalThis.evaluations);
`,
"/thrower.cjs": /* js */ `
exports.v = 1;
globalThis.evaluations = (globalThis.evaluations ?? 0) + 1;
throw new Error("boom");
`,
},
run: {
stdout: "1 threw boom\n2 threw boom\nevaluations: 2",
},
});

itBundled("edgecase/UsingExportDefaultAsync", {
files: {
"/entry.ts": `
Expand Down
28 changes: 24 additions & 4 deletions test/bundler/bundler_promiseall_deadcode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,17 @@ describe("bundler", () => {
set: __exportSetter.bind(all, name)
});
};
var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
var __esm = (fn, res, err) => () => {
if (err)
throw err[0];
if (fn)
try {
res = fn(fn = 0);
} catch (e) {
throw err = [e], e;
}
return res;
};
var __promiseAll = (args) => Promise.all(args);

// StoreDependencyAsync.ts
Expand Down Expand Up @@ -164,7 +174,7 @@ describe("bundler", () => {
var { AsyncEntryPoint: AsyncEntryPoint2 } = await Promise.resolve().then(() => exports_AsyncEntryPoint);
AsyncEntryPoint2();

//# debugId=42062903F19477CF64756E2164756E21
//# debugId=3EF63DB2CB2ED5F664756E2164756E21
//# sourceMappingURL=out.js.map
"
`);
Expand Down Expand Up @@ -354,7 +364,17 @@ describe("bundler", () => {
set: __exportSetter.bind(all, name)
});
};
var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
var __esm = (fn, res, err) => () => {
if (err)
throw err[0];
if (fn)
try {
res = fn(fn = 0);
} catch (e) {
throw err = [e], e;
}
return res;
};

// SecondElementImport.ts
function SecondElementImport() {
Expand Down Expand Up @@ -410,7 +430,7 @@ describe("bundler", () => {
var { AsyncEntryPoint: AsyncEntryPoint2 } = await Promise.resolve().then(() => exports_AsyncEntryPoint);
AsyncEntryPoint2();

//# debugId=BF876FBF618133C264756E2164756E21
//# debugId=71E152310DE2511464756E2164756E21
//# sourceMappingURL=out.js.map
"
`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,15 @@ var __toESM = (mod, isNodeMode, target) => {
cache.set(mod, to);
return to;
};
var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
var __commonJS = (cb, mod) => () => {
if (!mod)
try {
cb((mod = { exports: {} }).exports, mod);
} catch (e) {
throw mod = 0, e;
}
return mod.exports;
};

// node_modules/react/index.js
var require_react = __commonJS(function(exports) {
Expand Down
14 changes: 12 additions & 2 deletions test/regression/issue/cyclic-imports-async-bundler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,17 @@ test("cyclic imports with async dependencies should generate async wrappers", as
set: __exportSetter.bind(all, name)
});
};
var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
var __esm = (fn, res, err) => () => {
if (err)
throw err[0];
if (fn)
try {
res = fn(fn = 0);
} catch (e) {
throw err = [e], e;
}
return res;
};
var __promiseAll = (args) => Promise.all(args);

// src/RecursiveDependencies/StoreDependencyAsync.ts
Expand Down Expand Up @@ -180,7 +190,7 @@ test("cyclic imports with async dependencies should generate async wrappers", as
var { AsyncEntryPoint: AsyncEntryPoint2 } = await Promise.resolve().then(() => exports_AsyncEntryPoint);
AsyncEntryPoint2();

//# debugId=2020261114B67BB564756E2164756E21
//# debugId=D5F43ED7FE060A5564756E2164756E21
//# sourceMappingURL=entryBuild.js.map
"
`);
Expand Down
Loading