From d8e3bd99e8430d85432066fe4e7fb00819fabb95 Mon Sep 17 00:00:00 2001 From: Bill Ticehurst Date: Mon, 27 Jun 2016 16:32:29 -0700 Subject: [PATCH] Added emitHost method to return source from node modules --- src/compiler/program.ts | 6 ++---- src/compiler/utilities.ts | 17 +++++++++++------ 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/compiler/program.ts b/src/compiler/program.ts index cee0936faf..fdc39e1eb3 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -1361,10 +1361,8 @@ namespace ts { getNewLine: () => host.getNewLine(), getSourceFile: program.getSourceFile, getSourceFileByPath: program.getSourceFileByPath, - getSourceFiles: () => filter(program.getSourceFiles(), - // Remove JavaScript files found by searching node_modules from the source files to emit - sourceFile => !lookUp(jsFilesFoundSearchingNodeModules, sourceFile.path) - ), + getSourceFiles: program.getSourceFiles, + getFilesFromNodeModules: () => jsFilesFoundSearchingNodeModules, writeFile: writeFileCallback || ( (fileName, data, writeByteOrderMark, onError, sourceFiles) => host.writeFile(fileName, data, writeByteOrderMark, onError, sourceFiles)), isEmitBlocked, diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 6daf090348..571c49bf44 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -34,6 +34,7 @@ namespace ts { export interface EmitHost extends ScriptReferenceHost { getSourceFiles(): SourceFile[]; + getFilesFromNodeModules(): Map; getCommonSourceDirectory(): string; getCanonicalFileName(fileName: string): string; @@ -2274,9 +2275,10 @@ 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 - if (!isDeclarationFile(sourceFile)) { + // Don't emit if source file is a declaration file, or was located under node_modules + if (!isDeclarationFile(sourceFile) && !lookUp(nodeModulesFiles, sourceFile.path)) { onSingleFileEmit(host, sourceFile); } } @@ -2308,11 +2310,14 @@ 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 + // 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) && // Not a declaration file - (!isExternalModule(sourceFile) || // non module file - !!getEmitModuleKind(options))); // module that can emit - note falsy value from getEmitModuleKind means the module kind that shouldn't be emitted + sourceFile => !isDeclarationFile(sourceFile) && + !lookUp(nodeModulesFiles, sourceFile.path) && + (!isExternalModule(sourceFile) || + !!getEmitModuleKind(options))); if (bundledSources.length) { const jsFilePath = options.outFile || options.out; const emitFileNames: EmitFileNames = {