Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resolve with esm conditions in async context #264

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export interface Jiti extends NodeRequire {
* ESM import a module with additional Typescript and ESM compatibility.
*/
import: (id: string) => Promise<unknown>;
/**
* Resolve with ESM import conditions.
*/
importResolve: (id: string) => string;
/**
* Transform source code
*/
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"build": "pnpm clean && NODE_ENV=production pnpm webpack",
"clean": "rm -rf dist",
"dev": "pnpm clean && pnpm webpack --watch",
"jiti": "JITI_DEBUG=1 JITI_CACHE=false JITI_REQUIRE_CACHE=false ./lib/jiti-cli.mjs",
"jiti": "JITI_DEBUG=1 ./lib/jiti-cli.mjs",
"lint": "eslint . && prettier -c src lib test stubs",
"lint:fix": "eslint --fix . && prettier -w src lib test stubs",
"release": "pnpm build && pnpm test && changelogen --release --prerelease --push --publish --publishTag 2x",
Expand Down
5 changes: 3 additions & 2 deletions src/eval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ export function evalModule(
evalOptions.id ||
(evalOptions.filename
? basename(evalOptions.filename)
: `_jitiEval.${evalOptions.ext || ".js"}`);
const filename = evalOptions.filename || jitiResolve(ctx, id);
: `_jitiEval.${evalOptions.ext || (evalOptions.async ? "mjs" : "js")}`);
const filename =
evalOptions.filename || jitiResolve(ctx, id, { async: evalOptions.async });
const ext = evalOptions.ext || extname(filename);
const cache = (evalOptions.cache || ctx.parentCache || {}) as ModuleCache;

Expand Down
5 changes: 4 additions & 1 deletion src/jiti.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export default function createJiti(
main: nativeRequire.main,
resolve: Object.assign(
function resolve(path: string) {
return jitiResolve(ctx, path);
return jitiResolve(ctx, path, { async: false });
},
{
paths: nativeRequire.resolve.paths,
Expand All @@ -133,6 +133,9 @@ export default function createJiti(
async import(id: string) {
return await jitiRequire(ctx, id, true /* async */);
},
importResolve(id: string) {
return jitiResolve(ctx, id, { async: true });
},
},
);

Expand Down
4 changes: 2 additions & 2 deletions src/require.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function jitiRequire(ctx: Context, id: string, async: boolean) {
if (ctx.opts.experimentalBun && !ctx.opts.transformOptions) {
try {
debug(ctx, `[bun] [native] ${id}`);
id = jitiResolve(ctx, id);
id = jitiResolve(ctx, id, { async });
if (async && ctx.nativeImport) {
return ctx.nativeImport(id).then((m: any) => {
if (ctx.opts.moduleCache === false) {
Expand All @@ -48,7 +48,7 @@ export function jitiRequire(ctx: Context, id: string, async: boolean) {
}

// Resolve path
const filename = jitiResolve(ctx, id);
const filename = jitiResolve(ctx, id, { async });
const ext = extname(filename);

// Check for .json modules
Expand Down
5 changes: 2 additions & 3 deletions src/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ const TS_EXT_RE = /\.(c|m)?t(sx?)$/;
export function jitiResolve(
ctx: Context,
id: string,
options?: { paths?: string[] },
async?: boolean,
options?: { paths?: string[]; async?: boolean },
) {
let resolved, err;

Expand All @@ -19,7 +18,7 @@ export function jitiResolve(
}

// Try resolving with ESM compatible Node.js resolution in async context
const conditionSets = async
const conditionSets = options?.async
? [
["node", "import"],
["node", "require"],
Expand Down