apply feedback
This commit is contained in:
parent
5b8f3d038b
commit
8fe370acf8
|
|
@ -26,7 +26,7 @@ import { fileIsModule } from '../util';
|
||||||
async function resolve(specifier: string, context: { parentURL?: string }, defaultResolve: Function) {
|
async function resolve(specifier: string, context: { parentURL?: string }, defaultResolve: Function) {
|
||||||
if (context.parentURL && context.parentURL.startsWith('file://')) {
|
if (context.parentURL && context.parentURL.startsWith('file://')) {
|
||||||
const filename = url.fileURLToPath(context.parentURL);
|
const filename = url.fileURLToPath(context.parentURL);
|
||||||
const resolved = resolveHook(filename, specifier, true);
|
const resolved = resolveHook(filename, specifier);
|
||||||
if (resolved !== undefined)
|
if (resolved !== undefined)
|
||||||
specifier = url.pathToFileURL(resolved).toString();
|
specifier = url.pathToFileURL(resolved).toString();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -92,14 +92,14 @@ function loadAndValidateTsconfigsForFile(file: string): ParsedTsConfigData[] {
|
||||||
const pathSeparator = process.platform === 'win32' ? ';' : ':';
|
const pathSeparator = process.platform === 'win32' ? ';' : ':';
|
||||||
const builtins = new Set(Module.builtinModules);
|
const builtins = new Set(Module.builtinModules);
|
||||||
|
|
||||||
export function resolveHook(filename: string, specifier: string, dontResolveDirectories = false): string | undefined {
|
export function resolveHook(filename: string, specifier: string): string | undefined {
|
||||||
if (specifier.startsWith('node:') || builtins.has(specifier))
|
if (specifier.startsWith('node:') || builtins.has(specifier))
|
||||||
return;
|
return;
|
||||||
if (!shouldTransform(filename))
|
if (!shouldTransform(filename))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (isRelativeSpecifier(specifier))
|
if (isRelativeSpecifier(specifier))
|
||||||
return resolveImportSpecifierExtension(path.resolve(path.dirname(filename), specifier), false);
|
return resolveImportSpecifierExtension(path.resolve(path.dirname(filename), specifier), true);
|
||||||
|
|
||||||
const isTypeScript = filename.endsWith('.ts') || filename.endsWith('.tsx');
|
const isTypeScript = filename.endsWith('.ts') || filename.endsWith('.tsx');
|
||||||
const tsconfigs = loadAndValidateTsconfigsForFile(filename);
|
const tsconfigs = loadAndValidateTsconfigsForFile(filename);
|
||||||
|
|
@ -142,7 +142,7 @@ export function resolveHook(filename: string, specifier: string, dontResolveDire
|
||||||
if (value.includes('*'))
|
if (value.includes('*'))
|
||||||
candidate = candidate.replace('*', matchedPartOfSpecifier);
|
candidate = candidate.replace('*', matchedPartOfSpecifier);
|
||||||
candidate = path.resolve(tsconfig.pathsBase!, candidate);
|
candidate = path.resolve(tsconfig.pathsBase!, candidate);
|
||||||
const existing = resolveImportSpecifierExtension(candidate, dontResolveDirectories);
|
const existing = resolveImportSpecifierExtension(candidate, false);
|
||||||
if (existing) {
|
if (existing) {
|
||||||
longestPrefixLength = keyPrefix.length;
|
longestPrefixLength = keyPrefix.length;
|
||||||
pathMatchedByLongestPrefix = existing;
|
pathMatchedByLongestPrefix = existing;
|
||||||
|
|
@ -156,7 +156,7 @@ export function resolveHook(filename: string, specifier: string, dontResolveDire
|
||||||
if (path.isAbsolute(specifier)) {
|
if (path.isAbsolute(specifier)) {
|
||||||
// Handle absolute file paths like `import '/path/to/file'`
|
// Handle absolute file paths like `import '/path/to/file'`
|
||||||
// Do not handle module imports like `import 'fs'`
|
// Do not handle module imports like `import 'fs'`
|
||||||
return resolveImportSpecifierExtension(specifier, false);
|
return resolveImportSpecifierExtension(specifier, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -304,7 +304,7 @@ const kExtLookups = new Map([
|
||||||
['.mjs', ['.mts']],
|
['.mjs', ['.mts']],
|
||||||
['', ['.js', '.ts', '.jsx', '.tsx', '.cjs', '.mjs', '.cts', '.mts']],
|
['', ['.js', '.ts', '.jsx', '.tsx', '.cjs', '.mjs', '.cts', '.mts']],
|
||||||
]);
|
]);
|
||||||
export function resolveImportSpecifierExtension(resolved: string, dontResolveDirectories: boolean): string | undefined {
|
export function resolveImportSpecifierExtension(resolved: string, resolveDirectories: boolean): string | undefined {
|
||||||
if (fileExists(resolved))
|
if (fileExists(resolved))
|
||||||
return resolved;
|
return resolved;
|
||||||
|
|
||||||
|
|
@ -319,17 +319,14 @@ export function resolveImportSpecifierExtension(resolved: string, dontResolveDir
|
||||||
break; // Do not try '' when a more specific extension like '.jsx' matched.
|
break; // Do not try '' when a more specific extension like '.jsx' matched.
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dirExists(resolved)) {
|
if (resolveDirectories && dirExists(resolved)) {
|
||||||
if (dontResolveDirectories)
|
|
||||||
return;
|
|
||||||
|
|
||||||
// If we import a package, let Node.js figure out the correct import based on package.json.
|
// If we import a package, let Node.js figure out the correct import based on package.json.
|
||||||
if (fileExists(path.join(resolved, 'package.json')))
|
if (fileExists(path.join(resolved, 'package.json')))
|
||||||
return resolved;
|
return resolved;
|
||||||
|
|
||||||
// Otherwise, try to find a corresponding index file.
|
// Otherwise, try to find a corresponding index file.
|
||||||
const dirImport = path.join(resolved, 'index');
|
const dirImport = path.join(resolved, 'index');
|
||||||
return resolveImportSpecifierExtension(dirImport, dontResolveDirectories);
|
return resolveImportSpecifierExtension(dirImport, resolveDirectories);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue