Skip to content

Commit

Permalink
Dont calculate version paths proactively as they may not be needed (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sheetalkamat committed Nov 29, 2022
1 parent 9a79aeb commit 9f93c67
Show file tree
Hide file tree
Showing 46 changed files with 675 additions and 709 deletions.
28 changes: 18 additions & 10 deletions src/compiler/moduleNameResolver.ts
Expand Up @@ -1818,7 +1818,7 @@ function tryFileLookup(fileName: string, onlyRecordFailures: boolean, state: Mod
function loadNodeModuleFromDirectory(extensions: Extensions, candidate: string, onlyRecordFailures: boolean, state: ModuleResolutionState, considerPackageJson = true) {
const packageInfo = considerPackageJson ? getPackageJsonInfo(candidate, onlyRecordFailures, state) : undefined;
const packageJsonContent = packageInfo && packageInfo.contents.packageJsonContent;
const versionPaths = packageInfo && packageInfo.contents.versionPaths;
const versionPaths = packageInfo && getVersionPathsOfPackageJsonInfo(packageInfo, state);
return withPackageId(packageInfo, loadNodeModuleFromDirectoryWorker(extensions, candidate, onlyRecordFailures, state, packageJsonContent, versionPaths));
}

Expand Down Expand Up @@ -1848,7 +1848,7 @@ export function getEntrypointsFromPackageJsonInfo(
/*onlyRecordFailures*/ false,
requireState,
packageJsonInfo.contents.packageJsonContent,
packageJsonInfo.contents.versionPaths);
getVersionPathsOfPackageJsonInfo(packageJsonInfo, requireState));
entrypoints = append(entrypoints, requireResolution?.path);

if (features & NodeResolutionFeatures.Exports && packageJsonInfo.contents.packageJsonContent.exports) {
Expand Down Expand Up @@ -1952,7 +1952,8 @@ export interface PackageJsonInfo {
/** @internal */
export interface PackageJsonInfoContents {
packageJsonContent: PackageJsonPathFields;
versionPaths: VersionPaths | undefined;
/** false: versionPaths are not present. undefined: not yet resolved */
versionPaths: VersionPaths | false | undefined;
/** false: resolved to nothing. undefined: not yet resolved */
resolvedEntrypoints: string[] | false | undefined;
}
Expand All @@ -1975,6 +1976,13 @@ export interface PackageJsonInfoContents {
return undefined;
}

function getVersionPathsOfPackageJsonInfo(packageJsonInfo: PackageJsonInfo, state: ModuleResolutionState): VersionPaths | undefined {
if (packageJsonInfo.contents.versionPaths === undefined) {
packageJsonInfo.contents.versionPaths = readPackageJsonTypesVersionPaths(packageJsonInfo.contents.packageJsonContent, state) || false;
}
return packageJsonInfo.contents.versionPaths || undefined;
}

/** @internal */
export function getPackageJsonInfo(packageDirectory: string, onlyRecordFailures: boolean, state: ModuleResolutionState): PackageJsonInfo | undefined {
const { host, traceEnabled } = state;
Expand Down Expand Up @@ -2005,8 +2013,7 @@ export function getPackageJsonInfo(packageDirectory: string, onlyRecordFailures:
if (traceEnabled) {
trace(host, Diagnostics.Found_package_json_at_0, packageJsonPath);
}
const versionPaths = readPackageJsonTypesVersionPaths(packageJsonContent, state);
const result: PackageJsonInfo = { packageDirectory, contents: { packageJsonContent, versionPaths, resolvedEntrypoints: undefined } };
const result: PackageJsonInfo = { packageDirectory, contents: { packageJsonContent, versionPaths: undefined, resolvedEntrypoints: undefined } };
state.packageJsonInfoCache?.setPackageJsonInfo(packageJsonPath, result);
state.affectingLocations.push(packageJsonPath);
return result;
Expand Down Expand Up @@ -2587,7 +2594,7 @@ function loadModuleFromSpecificNodeModulesDirectory(extensions: Extensions, modu
!nodeModulesDirectoryExists,
state,
packageInfo.contents.packageJsonContent,
packageInfo.contents.versionPaths
getVersionPathsOfPackageJsonInfo(packageInfo, state),
);
return withPackageId(packageInfo, fromDirectory);
}
Expand All @@ -2602,7 +2609,7 @@ function loadModuleFromSpecificNodeModulesDirectory(extensions: Extensions, modu
onlyRecordFailures,
state,
packageInfo && packageInfo.contents.packageJsonContent,
packageInfo && packageInfo.contents.versionPaths
packageInfo && getVersionPathsOfPackageJsonInfo(packageInfo, state),
);
if (
!pathAndExtension && packageInfo
Expand All @@ -2627,12 +2634,13 @@ function loadModuleFromSpecificNodeModulesDirectory(extensions: Extensions, modu
if (packageInfo && packageInfo.contents.packageJsonContent.exports && state.features & NodeResolutionFeatures.Exports) {
return loadModuleFromExports(packageInfo, extensions, combinePaths(".", rest), state, cache, redirectedReference)?.value;
}
if (rest !== "" && packageInfo && packageInfo.contents.versionPaths) {
const versionPaths = rest !== "" && packageInfo ? getVersionPathsOfPackageJsonInfo(packageInfo, state) : undefined;
if (versionPaths) {
if (state.traceEnabled) {
trace(state.host, Diagnostics.package_json_has_a_typesVersions_entry_0_that_matches_compiler_version_1_looking_for_a_pattern_to_match_module_name_2, packageInfo.contents.versionPaths.version, version, rest);
trace(state.host, Diagnostics.package_json_has_a_typesVersions_entry_0_that_matches_compiler_version_1_looking_for_a_pattern_to_match_module_name_2, versionPaths.version, version, rest);
}
const packageDirectoryExists = nodeModulesDirectoryExists && directoryProbablyExists(packageDirectory, state.host);
const fromPaths = tryLoadModuleUsingPaths(extensions, rest, packageDirectory, packageInfo.contents.versionPaths.paths, /*pathPatterns*/ undefined, loader, !packageDirectoryExists, state);
const fromPaths = tryLoadModuleUsingPaths(extensions, rest, packageDirectory, versionPaths.paths, /*pathPatterns*/ undefined, loader, !packageDirectoryExists, state);
if (fromPaths) {
return fromPaths.value;
}
Expand Down
Expand Up @@ -33,10 +33,10 @@
"Module resolution kind is not specified, using 'NodeJs'.",
"Loading module 'foo' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"Found 'package.json' at '/node_modules/a/node_modules/foo/package.json'.",
"'package.json' does not have a 'typesVersions' field.",
"File '/node_modules/a/node_modules/foo.ts' does not exist.",
"File '/node_modules/a/node_modules/foo.tsx' does not exist.",
"File '/node_modules/a/node_modules/foo.d.ts' does not exist.",
"'package.json' does not have a 'typesVersions' field.",
"'package.json' does not have a 'typings' field.",
"'package.json' does not have a 'types' field.",
"'package.json' does not have a 'main' field.",
Expand Down
Expand Up @@ -33,10 +33,10 @@
"Module resolution kind is not specified, using 'NodeJs'.",
"Loading module '@foo/bar' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"Found 'package.json' at '/node_modules/a/node_modules/@foo/bar/package.json'.",
"'package.json' does not have a 'typesVersions' field.",
"File '/node_modules/a/node_modules/@foo/bar.ts' does not exist.",
"File '/node_modules/a/node_modules/@foo/bar.tsx' does not exist.",
"File '/node_modules/a/node_modules/@foo/bar.d.ts' does not exist.",
"'package.json' does not have a 'typesVersions' field.",
"'package.json' does not have a 'typings' field.",
"'package.json' does not have a 'types' field.",
"'package.json' does not have a 'main' field.",
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/library-reference-11.trace.json
Expand Up @@ -4,8 +4,8 @@
"Looking up in 'node_modules' folder, initial location '/a/b'.",
"Directory '/a/b/node_modules' does not exist, skipping all lookups in it.",
"Found 'package.json' at '/a/node_modules/jquery/package.json'.",
"'package.json' does not have a 'typesVersions' field.",
"File '/a/node_modules/jquery.d.ts' does not exist.",
"'package.json' does not have a 'typesVersions' field.",
"'package.json' has 'typings' field 'jquery.d.ts' that references '/a/node_modules/jquery/jquery.d.ts'.",
"File '/a/node_modules/jquery/jquery.d.ts' exist - use it as a name resolution result.",
"Resolving real path for '/a/node_modules/jquery/jquery.d.ts', result '/a/node_modules/jquery/jquery.d.ts'.",
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/library-reference-12.trace.json
Expand Up @@ -4,8 +4,8 @@
"Looking up in 'node_modules' folder, initial location '/a/b'.",
"Directory '/a/b/node_modules' does not exist, skipping all lookups in it.",
"Found 'package.json' at '/a/node_modules/jquery/package.json'.",
"'package.json' does not have a 'typesVersions' field.",
"File '/a/node_modules/jquery.d.ts' does not exist.",
"'package.json' does not have a 'typesVersions' field.",
"'package.json' does not have a 'typings' field.",
"'package.json' has 'types' field 'dist/jquery.d.ts' that references '/a/node_modules/jquery/dist/jquery.d.ts'.",
"File '/a/node_modules/jquery/dist/jquery.d.ts' exist - use it as a name resolution result.",
Expand Down
Expand Up @@ -3,10 +3,10 @@
"Module resolution kind is not specified, using 'NodeJs'.",
"Loading module 'normalize.css' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"Found 'package.json' at '/node_modules/normalize.css/package.json'.",
"'package.json' does not have a 'typesVersions' field.",
"File '/node_modules/normalize.css.ts' does not exist.",
"File '/node_modules/normalize.css.tsx' does not exist.",
"File '/node_modules/normalize.css.d.ts' does not exist.",
"'package.json' does not have a 'typesVersions' field.",
"'package.json' does not have a 'typings' field.",
"'package.json' does not have a 'types' field.",
"'package.json' has 'main' field 'normalize.css' that references '/node_modules/normalize.css/normalize.css'.",
Expand Down
Expand Up @@ -3,10 +3,10 @@
"Module resolution kind is not specified, using 'NodeJs'.",
"Loading module 'foo' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"Found 'package.json' at '/node_modules/foo/package.json'.",
"'package.json' does not have a 'typesVersions' field.",
"File '/node_modules/foo.ts' does not exist.",
"File '/node_modules/foo.tsx' does not exist.",
"File '/node_modules/foo.d.ts' does not exist.",
"'package.json' does not have a 'typesVersions' field.",
"'package.json' does not have a 'typings' field.",
"'package.json' has 'types' field 'foo.js' that references '/node_modules/foo/foo.js'.",
"File '/node_modules/foo/foo.js' exist - use it as a name resolution result.",
Expand Down
Expand Up @@ -3,10 +3,10 @@
"Module resolution kind is not specified, using 'NodeJs'.",
"Loading module 'foo/bar' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"Found 'package.json' at '/node_modules/foo/bar/package.json'.",
"'package.json' does not have a 'typesVersions' field.",
"File '/node_modules/foo/bar.ts' does not exist.",
"File '/node_modules/foo/bar.tsx' does not exist.",
"File '/node_modules/foo/bar.d.ts' does not exist.",
"'package.json' does not have a 'typesVersions' field.",
"'package.json' does not have a 'typings' field.",
"'package.json' has 'types' field 'types.d.ts' that references '/node_modules/foo/bar/types.d.ts'.",
"File '/node_modules/foo/bar/types.d.ts' exist - use it as a name resolution result.",
Expand Down
Expand Up @@ -3,10 +3,10 @@
"Module resolution kind is not specified, using 'NodeJs'.",
"Loading module 'foo/@bar' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"Found 'package.json' at '/node_modules/foo/@bar/package.json'.",
"'package.json' does not have a 'typesVersions' field.",
"File '/node_modules/foo/@bar.ts' does not exist.",
"File '/node_modules/foo/@bar.tsx' does not exist.",
"File '/node_modules/foo/@bar.d.ts' does not exist.",
"'package.json' does not have a 'typesVersions' field.",
"'package.json' does not have a 'typings' field.",
"'package.json' has 'types' field 'types.d.ts' that references '/node_modules/foo/@bar/types.d.ts'.",
"File '/node_modules/foo/@bar/types.d.ts' exist - use it as a name resolution result.",
Expand Down
Expand Up @@ -3,10 +3,10 @@
"Module resolution kind is not specified, using 'NodeJs'.",
"Loading module '@foo/bar' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"Found 'package.json' at '/node_modules/@foo/bar/package.json'.",
"'package.json' does not have a 'typesVersions' field.",
"File '/node_modules/@foo/bar.ts' does not exist.",
"File '/node_modules/@foo/bar.tsx' does not exist.",
"File '/node_modules/@foo/bar.d.ts' does not exist.",
"'package.json' does not have a 'typesVersions' field.",
"'package.json' does not have a 'typings' field.",
"'package.json' has 'types' field 'types.d.ts' that references '/node_modules/@foo/bar/types.d.ts'.",
"File '/node_modules/@foo/bar/types.d.ts' exist - use it as a name resolution result.",
Expand Down
Expand Up @@ -3,10 +3,10 @@
"Module resolution kind is not specified, using 'NodeJs'.",
"Loading module 'foo' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"Found 'package.json' at '/node_modules/foo/package.json'.",
"'package.json' does not have a 'typesVersions' field.",
"File '/node_modules/foo.ts' does not exist.",
"File '/node_modules/foo.tsx' does not exist.",
"File '/node_modules/foo.d.ts' does not exist.",
"'package.json' does not have a 'typesVersions' field.",
"'package.json' does not have a 'typings' field.",
"'package.json' does not have a 'types' field.",
"'package.json' has 'main' field 'src/index.js' that references '/node_modules/foo/src/index.js'.",
Expand Down
@@ -1,10 +1,8 @@
[
"File '/node_modules/exports-and-types-versions/types/package.json' does not exist.",
"Found 'package.json' at '/node_modules/exports-and-types-versions/package.json'.",
"'package.json' has a 'typesVersions' field with version-specific path mappings.",
"File '/node_modules/just-types-versions/types/package.json' does not exist.",
"Found 'package.json' at '/node_modules/just-types-versions/package.json'.",
"'package.json' has a 'typesVersions' field with version-specific path mappings.",
"======== Resolving module 'exports-and-types-versions/foo' from '/main.cts'. ========",
"Module resolution kind is not specified, using 'Node16'.",
"Resolving in CJS mode with conditions 'node', 'require', 'types'.",
Expand Down Expand Up @@ -76,6 +74,7 @@
"File '/package.json' does not exist according to earlier cached lookups.",
"Loading module 'just-types-versions/foo' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration.",
"File '/node_modules/just-types-versions/package.json' exists according to earlier cached lookups.",
"'package.json' has a 'typesVersions' field with version-specific path mappings.",
"'package.json' has a 'typesVersions' entry '*' that matches compiler version '3.1.0-dev', looking for a pattern to match module name 'foo'.",
"Module name 'foo', matched pattern 'foo'.",
"Trying substitution './types/foo.d.ts', candidate module location: './types/foo.d.ts'.",
Expand Down
@@ -1,10 +1,8 @@
[
"File '/node_modules/exports-and-types-versions/types/package.json' does not exist.",
"Found 'package.json' at '/node_modules/exports-and-types-versions/package.json'.",
"'package.json' has a 'typesVersions' field with version-specific path mappings.",
"File '/node_modules/just-types-versions/types/package.json' does not exist.",
"Found 'package.json' at '/node_modules/just-types-versions/package.json'.",
"'package.json' has a 'typesVersions' field with version-specific path mappings.",
"======== Resolving module 'exports-and-types-versions/foo' from '/main.cts'. ========",
"Module resolution kind is not specified, using 'NodeNext'.",
"Resolving in CJS mode with conditions 'node', 'require', 'types'.",
Expand Down Expand Up @@ -76,6 +74,7 @@
"File '/package.json' does not exist according to earlier cached lookups.",
"Loading module 'just-types-versions/foo' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration.",
"File '/node_modules/just-types-versions/package.json' exists according to earlier cached lookups.",
"'package.json' has a 'typesVersions' field with version-specific path mappings.",
"'package.json' has a 'typesVersions' entry '*' that matches compiler version '3.1.0-dev', looking for a pattern to match module name 'foo'.",
"Module name 'foo', matched pattern 'foo'.",
"Trying substitution './types/foo.d.ts', candidate module location: './types/foo.d.ts'.",
Expand Down
@@ -1,6 +1,5 @@
[
"Found 'package.json' at 'tests/cases/conformance/node/package.json'.",
"'package.json' does not have a 'typesVersions' field.",
"======== Resolving module '#cjs' from 'tests/cases/conformance/node/index.ts'. ========",
"Module resolution kind is not specified, using 'Node16'.",
"Resolving in ESM mode with conditions 'node', 'import', 'types'.",
Expand Down
@@ -1,6 +1,5 @@
[
"Found 'package.json' at 'tests/cases/conformance/node/package.json'.",
"'package.json' does not have a 'typesVersions' field.",
"======== Resolving module '#cjs' from 'tests/cases/conformance/node/index.ts'. ========",
"Module resolution kind is not specified, using 'NodeNext'.",
"Resolving in ESM mode with conditions 'node', 'import', 'types'.",
Expand Down
@@ -1,13 +1,11 @@
[
"Found 'package.json' at 'tests/cases/conformance/node/package.json'.",
"'package.json' does not have a 'typesVersions' field.",
"======== Resolving module 'inner/cjs/index.cjs' from 'tests/cases/conformance/node/index.ts'. ========",
"Module resolution kind is not specified, using 'Node16'.",
"Resolving in ESM mode with conditions 'node', 'import', 'types'.",
"File 'tests/cases/conformance/node/package.json' exists according to earlier cached lookups.",
"Loading module 'inner/cjs/index.cjs' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration.",
"Found 'package.json' at 'tests/cases/conformance/node/node_modules/inner/package.json'.",
"'package.json' does not have a 'typesVersions' field.",
"Using 'exports' subpath './cjs/*.cjs' with target './index.cjs'.",
"File name 'tests/cases/conformance/node/node_modules/inner/index.cjs' has a '.cjs' extension - stripping it.",
"File 'tests/cases/conformance/node/node_modules/inner/index.cts' does not exist.",
Expand Down
@@ -1,13 +1,11 @@
[
"Found 'package.json' at 'tests/cases/conformance/node/package.json'.",
"'package.json' does not have a 'typesVersions' field.",
"======== Resolving module 'inner/cjs/index.cjs' from 'tests/cases/conformance/node/index.ts'. ========",
"Module resolution kind is not specified, using 'NodeNext'.",
"Resolving in ESM mode with conditions 'node', 'import', 'types'.",
"File 'tests/cases/conformance/node/package.json' exists according to earlier cached lookups.",
"Loading module 'inner/cjs/index.cjs' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration.",
"Found 'package.json' at 'tests/cases/conformance/node/node_modules/inner/package.json'.",
"'package.json' does not have a 'typesVersions' field.",
"Using 'exports' subpath './cjs/*.cjs' with target './index.cjs'.",
"File name 'tests/cases/conformance/node/node_modules/inner/index.cjs' has a '.cjs' extension - stripping it.",
"File 'tests/cases/conformance/node/node_modules/inner/index.cts' does not exist.",
Expand Down

0 comments on commit 9f93c67

Please sign in to comment.