Skip to content

Commit

Permalink
Don't emit source files found under node_modules
Browse files Browse the repository at this point in the history
  • Loading branch information
billti committed Jun 29, 2016
1 parent b524795 commit 5f8cf1a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 18 deletions.
18 changes: 10 additions & 8 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1097,7 +1097,7 @@ namespace ts {
const modulesWithElidedImports: Map<boolean> = {};

// Track source files that are JavaScript files found by searching under node_modules, as these shouldn't be compiled.
const jsFilesFoundSearchingNodeModules: Map<boolean> = {};
const sourceFilesFoundSearchingNodeModules: Map<boolean> = {};

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

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand All @@ -2089,7 +2091,7 @@ namespace ts {
file.imports[i].end);
}

if (isJsFileUnderNodeModules) {
if (isJsFileFromNodeModules) {
currentNodeModulesJsDepth--;
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace ts {
getSourceFiles(): SourceFile[];

/* @internal */
getFilesFromNodeModules(): Map<boolean>;
isSourceFileFromNodeModules(file: SourceFile): boolean;

getCommonSourceDirectory(): string;
getCanonicalFileName(fileName: string): string;
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 0 additions & 2 deletions tests/baselines/reference/moduleAugmentationInDependency2.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ export {};
//// [app.ts]
import "A"

//// [index.js]
"use strict";
//// [app.js]
"use strict";
require("A");
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 5f8cf1a

Please sign in to comment.