diff --git a/src/compiler/program.ts b/src/compiler/program.ts index fb74f1dcaab33..5cb3b8dad7e8c 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -1097,7 +1097,7 @@ namespace ts { const modulesWithElidedImports: Map = {}; // Track source files that are JavaScript files found by searching under node_modules, as these shouldn't be compiled. - const jsFilesFoundSearchingNodeModules: Map = {}; + const sourceFilesFoundSearchingNodeModules: Map = {}; const start = new Date().getTime(); @@ -1378,7 +1378,7 @@ namespace ts { getSourceFile: program.getSourceFile, getSourceFileByPath: program.getSourceFileByPath, getSourceFiles: program.getSourceFiles, - getFilesFromNodeModules: () => jsFilesFoundSearchingNodeModules, + isSourceFileFromNodeModules: (file: SourceFile) => !!lookUp(sourceFilesFoundSearchingNodeModules, file.path), writeFile: writeFileCallback || ( (fileName, data, writeByteOrderMark, onError, sourceFiles) => host.writeFile(fileName, data, writeByteOrderMark, onError, sourceFiles)), isEmitBlocked, @@ -2066,15 +2066,17 @@ namespace ts { // - noResolve is falsy // - module name comes from the list of imports // - it's not a top level JavaScript module that exceeded the search max - const isJsFileUnderNodeModules = resolution && resolution.isExternalLibraryImport && - hasJavaScriptFileExtension(resolution.resolvedFileName); + const isFromNodeModulesSearch = resolution && resolution.isExternalLibraryImport; + const isJsFileFromNodeModules = isFromNodeModulesSearch && hasJavaScriptFileExtension(resolution.resolvedFileName); - if (isJsFileUnderNodeModules) { - jsFilesFoundSearchingNodeModules[resolvedPath] = true; + if (isFromNodeModulesSearch) { + sourceFilesFoundSearchingNodeModules[resolvedPath] = true; + } + if (isJsFileFromNodeModules) { currentNodeModulesJsDepth++; } - const elideImport = isJsFileUnderNodeModules && currentNodeModulesJsDepth > maxNodeModulesJsDepth; + const elideImport = isJsFileFromNodeModules && currentNodeModulesJsDepth > maxNodeModulesJsDepth; const shouldAddFile = resolution && !options.noResolve && i < file.imports.length && !elideImport; if (elideImport) { @@ -2089,7 +2091,7 @@ namespace ts { file.imports[i].end); } - if (isJsFileUnderNodeModules) { + if (isJsFileFromNodeModules) { currentNodeModulesJsDepth--; } } diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index e8f2832cd592e..e4111e3f92563 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -36,7 +36,7 @@ namespace ts { getSourceFiles(): SourceFile[]; /* @internal */ - getFilesFromNodeModules(): Map; + isSourceFileFromNodeModules(file: SourceFile): boolean; getCommonSourceDirectory(): string; getCanonicalFileName(fileName: string): string; @@ -2277,10 +2277,9 @@ namespace ts { } else { const sourceFiles = targetSourceFile === undefined ? host.getSourceFiles() : [targetSourceFile]; - const nodeModulesFiles = host.getFilesFromNodeModules(); for (const sourceFile of sourceFiles) { // Don't emit if source file is a declaration file, or was located under node_modules - if (!isDeclarationFile(sourceFile) && !lookUp(nodeModulesFiles, sourceFile.path)) { + if (!isDeclarationFile(sourceFile) && !host.isSourceFileFromNodeModules(sourceFile)) { onSingleFileEmit(host, sourceFile); } } @@ -2314,10 +2313,9 @@ namespace ts { function onBundledEmit(host: EmitHost) { // Can emit only sources that are not declaration file and are either non module code or module with // --module or --target es6 specified. Files included by searching under node_modules are also not emitted. - const nodeModulesFiles = host.getFilesFromNodeModules(); const bundledSources = filter(host.getSourceFiles(), sourceFile => !isDeclarationFile(sourceFile) && - !lookUp(nodeModulesFiles, sourceFile.path) && + !host.isSourceFileFromNodeModules(sourceFile) && (!isExternalModule(sourceFile) || !!getEmitModuleKind(options))); if (bundledSources.length) { diff --git a/tests/baselines/reference/moduleAugmentationInDependency2.js b/tests/baselines/reference/moduleAugmentationInDependency2.js index 381f1e72d8f31..0a5d83695a34f 100644 --- a/tests/baselines/reference/moduleAugmentationInDependency2.js +++ b/tests/baselines/reference/moduleAugmentationInDependency2.js @@ -8,8 +8,6 @@ export {}; //// [app.ts] import "A" -//// [index.js] -"use strict"; //// [app.js] "use strict"; require("A"); diff --git a/tests/baselines/reference/pathMappingBasedModuleResolution5_node.js b/tests/baselines/reference/pathMappingBasedModuleResolution5_node.js index 1958800f91893..e4440299cc75c 100644 --- a/tests/baselines/reference/pathMappingBasedModuleResolution5_node.js +++ b/tests/baselines/reference/pathMappingBasedModuleResolution5_node.js @@ -31,9 +31,6 @@ exports.x = 1; //// [file2.js] "use strict"; exports.y = 1; -//// [file4.js] -"use strict"; -exports.z1 = 1; //// [file1.js] "use strict"; var file1_1 = require("folder2/file1");