Skip to content

Commit

Permalink
refactor: make jiti.esmResolve consistent with import.meta.resolve (
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Sep 25, 2024
1 parent 2bef8ae commit 27899c2
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 27 deletions.
4 changes: 2 additions & 2 deletions lib/jiti-hooks.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { dirname, join } from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
import { fileURLToPath } from "node:url";
import { existsSync } from "node:fs";
import { readFile } from "node:fs/promises";
import { isBuiltin } from "node:module";
Expand All @@ -22,7 +22,7 @@ export async function resolve(specifier, context, nextResolve) {
conditions: context?.conditions,
});
return {
url: pathToFileURL(resolvedPath).href,
url: resolvedPath,
shortCircuit: true,
};
}
Expand Down
1 change: 1 addition & 0 deletions lib/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface Jiti extends NodeRequire {
/**
* Resolve with ESM import conditions.
*/
esmResolve(id: string, parentURL?: string): string;
esmResolve<T extends JitiResolveOptions = JitiResolveOptions>(
id: string,
opts?: T,
Expand Down
22 changes: 2 additions & 20 deletions src/eval.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Module } from "node:module";
import { performance } from "node:perf_hooks";
import vm from "node:vm";
import { pathToFileURL } from "node:url";
import { dirname, basename, extname } from "pathe";
import { hasESMSyntax } from "mlly";
import {
Expand All @@ -10,12 +9,7 @@ import {
readNearestPackageJSON,
wrapModule,
} from "./utils";
import type {
ModuleCache,
Context,
EvalModuleOptions,
JitiResolveOptions,
} from "./types";
import type { ModuleCache, Context, EvalModuleOptions } from "./types";
import { jitiResolve } from "./resolve";
import { jitiRequire, nativeImportOrRequire } from "./require";
import createJiti from "./jiti";
Expand Down Expand Up @@ -151,18 +145,6 @@ export function evalModule(
}
}

// import.meta.resolve polyfill
// TODO: Make esmResolve more consistent with this
const importMetaResolve = (id: string, opts?: string | JitiResolveOptions) =>
pathToFileURL(
_jiti.esmResolve(
id,
typeof opts === "string"
? { parentURL: opts }
: { parentURL: mod.filename, ...opts },
),
).href;

// Evaluate module
let evalResult;
try {
Expand All @@ -173,7 +155,7 @@ export function evalModule(
mod.filename,
dirname(mod.filename),
_jiti.import,
importMetaResolve,
_jiti.esmResolve,
);
} catch (error: any) {
if (ctx.opts.moduleCache) {
Expand Down
23 changes: 19 additions & 4 deletions src/jiti.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
JitiResolveOptions,
} from "./types";
import { platform } from "node:os";
import { pathToFileURL } from "node:url";
import { pathToFileURL } from "mlly";
import { join } from "pathe";
import escapeStringRegexp from "escape-string-regexp";
import { normalizeAliases } from "pathe/utils";
Expand Down Expand Up @@ -82,7 +82,7 @@ export default function createJiti(
// Create shared context
const ctx: Context = {
filename,
url: url as URL,
url,
opts,
alias,
nativeModules,
Expand Down Expand Up @@ -144,8 +144,23 @@ export default function createJiti(
async import(id: string, opts?: JitiResolveOptions) {
return await jitiRequire(ctx, id, { ...opts, async: true });
},
esmResolve(id: string, opts?: JitiResolveOptions) {
return jitiResolve(ctx, id, { ...opts, async: true });
esmResolve(id: string, opts?: string | JitiResolveOptions): string {
if (typeof opts === "string") {
opts = { parentURL: opts };
}
const resolved = jitiResolve(ctx, id, {
parentURL: url as any,
...opts,
async: true,
});
if (
!resolved ||
typeof resolved !== "string" ||
resolved.startsWith("file://")
) {
return resolved;
}
return pathToFileURL(resolved);
},
},
);
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type {

export interface Context {
filename: string;
url: URL;
url: string;
parentModule?: NodeModule;
parentCache?: ModuleCache;
nativeImport?: (id: string) => Promise<any>;
Expand Down

0 comments on commit 27899c2

Please sign in to comment.