|
1 |
| -import { existsSync } from 'fs'; |
2 |
| -import { createRequire } from 'module'; |
| 1 | +import { builtinModules } from 'node:module'; |
3 | 2 | import { dirname } from 'path';
|
4 |
| -import { URL, fileURLToPath, pathToFileURL } from 'url'; |
| 3 | +import { cwd } from 'process'; |
| 4 | +import { fileURLToPath, pathToFileURL } from 'url'; |
| 5 | +import { promisify } from 'util'; |
5 | 6 |
|
6 |
| -const require = createRequire(import.meta.url); |
7 |
| -const baseURL = pathToFileURL(process.cwd() + '/').href; |
| 7 | +import resolveCallback from 'resolve/async.js'; |
8 | 8 |
|
9 |
| -export function resolve(specifier, context, defaultResolve) { |
| 9 | +const resolveAsync = promisify(resolveCallback); |
| 10 | + |
| 11 | +const baseURL = pathToFileURL(cwd() + '/').href; |
| 12 | + |
| 13 | + |
| 14 | +export async function resolve(specifier, context, next) { |
10 | 15 | const { parentURL = baseURL } = context;
|
11 | 16 |
|
12 |
| - // `require.resolve` works with paths, not URLs, so convert to and from |
| 17 | + if (specifier.startsWith('node:') || builtinModules.includes(specifier)) { |
| 18 | + return next(specifier, context); |
| 19 | + } |
| 20 | + |
| 21 | + // `resolveAsync` works with paths, not URLs |
13 | 22 | if (specifier.startsWith('file://')) {
|
14 | 23 | specifier = fileURLToPath(specifier);
|
15 | 24 | }
|
16 |
| - const basePath = dirname(fileURLToPath(parentURL)); |
17 |
| - const resolvedPath = require.resolve(specifier, {paths: [basePath]}); |
| 25 | + const parentPath = fileURLToPath(parentURL); |
18 | 26 |
|
19 |
| - if (existsSync(resolvedPath)) { |
20 |
| - return { |
21 |
| - url: pathToFileURL(resolvedPath).href |
22 |
| - }; |
23 |
| - } |
| 27 | + const resolution = await resolveAsync(specifier, { |
| 28 | + basedir: dirname(parentPath), |
| 29 | + extensions: ['.js', '.json', '.node'], |
| 30 | + }); |
| 31 | + const url = pathToFileURL(resolution).href; |
24 | 32 |
|
25 |
| - // Let Node.js handle all other specifiers, such as package names |
26 |
| - return defaultResolve(specifier, context, defaultResolve); |
| 33 | + return next(url, context); |
27 | 34 | }
|
0 commit comments