Skip to content

Commit be02f96

Browse files
author
Yui T
committed
Fix compileOnSave with external module
1 parent 85fd141 commit be02f96

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

src/compiler/emitter.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3228,21 +3228,27 @@ module ts {
32283228
}
32293229

32303230
if (targetSourceFile === undefined) {
3231+
// No targetSourceFile is specified (i.e. calling emitter from batch compiler)
32313232
forEach(program.getSourceFiles(), sourceFile => {
32323233
if (shouldEmitToOwnFile(sourceFile, compilerOptions)) {
32333234
var jsFilePath = getOwnEmitOutputFilePath(sourceFile, ".js");
32343235
emitFile(jsFilePath, sourceFile);
32353236
}
32363237
});
3237-
}
3238-
else {
3239-
// Emit only one file specified in targetFilename. This is mainly used in compilerOnSave feature
3240-
var jsFilePath = getOwnEmitOutputFilePath(targetSourceFile, ".js");
3241-
emitFile(jsFilePath, targetSourceFile);
3242-
}
32433238

3244-
if (compilerOptions.out) {
3245-
emitFile(compilerOptions.out);
3239+
if (compilerOptions.out) {
3240+
emitFile(compilerOptions.out);
3241+
}
3242+
} else {
3243+
// targetSourceFile is specified (i.e. calling emitter from language service)
3244+
if (shouldEmitToOwnFile(targetSourceFile, compilerOptions)) {
3245+
// If shouldEmitToOwnFile is true or targetSouceFile is an external module file, then emit targetSourceFile in its own output file
3246+
var jsFilePath = getOwnEmitOutputFilePath(targetSourceFile, ".js");
3247+
emitFile(jsFilePath, targetSourceFile);
3248+
} else {
3249+
// If shouldEmitToOwnFile is false, then emit all, non-external-module file, into one single output file
3250+
emitFile(compilerOptions.out);
3251+
}
32463252
}
32473253

32483254
// Sort and make the unique list of diagnostics

src/services/services.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3881,7 +3881,8 @@ module ts {
38813881
filename = TypeScript.switchToForwardSlashes(filename);
38823882
var compilerOptions = program.getCompilerOptions();
38833883
var targetSourceFile = program.getSourceFile(filename); // Current selected file to be output
3884-
var emitToSingleFile = ts.shouldEmitToOwnFile(targetSourceFile, compilerOptions);
3884+
// If --out flag is not specified, shouldEmitToOwnFile is true. Otherwise shouldEmitToOwnFile is false.
3885+
var shouldEmitToOwnFile = ts.shouldEmitToOwnFile(targetSourceFile, compilerOptions);
38853886
var emitDeclaration = compilerOptions.declaration;
38863887
var emitOutput: EmitOutput = {
38873888
outputFiles: [],
@@ -3902,7 +3903,7 @@ module ts {
39023903
var syntacticDiagnostics: Diagnostic[] = [];
39033904
var containSyntacticErrors = false;
39043905

3905-
if (emitToSingleFile) {
3906+
if (shouldEmitToOwnFile) {
39063907
// Check only the file we want to emit
39073908
containSyntacticErrors = containErrors(program.getDiagnostics(targetSourceFile));
39083909
} else {
@@ -3929,7 +3930,7 @@ module ts {
39293930
// Perform semantic and force a type check before emit to ensure that all symbols are updated
39303931
// EmitFiles will report if there is an error from TypeChecker and Emitter
39313932
// Depend whether we will have to emit into a single file or not either emit only selected file in the project, emit all files into a single file
3932-
var emitFilesResult = emitToSingleFile ? getFullTypeCheckChecker().emitFiles(targetSourceFile) : getFullTypeCheckChecker().emitFiles();
3933+
var emitFilesResult = getFullTypeCheckChecker().emitFiles(targetSourceFile);;
39333934
emitOutput.emitOutputStatus = emitFilesResult.emitResultStatus;
39343935

39353936
// Reset writer back to undefined to make sure that we produce an error message if CompilerHost.writeFile method is called when we are not in getEmitOutput

0 commit comments

Comments
 (0)