Skip to content

Commit d8e3bd9

Browse files
committed
Added emitHost method to return source from node modules
1 parent de559fb commit d8e3bd9

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

src/compiler/program.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,10 +1361,8 @@ namespace ts {
13611361
getNewLine: () => host.getNewLine(),
13621362
getSourceFile: program.getSourceFile,
13631363
getSourceFileByPath: program.getSourceFileByPath,
1364-
getSourceFiles: () => filter(program.getSourceFiles(),
1365-
// Remove JavaScript files found by searching node_modules from the source files to emit
1366-
sourceFile => !lookUp(jsFilesFoundSearchingNodeModules, sourceFile.path)
1367-
),
1364+
getSourceFiles: program.getSourceFiles,
1365+
getFilesFromNodeModules: () => jsFilesFoundSearchingNodeModules,
13681366
writeFile: writeFileCallback || (
13691367
(fileName, data, writeByteOrderMark, onError, sourceFiles) => host.writeFile(fileName, data, writeByteOrderMark, onError, sourceFiles)),
13701368
isEmitBlocked,

src/compiler/utilities.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ namespace ts {
3434

3535
export interface EmitHost extends ScriptReferenceHost {
3636
getSourceFiles(): SourceFile[];
37+
getFilesFromNodeModules(): Map<boolean>;
3738

3839
getCommonSourceDirectory(): string;
3940
getCanonicalFileName(fileName: string): string;
@@ -2274,9 +2275,10 @@ namespace ts {
22742275
}
22752276
else {
22762277
const sourceFiles = targetSourceFile === undefined ? host.getSourceFiles() : [targetSourceFile];
2278+
const nodeModulesFiles = host.getFilesFromNodeModules();
22772279
for (const sourceFile of sourceFiles) {
2278-
// Don't emit if source file is a declaration file
2279-
if (!isDeclarationFile(sourceFile)) {
2280+
// Don't emit if source file is a declaration file, or was located under node_modules
2281+
if (!isDeclarationFile(sourceFile) && !lookUp(nodeModulesFiles, sourceFile.path)) {
22802282
onSingleFileEmit(host, sourceFile);
22812283
}
22822284
}
@@ -2308,11 +2310,14 @@ namespace ts {
23082310
}
23092311

23102312
function onBundledEmit(host: EmitHost) {
2311-
// Can emit only sources that are not declaration file and are either non module code or module with --module or --target es6 specified
2313+
// Can emit only sources that are not declaration file and are either non module code or module with
2314+
// --module or --target es6 specified. Files included by searching under node_modules are also not emitted.
2315+
const nodeModulesFiles = host.getFilesFromNodeModules();
23122316
const bundledSources = filter(host.getSourceFiles(),
2313-
sourceFile => !isDeclarationFile(sourceFile) && // Not a declaration file
2314-
(!isExternalModule(sourceFile) || // non module file
2315-
!!getEmitModuleKind(options))); // module that can emit - note falsy value from getEmitModuleKind means the module kind that shouldn't be emitted
2317+
sourceFile => !isDeclarationFile(sourceFile) &&
2318+
!lookUp(nodeModulesFiles, sourceFile.path) &&
2319+
(!isExternalModule(sourceFile) ||
2320+
!!getEmitModuleKind(options)));
23162321
if (bundledSources.length) {
23172322
const jsFilePath = options.outFile || options.out;
23182323
const emitFileNames: EmitFileNames = {

0 commit comments

Comments
 (0)