Skip to content

Commit 969b46e

Browse files
authored
Merge pull request microsoft#26458 from Microsoft/pathMappingResultsToNodeModules
When path mapping results to file in node_modules, mark it as external library
2 parents cc67ce1 + fea1667 commit 969b46e

8 files changed

+59
-5
lines changed

src/compiler/moduleNameResolver.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ namespace ts {
769769
const loader: ResolutionKindSpecificLoader = (extensions, candidate, failedLookupLocations, onlyRecordFailures, state) => nodeLoadModuleByRelativeName(extensions, candidate, failedLookupLocations, onlyRecordFailures, state, /*considerPackageJson*/ true);
770770
const resolved = tryLoadModuleUsingOptionalResolutionSettings(extensions, moduleName, containingDirectory, loader, failedLookupLocations, state);
771771
if (resolved) {
772-
return toSearchResult({ resolved, isExternalLibraryImport: false });
772+
return toSearchResult({ resolved, isExternalLibraryImport: stringContains(resolved.path, nodeModulesPathPart) });
773773
}
774774

775775
if (!isExternalModuleNameRelative(moduleName)) {
@@ -843,7 +843,8 @@ namespace ts {
843843
return loadNodeModuleFromDirectory(extensions, candidate, failedLookupLocations, onlyRecordFailures, state, considerPackageJson);
844844
}
845845

846-
const nodeModulesPathPart = "/node_modules/";
846+
/*@internal*/
847+
export const nodeModulesPathPart = "/node_modules/";
847848

848849
/**
849850
* This will be called on the successfully resolved path from `loadModuleFromFile`.

src/compiler/moduleSpecifiers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ namespace ts.moduleSpecifiers {
401401
partEnd = fullPath.indexOf("/", partStart + 1);
402402
switch (state) {
403403
case States.BeforeNodeModules:
404-
if (fullPath.indexOf("/node_modules/", partStart) === partStart) {
404+
if (fullPath.indexOf(nodeModulesPathPart, partStart) === partStart) {
405405
topLevelNodeModulesIndex = partStart;
406406
topLevelPackageNameIndex = partEnd;
407407
state = States.NodeModules;
@@ -418,7 +418,7 @@ namespace ts.moduleSpecifiers {
418418
}
419419
break;
420420
case States.PackageContent:
421-
if (fullPath.indexOf("/node_modules/", partStart) === partStart) {
421+
if (fullPath.indexOf(nodeModulesPathPart, partStart) === partStart) {
422422
state = States.NodeModules;
423423
}
424424
else {

src/compiler/resolutionCache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ namespace ts {
419419

420420
function getDirectoryToWatchFromFailedLookupLocationDirectory(dir: string, dirPath: Path) {
421421
// If directory path contains node module, get the most parent node_modules directory for watching
422-
while (stringContains(dirPath, "/node_modules/")) {
422+
while (stringContains(dirPath, nodeModulesPathPart)) {
423423
dir = getDirectoryPath(dir);
424424
dirPath = getDirectoryPath(dirPath);
425425
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//// [tests/cases/compiler/pathMappingBasedModuleResolution_withExtension_MapedToNodeModules.ts] ////
2+
3+
//// [foobar.js]
4+
module.exports = { a: 10 };
5+
6+
//// [a.ts]
7+
import foobar from "foo/bar/foobar.js";
8+
9+
10+
//// [/bin/a.js]
11+
"use strict";
12+
exports.__esModule = true;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=== /a.ts ===
2+
import foobar from "foo/bar/foobar.js";
3+
>foobar : Symbol(foobar, Decl(a.ts, 0, 6))
4+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[
2+
"======== Resolving module 'foo/bar/foobar.js' from '/a.ts'. ========",
3+
"Module resolution kind is not specified, using 'NodeJs'.",
4+
"'baseUrl' option is set to '/', using this value to resolve non-relative module name 'foo/bar/foobar.js'.",
5+
"'paths' option is specified, looking for a pattern to match module name 'foo/bar/foobar.js'.",
6+
"Module name 'foo/bar/foobar.js', matched pattern '*'.",
7+
"Trying substitution 'node_modules/*', candidate module location: 'node_modules/foo/bar/foobar.js'.",
8+
"File '/node_modules/foo/bar/foobar.js' exist - use it as a name resolution result.",
9+
"======== Module name 'foo/bar/foobar.js' was successfully resolved to '/node_modules/foo/bar/foobar.js'. ========"
10+
]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=== /a.ts ===
2+
import foobar from "foo/bar/foobar.js";
3+
>foobar : any
4+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// @noImplicitReferences: true
2+
// @traceResolution: true
3+
// @allowJs: true
4+
// @esModuleInterop: true
5+
// @fullEmitPaths: true
6+
7+
// @Filename: /node_modules/foo/bar/foobar.js
8+
module.exports = { a: 10 };
9+
10+
// @Filename: /a.ts
11+
import foobar from "foo/bar/foobar.js";
12+
13+
// @Filename: /tsconfig.json
14+
{
15+
"compilerOptions": {
16+
"baseUrl": ".",
17+
"paths": {
18+
"*": ["node_modules/*", "src/types"]
19+
},
20+
"allowJs": true,
21+
"outDir": "bin"
22+
}
23+
}

0 commit comments

Comments
 (0)