Skip to content

Commit

Permalink
fix: use native require cache of loaded entries only (#348)
Browse files Browse the repository at this point in the history
Co-authored-by: Jordan Pittman <[email protected]>
  • Loading branch information
pi0 and thecrypticace authored Dec 17, 2024
1 parent 4dacbf1 commit cf952e4
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/require.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,11 @@ export function jitiRequire(
if (cache[filename]) {
return jitiInteropDefault(ctx, cache[filename]?.exports);
}
if (ctx.opts.moduleCache && ctx.nativeRequire.cache[filename]) {
return jitiInteropDefault(ctx, ctx.nativeRequire.cache[filename]?.exports);
if (ctx.opts.moduleCache) {
const cacheEntry = ctx.nativeRequire.cache[filename];
if (cacheEntry?.loaded) {
return jitiInteropDefault(ctx, cacheEntry.exports);
}
}

// Read source
Expand Down
8 changes: 8 additions & 0 deletions test/__snapshots__/fixtures.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ Enter Identifier
Enter Literal"
`;
exports[`fixtures > require-esm > stderr 1`] = `
[
"ExperimentalWarning: CommonJS module <cwd>/index.cjs is loading ES Module <cwd>/_dist/esm.js using require().",
]
`;
exports[`fixtures > require-esm > stdout 1`] = `"Works!"`;
exports[`fixtures > syntax > stdout 1`] = `
"Optional chaining: undefined
Nullish coalescing: 0
Expand Down
11 changes: 10 additions & 1 deletion test/fixtures.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import { x } from "tinyexec";
import { describe, it, expect } from "vitest";
import fg from "fast-glob";

const nodeMajorVersion = Number.parseInt(
process.versions.node.split(".")[0],
10,
);

describe("fixtures", async () => {
const jitiPath = resolve(__dirname, "../lib/jiti-cli.mjs");

Expand Down Expand Up @@ -34,6 +39,7 @@ describe("fixtures", async () => {
.replace(/ParseError: \w:\/:\s+/, "ParseError: ") // Unknown chars in Windows
.replace("TypeError [ERR_INVALID_ARG_TYPE]:", "TypeError:")
.replace("eval_evalModule", "evalModule")
.replace(/\(node:\d+\)/g, "(node)")
// Node 18
.replace(
" ErrorCaptureStackTrace(err);",
Expand Down Expand Up @@ -66,7 +72,10 @@ describe("fixtures", async () => {
},
});

if (name.includes("error")) {
if (
name.includes("error") ||
(nodeMajorVersion >= 22 && name === "require-esm")
) {
expect(extractErrors(cleanUpSnap(stderr))).toMatchSnapshot("stderr");
} else {
// Expect no error by default
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/require-esm/_dist/esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import fn from "./fn.js";
import file from "./file";
export default { fn, file };
1 change: 1 addition & 0 deletions test/fixtures/require-esm/_dist/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {};
1 change: 1 addition & 0 deletions test/fixtures/require-esm/_dist/fn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = function _() {};
18 changes: 18 additions & 0 deletions test/fixtures/require-esm/index.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
async function run() {
let mod;
try {
mod = require("./_dist/esm.js");
} catch {
const { createJiti } = await import("../../../lib/jiti.mjs");
const jiti = createJiti(__filename);
mod = await jiti.import("./_dist/esm.js");
}
mod = mod.default;
if (typeof mod.fn === "function") {
console.log("Works!");
return;
}
console.error("broken!", { mod });
}

run();
3 changes: 3 additions & 0 deletions test/fixtures/require-esm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "-"
}

0 comments on commit cf952e4

Please sign in to comment.