diff --git a/src/require.ts b/src/require.ts index f65c689a..92959e2d 100644 --- a/src/require.ts +++ b/src/require.ts @@ -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 diff --git a/test/__snapshots__/fixtures.test.ts.snap b/test/__snapshots__/fixtures.test.ts.snap index 34d0a361..828f89f7 100644 --- a/test/__snapshots__/fixtures.test.ts.snap +++ b/test/__snapshots__/fixtures.test.ts.snap @@ -94,6 +94,14 @@ Enter Identifier Enter Literal" `; +exports[`fixtures > require-esm > stderr 1`] = ` +[ + "ExperimentalWarning: CommonJS module /index.cjs is loading ES Module /_dist/esm.js using require().", +] +`; + +exports[`fixtures > require-esm > stdout 1`] = `"Works!"`; + exports[`fixtures > syntax > stdout 1`] = ` "Optional chaining: undefined Nullish coalescing: 0 diff --git a/test/fixtures.test.ts b/test/fixtures.test.ts index 1c67e48d..07652f61 100644 --- a/test/fixtures.test.ts +++ b/test/fixtures.test.ts @@ -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"); @@ -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);", @@ -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 diff --git a/test/fixtures/require-esm/_dist/esm.js b/test/fixtures/require-esm/_dist/esm.js new file mode 100644 index 00000000..9c5bd062 --- /dev/null +++ b/test/fixtures/require-esm/_dist/esm.js @@ -0,0 +1,3 @@ +import fn from "./fn.js"; +import file from "./file"; +export default { fn, file }; diff --git a/test/fixtures/require-esm/_dist/file.js b/test/fixtures/require-esm/_dist/file.js new file mode 100644 index 00000000..f053ebf7 --- /dev/null +++ b/test/fixtures/require-esm/_dist/file.js @@ -0,0 +1 @@ +module.exports = {}; diff --git a/test/fixtures/require-esm/_dist/fn.js b/test/fixtures/require-esm/_dist/fn.js new file mode 100644 index 00000000..3e856d5f --- /dev/null +++ b/test/fixtures/require-esm/_dist/fn.js @@ -0,0 +1 @@ +module.exports = function _() {}; diff --git a/test/fixtures/require-esm/index.cjs b/test/fixtures/require-esm/index.cjs new file mode 100644 index 00000000..bdc0e6d1 --- /dev/null +++ b/test/fixtures/require-esm/index.cjs @@ -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(); diff --git a/test/fixtures/require-esm/package.json b/test/fixtures/require-esm/package.json new file mode 100644 index 00000000..c528eb84 --- /dev/null +++ b/test/fixtures/require-esm/package.json @@ -0,0 +1,3 @@ +{ + "type": "-" +}