Skip to content

Commit

Permalink
backport #348
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Dec 17, 2024
1 parent e63ec80 commit 50e8f2b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
5 changes: 4 additions & 1 deletion src/jiti.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,10 @@ export default function createJITI(
return _interopDefault(cache[filename]?.exports);
}
if (opts.requireCache && nativeRequire.cache[filename]) {
return _interopDefault(nativeRequire.cache[filename]?.exports);
const cacheEntry = nativeRequire.cache[filename];
if (cacheEntry?.loaded) {
return _interopDefault(cacheEntry.exports);
}
}

// Read source
Expand Down
27 changes: 15 additions & 12 deletions test/__snapshots__/fixtures.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ Error: ParseError: \`import\` can only be used in \`import
<cwd>/index.ts
at Object.<anonymous> (<root>/bin/jiti)
at Module._compile (internal/modules/cjs/loader)
at Module._extensions..js (internal/modules/cjs/loader)
at Object..js (internal/modules/cjs/loader)
at Module.load (internal/modules/cjs/loader)
at Module._load (internal/modules/cjs/loader)
at Function._load (internal/modules/cjs/loader)
at TracingChannel.traceSync (node:diagnostics_channel)
at wrapModuleLoad (internal/modules/cjs/loader)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main)
at internal/main/run_main_module
Expand All @@ -47,9 +49,11 @@ TypeError: The "listener" argument must be of type function. Received undefined
at jiti (<root>/dist/jiti)
at Object.<anonymous> (<root>/bin/jiti)
at Module._compile (internal/modules/cjs/loader)
at Module._extensions..js (internal/modules/cjs/loader)
at Object..js (internal/modules/cjs/loader)
at Module.load (internal/modules/cjs/loader)
at Module._load (internal/modules/cjs/loader)
at Function._load (internal/modules/cjs/loader)
at TracingChannel.traceSync (node:diagnostics_channel)
at wrapModuleLoad (internal/modules/cjs/loader)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main)
at internal/main/run_main_module {
code: 'ERR_INVALID_ARG_TYPE'
Expand All @@ -74,9 +78,11 @@ exports[`fixtures > esm > stdout 1`] = `
'at jiti (<root>/dist/jiti)',
'at Object.<anonymous> (<root>/bin/jiti)',
'at Module._compile (internal/modules/cjs/loader)',
'at Module._extensions..js (internal/modules/cjs/loader)',
'at Object..js (internal/modules/cjs/loader)',
'at Module.load (internal/modules/cjs/loader)',
'at Module._load (internal/modules/cjs/loader)',
'at Function._load (internal/modules/cjs/loader)',
'at TracingChannel.traceSync (node:diagnostics_channel)',
'at wrapModuleLoad (internal/modules/cjs/loader)',
'at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main)',
'at internal/main/run_main_module'
]
Expand All @@ -99,8 +105,6 @@ Required : { test: 123 } .default: { test: 123 }
Dynamic Imported : { test: 123 } .default: { test: 123 }"
`;
exports[`fixtures > mixed > stdout 1`] = `"Mixed works for: <cwd>"`;
exports[`fixtures > native > stdout 1`] = `"[Module: null prototype] { default: { hasRequire: false } }"`;
exports[`fixtures > proto > stdout 1`] = `"exists: true"`;
Expand All @@ -114,13 +118,12 @@ Enter Literal"
`;
exports[`fixtures > require-esm > require-esm-stderr 1`] = `
"(node:11558) ExperimentalWarning: CommonJS module <cwd>/index.cjs is loading ES Module <cwd>/_dist/esm.js using require().
"(node:43074) ExperimentalWarning: CommonJS module <cwd>/index.cjs is loading ES Module <cwd>/_dist/esm.js using require().
Support for loading ES Module in require() is an experimental feature and might change at any time
(Use \`node --trace-warnings ...\` to show where the warning was created)
broken! { mod: { fn: {}, file: {} } }"
(Use \`node --trace-warnings ...\` to show where the warning was created)"
`;
exports[`fixtures > require-esm > stdout 1`] = `""`;
exports[`fixtures > require-esm > stdout 1`] = `"Works!"`;
exports[`fixtures > syntax > stdout 1`] = `
"Optional chaining: undefined
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/require-esm/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ async function run() {
try {
mod = require("./_dist/esm.js");
} catch {
const { createJiti } = await import("../../../lib/jiti.mjs");
const createJiti = require("../../../lib/index.js");
const jiti = createJiti(__filename, { interopDefault: true });
mod = await jiti.import("./_dist/esm.js");
}
Expand Down

0 comments on commit 50e8f2b

Please sign in to comment.