Skip to content

Commit 694deb9

Browse files
filipesilvahansl
authored andcommitted
fix(@ngtools/webpack): show TS error message when there is no file
1 parent c873e03 commit 694deb9

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
lines changed

packages/@ngtools/webpack/src/loader.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -574,13 +574,16 @@ export function ngcLoader(this: LoaderContext & { _compilation: any }, source: s
574574
let message = '';
575575

576576
diagnostics.forEach(diagnostic => {
577-
const position = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
578-
579-
const fileName = diagnostic.file.fileName;
580-
const {line, character} = position;
581-
582577
const messageText = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
583-
message += `${fileName} (${line + 1},${character + 1}): ${messageText}\n`;
578+
579+
if (diagnostic.file) {
580+
const position = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
581+
const fileName = diagnostic.file.fileName;
582+
const {line, character} = position;
583+
message += `${fileName} (${line + 1},${character + 1}): ${messageText}\n`;
584+
} else {
585+
message += `${messageText}\n`;
586+
}
584587
});
585588
throw new Error(message);
586589
}

packages/@ngtools/webpack/src/plugin.ts

+27-11
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,14 @@ export class AotPlugin implements Tapable {
122122
const configResult = ts.readConfigFile(this._tsConfigPath, ts.sys.readFile);
123123
if (configResult.error) {
124124
const diagnostic = configResult.error;
125-
const {line, character} = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
126125
const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
127-
throw new Error(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message})`);
126+
127+
if (diagnostic.file) {
128+
const {line, character} = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
129+
throw new Error(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message})`);
130+
} else {
131+
throw new Error(message);
132+
}
128133
}
129134

130135
const tsConfigJson = configResult.config;
@@ -422,14 +427,20 @@ export class AotPlugin implements Tapable {
422427

423428
if (diagnostics.length > 0) {
424429
diagnostics.forEach(diagnostic => {
425-
const position = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
430+
const messageText = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
431+
let message;
426432

427-
const sourceText = diagnostic.file.getFullText();
428-
let {line, character, fileName} = this._translateSourceMap(sourceText,
429-
diagnostic.file.fileName, position);
433+
if (diagnostic.file) {
434+
const position = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
430435

431-
const messageText = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
432-
const message = `${fileName} (${line + 1},${character + 1}): ${messageText}`;
436+
const sourceText = diagnostic.file.getFullText();
437+
let {line, character, fileName} = this._translateSourceMap(sourceText,
438+
diagnostic.file.fileName, position);
439+
440+
message = `${fileName} (${line + 1},${character + 1}): ${messageText}`;
441+
} else {
442+
message = messageText;
443+
}
433444

434445
switch (diagnostic.category) {
435446
case ts.DiagnosticCategory.Error:
@@ -501,10 +512,15 @@ export class AotPlugin implements Tapable {
501512
if (diagnostics.length > 0) {
502513
const message = diagnostics
503514
.map(diagnostic => {
504-
const {line, character} = diagnostic.file.getLineAndCharacterOfPosition(
505-
diagnostic.start);
506515
const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
507-
return `${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message})`;
516+
517+
if (diagnostic.file) {
518+
const {line, character} = diagnostic.file.getLineAndCharacterOfPosition(
519+
diagnostic.start);
520+
return `${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message})`;
521+
} else {
522+
return message;
523+
}
508524
})
509525
.join('\n');
510526

0 commit comments

Comments
 (0)