Merge pull request #26458 from Microsoft/pathMappingResultsToNodeModules

When path mapping results to file in node_modules, mark it as external library
This commit is contained in:
Sheetal Nandi 2018-08-15 15:28:50 -07:00 committed by GitHub
commit 969b46e44b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 59 additions and 5 deletions

View file

@ -769,7 +769,7 @@ namespace ts {
const loader: ResolutionKindSpecificLoader = (extensions, candidate, failedLookupLocations, onlyRecordFailures, state) => nodeLoadModuleByRelativeName(extensions, candidate, failedLookupLocations, onlyRecordFailures, state, /*considerPackageJson*/ true);
const resolved = tryLoadModuleUsingOptionalResolutionSettings(extensions, moduleName, containingDirectory, loader, failedLookupLocations, state);
if (resolved) {
return toSearchResult({ resolved, isExternalLibraryImport: false });
return toSearchResult({ resolved, isExternalLibraryImport: stringContains(resolved.path, nodeModulesPathPart) });
}
if (!isExternalModuleNameRelative(moduleName)) {
@ -843,7 +843,8 @@ namespace ts {
return loadNodeModuleFromDirectory(extensions, candidate, failedLookupLocations, onlyRecordFailures, state, considerPackageJson);
}
const nodeModulesPathPart = "/node_modules/";
/*@internal*/
export const nodeModulesPathPart = "/node_modules/";
/**
* This will be called on the successfully resolved path from `loadModuleFromFile`.

View file

@ -401,7 +401,7 @@ namespace ts.moduleSpecifiers {
partEnd = fullPath.indexOf("/", partStart + 1);
switch (state) {
case States.BeforeNodeModules:
if (fullPath.indexOf("/node_modules/", partStart) === partStart) {
if (fullPath.indexOf(nodeModulesPathPart, partStart) === partStart) {
topLevelNodeModulesIndex = partStart;
topLevelPackageNameIndex = partEnd;
state = States.NodeModules;
@ -418,7 +418,7 @@ namespace ts.moduleSpecifiers {
}
break;
case States.PackageContent:
if (fullPath.indexOf("/node_modules/", partStart) === partStart) {
if (fullPath.indexOf(nodeModulesPathPart, partStart) === partStart) {
state = States.NodeModules;
}
else {

View file

@ -419,7 +419,7 @@ namespace ts {
function getDirectoryToWatchFromFailedLookupLocationDirectory(dir: string, dirPath: Path) {
// If directory path contains node module, get the most parent node_modules directory for watching
while (stringContains(dirPath, "/node_modules/")) {
while (stringContains(dirPath, nodeModulesPathPart)) {
dir = getDirectoryPath(dir);
dirPath = getDirectoryPath(dirPath);
}

View file

@ -0,0 +1,12 @@
//// [tests/cases/compiler/pathMappingBasedModuleResolution_withExtension_MapedToNodeModules.ts] ////
//// [foobar.js]
module.exports = { a: 10 };
//// [a.ts]
import foobar from "foo/bar/foobar.js";
//// [/bin/a.js]
"use strict";
exports.__esModule = true;

View file

@ -0,0 +1,4 @@
=== /a.ts ===
import foobar from "foo/bar/foobar.js";
>foobar : Symbol(foobar, Decl(a.ts, 0, 6))

View file

@ -0,0 +1,10 @@
[
"======== Resolving module 'foo/bar/foobar.js' from '/a.ts'. ========",
"Module resolution kind is not specified, using 'NodeJs'.",
"'baseUrl' option is set to '/', using this value to resolve non-relative module name 'foo/bar/foobar.js'.",
"'paths' option is specified, looking for a pattern to match module name 'foo/bar/foobar.js'.",
"Module name 'foo/bar/foobar.js', matched pattern '*'.",
"Trying substitution 'node_modules/*', candidate module location: 'node_modules/foo/bar/foobar.js'.",
"File '/node_modules/foo/bar/foobar.js' exist - use it as a name resolution result.",
"======== Module name 'foo/bar/foobar.js' was successfully resolved to '/node_modules/foo/bar/foobar.js'. ========"
]

View file

@ -0,0 +1,4 @@
=== /a.ts ===
import foobar from "foo/bar/foobar.js";
>foobar : any

View file

@ -0,0 +1,23 @@
// @noImplicitReferences: true
// @traceResolution: true
// @allowJs: true
// @esModuleInterop: true
// @fullEmitPaths: true
// @Filename: /node_modules/foo/bar/foobar.js
module.exports = { a: 10 };
// @Filename: /a.ts
import foobar from "foo/bar/foobar.js";
// @Filename: /tsconfig.json
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"*": ["node_modules/*", "src/types"]
},
"allowJs": true,
"outDir": "bin"
}
}