Skip to content

Commit 7cdac22

Browse files
committed
feat: improve relative path handling for Bun and Deno in createPathResolverForRuntime
1 parent 1877278 commit 7cdac22

1 file changed

Lines changed: 32 additions & 47 deletions

File tree

use.mjs

Lines changed: 32 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -32,60 +32,45 @@ const createPathResolverForRuntime = async (scriptPath, protocol) => {
3232
return (path) => path;
3333
}
3434

35-
// Bun with scriptPath - needs special handling for relative paths
36-
if (isBun && hasScriptPath) {
37-
const module = await import('node:module');
38-
const requireResolve = module.createRequire(scriptPath).resolve;
39-
return (specifier) => {
40-
try {
41-
return requireResolve(specifier);
42-
} catch (error) {
43-
if (specifier.startsWith('./') || specifier.startsWith('../')) {
44-
try {
45-
return new URL(specifier, scriptPath).pathname; // Bun uses pathname
46-
} catch (urlError) {
47-
throw error;
48-
}
49-
}
50-
throw error;
51-
}
52-
};
53-
}
54-
55-
// Deno with scriptPath - needs special handling for relative paths
56-
if (isDeno && hasScriptPath) {
57-
const module = await import('node:module');
58-
const requireResolve = module.createRequire(scriptPath).resolve;
59-
return (specifier) => {
60-
try {
61-
return requireResolve(specifier);
62-
} catch (error) {
63-
if (specifier.startsWith('./') || specifier.startsWith('../')) {
64-
try {
65-
return new URL(specifier, scriptPath).href; // Deno uses full URL
66-
} catch (urlError) {
67-
return specifier;
68-
}
69-
}
70-
return specifier; // For absolute paths/modules, return as-is
71-
}
72-
};
35+
// Has require but no scriptPath (Node.js fallback)
36+
if (hasRequire && !hasScriptPath) {
37+
return require.resolve;
7338
}
7439

75-
// Node.js with scriptPath - use createRequire
40+
// Get the base requireResolve function for all scriptPath cases
41+
let requireResolve;
7642
if (hasScriptPath) {
77-
return await import('node:module')
78-
.then(module => module.createRequire(scriptPath))
79-
.then(require => require.resolve);
43+
const module = await import('node:module');
44+
requireResolve = module.createRequire(scriptPath).resolve;
8045
}
8146

82-
// Has require but no scriptPath (Node.js fallback)
83-
if (hasRequire) {
84-
return require.resolve;
47+
// For vanilla Node.js with scriptPath, just return the requireResolve
48+
if (!isBun && !isDeno && hasScriptPath) {
49+
return requireResolve;
8550
}
8651

87-
// Final fallback
88-
return (path) => path;
52+
// For Bun and Deno, add relative path fallback
53+
return (specifier) => {
54+
try {
55+
return requireResolve(specifier);
56+
} catch (error) {
57+
// Handle relative paths with runtime-specific behavior
58+
if (specifier.startsWith('./') || specifier.startsWith('../')) {
59+
try {
60+
const url = new URL(specifier, scriptPath);
61+
if (isBun) return url.pathname;
62+
if (isDeno) return url.href;
63+
return url.href; // Default
64+
} catch (urlError) {
65+
if (isDeno) return specifier; // Deno fallback
66+
throw error;
67+
}
68+
}
69+
// Deno returns specifier as-is for non-relative paths
70+
if (isDeno) return specifier;
71+
throw error;
72+
}
73+
};
8974
};
9075

9176
const extractCallerContext = () => {

0 commit comments

Comments
 (0)