Skip to content

Commit

Permalink
fix: proprly resolve .mts/.cts with .mjs/.cjs imports
Browse files Browse the repository at this point in the history
bun still does not supports this
  • Loading branch information
pi0 committed Jul 2, 2024
1 parent 934a5bb commit a5aefad
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export function resolveJitiOptions(userOptions: JitiOptions): JitiOptions {
".cjs",
".ts",
".tsx",
".mts",
".cts",
]),
alias: _jsonEnv<Record<string, string>>("JITI_ALIAS", {}),
nativeModules: _jsonEnv<string[]>("JITI_NATIVE_MODULES", []),
Expand Down
8 changes: 7 additions & 1 deletion src/require.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,13 @@ export function jitiRequire(

// Unknown format
if (ext && !ctx.opts.extensions!.includes(ext)) {
debug(ctx, "[unknown]", filename);
debug(
ctx,
"[native]",
"[unknown]",
opts.async ? "[import]" : "[require]",
filename,
);
return nativeImportOrRequire(ctx, id, opts.async);
}

Expand Down
5 changes: 4 additions & 1 deletion src/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ export function jitiResolve(
return resolved;
}
// Try resolving .ts files with .js extension
if (TS_EXT_RE.test(ctx.parentModule?.filename || "")) {
if (
TS_EXT_RE.test(ctx.filename) ||
TS_EXT_RE.test(ctx.parentModule?.filename || "")
) {
resolved = tryNativeRequireResolve(
ctx,
id.replace(JS_EXT_RE, ".$1t$2"),
Expand Down
1 change: 1 addition & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const debugMap = {
"[native]": cyan("[native]"),
"[transpile]": yellow("[transpile]"),
"[fallback]": red("[fallback]"),
"[unknown]": red("[unknown]"),
"[hit]": green("[hit]"),
"[miss]": yellow("[miss]"),
};
Expand Down
3 changes: 0 additions & 3 deletions test/register-test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ const fixturesDir = fileURLToPath(new URL("fixtures", import.meta.url));
const fixtures = await readdir(fixturesDir);

for (const fixture of fixtures) {
if (fixture === "typescript") {
continue; // .mts support
}
test("fixtures/" + fixture + " (ESM)", async () => {
const promise = import(`./fixtures/${fixture}`);
const shouldReject =
Expand Down

0 comments on commit a5aefad

Please sign in to comment.