Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

external modules resolve by node_modules and package.json-main #3147

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules/
!tests/**/node_modules/
built/*
tests/cases/*.js
tests/cases/*/*.js
Expand Down
14 changes: 3 additions & 11 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -859,17 +859,9 @@ module ts {
}
let fileName: string;
let sourceFile: SourceFile;
while (true) {
fileName = normalizePath(combinePaths(searchPath, moduleName));
sourceFile = forEach(supportedExtensions, extension => host.getSourceFile(fileName + extension));
if (sourceFile || isRelative) {
break;
}
let parentPath = getDirectoryPath(searchPath);
if (parentPath === searchPath) {
break;
}
searchPath = parentPath;
let resolvedName: string = host.resolveExternalModule(moduleName, searchPath);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Omit the type annotation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

if (resolvedName) {
sourceFile = host.getSourceFile(resolvedName);
}
if (sourceFile) {
if (sourceFile.symbol) {
Expand Down
71 changes: 59 additions & 12 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ module ts {
let commonSourceDirectory: string;
let diagnosticsProducingTypeChecker: TypeChecker;
let noDiagnosticsTypeChecker: TypeChecker;
let resolvedExternalModuleCache: Map<string> = {};

let start = new Date().getTime();

Expand Down Expand Up @@ -184,6 +185,7 @@ module ts {
getIdentifierCount: () => getDiagnosticsProducingTypeChecker().getIdentifierCount(),
getSymbolCount: () => getDiagnosticsProducingTypeChecker().getSymbolCount(),
getTypeCount: () => getDiagnosticsProducingTypeChecker().getTypeCount(),
resolveExternalModule
};
return program;

Expand Down Expand Up @@ -236,6 +238,60 @@ module ts {
emitTime += new Date().getTime() - start;
return emitResult;
}

function resolveExternalModule(moduleName: string, searchPath: string): string {
let cacheLookupName = moduleName + searchPath;
if (resolvedExternalModuleCache[cacheLookupName]) {
return resolvedExternalModuleCache[cacheLookupName];
}
if (resolvedExternalModuleCache[cacheLookupName] === '') {
return undefined;
}
function getNameIfExists(fileName: string): string {
if (sys.fileExists(fileName)) {
return fileName;
}
}
while (true) {
// Look at files by all extensions
let found = forEach(supportedExtensions,
extension => getNameIfExists(normalizePath(combinePaths(searchPath, moduleName)) + extension));
// Also look at all files by node_modules
if (!found) {
found = forEach(supportedExtensions,
extension => getNameIfExists(normalizePath(combinePaths(combinePaths(searchPath, "node_modules"), moduleName)) + extension));
}
// Also look at package.json's main in node_modules
if (!found) {
// If we found a package.json then look at its main field
let pkgJson = getNameIfExists(normalizePath(combinePaths(combinePaths(combinePaths(searchPath, "node_modules"), moduleName), "package.json")));
if (pkgJson) {
let pkgFile = JSON.parse(sys.readFile(pkgJson));
if (pkgFile.main) {
var indexFileName = removeFileExtension(combinePaths(getDirectoryPath(pkgJson), pkgFile.main));
found = forEach(supportedExtensions,
extension => getNameIfExists(indexFileName + extension))
}
}
}
// look at node_modules index
if (!found) {
found = forEach(supportedExtensions,
extension => getNameIfExists(normalizePath(combinePaths(combinePaths(combinePaths(searchPath, "node_modules"), moduleName), "index")) + extension));
}

// Finally cache and return or continue up the directory tree
if (found) {
return resolvedExternalModuleCache[cacheLookupName] = found;
}
let parentPath = getDirectoryPath(searchPath);
if (parentPath === searchPath) {
resolvedExternalModuleCache[cacheLookupName] = '';
return undefined;
}
searchPath = parentPath;
}
}

function getSourceFile(fileName: string) {
fileName = host.getCanonicalFileName(normalizeSlashes(fileName));
Expand Down Expand Up @@ -428,18 +484,9 @@ module ts {
if (moduleNameExpr && moduleNameExpr.kind === SyntaxKind.StringLiteral) {
let moduleNameText = (<LiteralExpression>moduleNameExpr).text;
if (moduleNameText) {
let searchPath = basePath;
let searchName: string;
while (true) {
searchName = normalizePath(combinePaths(searchPath, moduleNameText));
if (forEach(supportedExtensions, extension => findModuleSourceFile(searchName + extension, moduleNameExpr))) {
break;
}
let parentPath = getDirectoryPath(searchPath);
if (parentPath === searchPath) {
break;
}
searchPath = parentPath;
let resolvedName: string = resolveExternalModule(moduleNameText, getDirectoryPath(file.fileName));
if (resolvedName) {
findModuleSourceFile(resolvedName, moduleNameExpr);
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,8 @@ module ts {
* Gets a type checker that can be used to semantically analyze source fils in the program.
*/
getTypeChecker(): TypeChecker;

resolveExternalModule(moduleName: string, basePath: string): string;

/* @internal */ getCommonSourceDirectory(): string;

Expand Down Expand Up @@ -1133,6 +1135,7 @@ module ts {
export interface TypeCheckerHost {
getCompilerOptions(): CompilerOptions;

resolveExternalModule(moduleName: string, basePath: string): string;
getSourceFiles(): SourceFile[];
getSourceFile(fileName: string): SourceFile;
}
Expand Down