mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Add alias ResolutionMode for ModuleKind.ESNext | ModuleKind.CommonJs | undefined (#51482)
* Add alias ResolutionMode for ModuleKind.ESNext | ModuleKind.CommonJs | undefined * ResolutionMode | undefined = ResolutionMode * More
This commit is contained in:
@@ -13,6 +13,7 @@ import {
|
||||
ModuleKind, ModuleResolutionHost, ModuleResolutionKind, noop, noopPush, normalizePath, normalizeSlashes,
|
||||
optionsHaveModuleResolutionChanges, PackageId, packageIdToString, ParsedCommandLine, Path, pathIsRelative, Pattern,
|
||||
patternText, perfLogger, Push, readJson, removeExtension, removeFileExtension, removePrefix,
|
||||
ResolutionMode,
|
||||
ResolvedModuleWithFailedLookupLocations, ResolvedProjectReference, ResolvedTypeReferenceDirective,
|
||||
ResolvedTypeReferenceDirectiveWithFailedLookupLocations, some, sort, SourceFile, startsWith, stringContains,
|
||||
StringLiteralLike, supportedTSExtensionsFlat, toFileNameLowerCase, toPath, tryExtractTSExtension, tryGetExtensionFromPath,
|
||||
@@ -333,7 +334,7 @@ function arePathsEqual(path1: string, path2: string, host: ModuleResolutionHost)
|
||||
* This is possible in case if resolution is performed for directives specified via 'types' parameter. In this case initial path for secondary lookups
|
||||
* is assumed to be the same as root directory of the project.
|
||||
*/
|
||||
export function resolveTypeReferenceDirective(typeReferenceDirectiveName: string, containingFile: string | undefined, options: CompilerOptions, host: ModuleResolutionHost, redirectedReference?: ResolvedProjectReference, cache?: TypeReferenceDirectiveResolutionCache, resolutionMode?: SourceFile["impliedNodeFormat"]): ResolvedTypeReferenceDirectiveWithFailedLookupLocations {
|
||||
export function resolveTypeReferenceDirective(typeReferenceDirectiveName: string, containingFile: string | undefined, options: CompilerOptions, host: ModuleResolutionHost, redirectedReference?: ResolvedProjectReference, cache?: TypeReferenceDirectiveResolutionCache, resolutionMode?: ResolutionMode): ResolvedTypeReferenceDirectiveWithFailedLookupLocations {
|
||||
Debug.assert(typeof typeReferenceDirectiveName === "string", "Non-string value passed to `ts.resolveTypeReferenceDirective`, likely by a wrapping package working with an outdated `resolveTypeReferenceDirectives` signature. This is probably not a problem in TS itself.");
|
||||
const traceEnabled = isTraceEnabled(options, host);
|
||||
if (redirectedReference) {
|
||||
@@ -571,11 +572,11 @@ export interface TypeReferenceDirectiveResolutionCache extends PerDirectoryResol
|
||||
}
|
||||
|
||||
export interface ModeAwareCache<T> {
|
||||
get(key: string, mode: ModuleKind.CommonJS | ModuleKind.ESNext | undefined): T | undefined;
|
||||
set(key: string, mode: ModuleKind.CommonJS | ModuleKind.ESNext | undefined, value: T): this;
|
||||
delete(key: string, mode: ModuleKind.CommonJS | ModuleKind.ESNext | undefined): this;
|
||||
has(key: string, mode: ModuleKind.CommonJS | ModuleKind.ESNext | undefined): boolean;
|
||||
forEach(cb: (elem: T, key: string, mode: ModuleKind.CommonJS | ModuleKind.ESNext | undefined) => void): void;
|
||||
get(key: string, mode: ResolutionMode): T | undefined;
|
||||
set(key: string, mode: ResolutionMode, value: T): this;
|
||||
delete(key: string, mode: ResolutionMode): this;
|
||||
has(key: string, mode: ResolutionMode): boolean;
|
||||
forEach(cb: (elem: T, key: string, mode: ResolutionMode) => void): void;
|
||||
size(): number;
|
||||
}
|
||||
|
||||
@@ -603,7 +604,7 @@ export interface ModuleResolutionCache extends PerDirectoryResolutionCache<Resol
|
||||
* We support only non-relative module names because resolution of relative module names is usually more deterministic and thus less expensive.
|
||||
*/
|
||||
export interface NonRelativeModuleNameResolutionCache extends PackageJsonInfoCache {
|
||||
getOrCreateCacheForModuleName(nonRelativeModuleName: string, mode: ModuleKind.CommonJS | ModuleKind.ESNext | undefined, redirectedReference?: ResolvedProjectReference): PerModuleNameCache;
|
||||
getOrCreateCacheForModuleName(nonRelativeModuleName: string, mode: ResolutionMode, redirectedReference?: ResolvedProjectReference): PerModuleNameCache;
|
||||
}
|
||||
|
||||
export interface PackageJsonInfoCache {
|
||||
@@ -758,7 +759,7 @@ function createPerDirectoryResolutionCache<T>(currentDirectory: string, getCanon
|
||||
export function createModeAwareCache<T>(): ModeAwareCache<T> {
|
||||
const underlying = new Map<ModeAwareCacheKey, T>();
|
||||
type ModeAwareCacheKey = string & { __modeAwareCacheKey: any; };
|
||||
const memoizedReverseKeys = new Map<ModeAwareCacheKey, [specifier: string, mode: ModuleKind.CommonJS | ModuleKind.ESNext | undefined]>();
|
||||
const memoizedReverseKeys = new Map<ModeAwareCacheKey, [specifier: string, mode: ResolutionMode]>();
|
||||
|
||||
const cache: ModeAwareCache<T> = {
|
||||
get(specifier, mode) {
|
||||
@@ -787,7 +788,7 @@ export function createModeAwareCache<T>(): ModeAwareCache<T> {
|
||||
};
|
||||
return cache;
|
||||
|
||||
function getUnderlyingCacheKey(specifier: string, mode: ModuleKind.CommonJS | ModuleKind.ESNext | undefined) {
|
||||
function getUnderlyingCacheKey(specifier: string, mode: ResolutionMode) {
|
||||
const result = (mode === undefined ? specifier : `${mode}|${specifier}`) as ModeAwareCacheKey;
|
||||
memoizedReverseKeys.set(result, [specifier, mode]);
|
||||
return result;
|
||||
@@ -864,7 +865,7 @@ export function createModuleResolutionCache(
|
||||
updateRedirectsMap(options, directoryToModuleNameMap!, moduleNameToDirectoryMap);
|
||||
}
|
||||
|
||||
function getOrCreateCacheForModuleName(nonRelativeModuleName: string, mode: ModuleKind.CommonJS | ModuleKind.ESNext | undefined, redirectedReference?: ResolvedProjectReference): PerModuleNameCache {
|
||||
function getOrCreateCacheForModuleName(nonRelativeModuleName: string, mode: ResolutionMode, redirectedReference?: ResolvedProjectReference): PerModuleNameCache {
|
||||
Debug.assert(!isExternalModuleNameRelative(nonRelativeModuleName));
|
||||
return getOrCreateCache(moduleNameToDirectoryMap!, redirectedReference, mode === undefined ? nonRelativeModuleName : `${mode}|${nonRelativeModuleName}`, createPerModuleNameCache);
|
||||
}
|
||||
@@ -983,14 +984,14 @@ export function createTypeReferenceDirectiveResolutionCache(
|
||||
}
|
||||
}
|
||||
|
||||
export function resolveModuleNameFromCache(moduleName: string, containingFile: string, cache: ModuleResolutionCache, mode?: ModuleKind.CommonJS | ModuleKind.ESNext): ResolvedModuleWithFailedLookupLocations | undefined {
|
||||
export function resolveModuleNameFromCache(moduleName: string, containingFile: string, cache: ModuleResolutionCache, mode?: ResolutionMode): ResolvedModuleWithFailedLookupLocations | undefined {
|
||||
const containingDirectory = getDirectoryPath(containingFile);
|
||||
const perFolderCache = cache && cache.getOrCreateCacheForDirectory(containingDirectory);
|
||||
if (!perFolderCache) return undefined;
|
||||
return perFolderCache.get(moduleName, mode);
|
||||
}
|
||||
|
||||
export function resolveModuleName(moduleName: string, containingFile: string, compilerOptions: CompilerOptions, host: ModuleResolutionHost, cache?: ModuleResolutionCache, redirectedReference?: ResolvedProjectReference, resolutionMode?: ModuleKind.CommonJS | ModuleKind.ESNext): ResolvedModuleWithFailedLookupLocations {
|
||||
export function resolveModuleName(moduleName: string, containingFile: string, compilerOptions: CompilerOptions, host: ModuleResolutionHost, cache?: ModuleResolutionCache, redirectedReference?: ResolvedProjectReference, resolutionMode?: ResolutionMode): ResolvedModuleWithFailedLookupLocations {
|
||||
const traceEnabled = isTraceEnabled(compilerOptions, host);
|
||||
if (redirectedReference) {
|
||||
compilerOptions = redirectedReference.commandLine.options;
|
||||
@@ -1312,7 +1313,7 @@ export enum NodeResolutionFeatures {
|
||||
|
||||
function node16ModuleNameResolver(moduleName: string, containingFile: string, compilerOptions: CompilerOptions,
|
||||
host: ModuleResolutionHost, cache?: ModuleResolutionCache, redirectedReference?: ResolvedProjectReference,
|
||||
resolutionMode?: ModuleKind.CommonJS | ModuleKind.ESNext): ResolvedModuleWithFailedLookupLocations {
|
||||
resolutionMode?: ResolutionMode): ResolvedModuleWithFailedLookupLocations {
|
||||
return nodeNextModuleNameResolverWorker(
|
||||
NodeResolutionFeatures.Node16Default,
|
||||
moduleName,
|
||||
@@ -1327,7 +1328,7 @@ function node16ModuleNameResolver(moduleName: string, containingFile: string, co
|
||||
|
||||
function nodeNextModuleNameResolver(moduleName: string, containingFile: string, compilerOptions: CompilerOptions,
|
||||
host: ModuleResolutionHost, cache?: ModuleResolutionCache, redirectedReference?: ResolvedProjectReference,
|
||||
resolutionMode?: ModuleKind.CommonJS | ModuleKind.ESNext): ResolvedModuleWithFailedLookupLocations {
|
||||
resolutionMode?: ResolutionMode): ResolvedModuleWithFailedLookupLocations {
|
||||
return nodeNextModuleNameResolverWorker(
|
||||
NodeResolutionFeatures.NodeNextDefault,
|
||||
moduleName,
|
||||
@@ -1344,7 +1345,7 @@ const jsOnlyExtensions = [Extensions.JavaScript];
|
||||
const tsExtensions = [Extensions.TypeScript, Extensions.JavaScript];
|
||||
const tsPlusJsonExtensions = [...tsExtensions, Extensions.Json];
|
||||
const tsconfigExtensions = [Extensions.TSConfig];
|
||||
function nodeNextModuleNameResolverWorker(features: NodeResolutionFeatures, moduleName: string, containingFile: string, compilerOptions: CompilerOptions, host: ModuleResolutionHost, cache?: ModuleResolutionCache, redirectedReference?: ResolvedProjectReference, resolutionMode?: ModuleKind.CommonJS | ModuleKind.ESNext): ResolvedModuleWithFailedLookupLocations {
|
||||
function nodeNextModuleNameResolverWorker(features: NodeResolutionFeatures, moduleName: string, containingFile: string, compilerOptions: CompilerOptions, host: ModuleResolutionHost, cache?: ModuleResolutionCache, redirectedReference?: ResolvedProjectReference, resolutionMode?: ResolutionMode): ResolvedModuleWithFailedLookupLocations {
|
||||
const containingDirectory = getDirectoryPath(containingFile);
|
||||
|
||||
// es module file or cjs-like input file, use a variant of the legacy cjs resolver that supports the selected modern features
|
||||
|
||||
Reference in New Issue
Block a user