Skip to content

Commit

Permalink
fix: use native require cache of loaded entries only (v1) (#349)
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 c3d53c4 commit 04eadf7
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 2 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
8 changes: 8 additions & 0 deletions test/__snapshots__/fixtures.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,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
2 changes: 1 addition & 1 deletion test/fixtures.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe("fixtures", async () => {

if (name.includes("error")) {
expect(extractErrors(cleanUpSnap(stderr))).toMatchSnapshot("stderr");
} else if (name === "mixed" && nodeMajor >= 22) {
} else if (nodeMajor >= 22 && ["mixed", "require-esm"].includes(name)) {
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 = require("../../../lib/index.js");
const jiti = createJiti(__filename, { interopDefault: true });
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 04eadf7

Please sign in to comment.