Skip to content

Commit c19d54b

Browse files
committed
TypeStrong#108 added collecting, logging and reporting errors during loader execution
1 parent 46e79c0 commit c19d54b

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

index.ts

+23-9
Original file line numberDiff line numberDiff line change
@@ -341,11 +341,11 @@ function ensureTypeScriptInstance(loaderOptions: LoaderOptions, loader: any): {
341341
// We either load from memory or from disk
342342
fileName = path.normalize(fileName);
343343
var file = files[fileName];
344-
344+
345345
if (!file) {
346346
let text = readFile(fileName);
347347
if (text == null) return;
348-
348+
349349
file = files[fileName] = { version: 0, text }
350350
}
351351

@@ -465,7 +465,7 @@ function ensureTypeScriptInstance(loaderOptions: LoaderOptions, loader: any): {
465465
pushArray(compilation.errors, formatErrors(errors, instance, {file: filePath}));
466466
}
467467
});
468-
468+
469469
callback();
470470
});
471471

@@ -526,13 +526,13 @@ function loader(contents) {
526526
if (!file) {
527527
file = instance.files[filePath] = <TSFile>{ version: 0 };
528528
}
529-
529+
530530
if (file.text !== contents) {
531531
file.version++;
532532
file.text = contents;
533533
instance.version++;
534534
}
535-
535+
536536
var outputText: string, sourceMapText: string, diagnostics: typescript.Diagnostic[] = [];
537537

538538
if (options.transpileOnly) {
@@ -549,11 +549,11 @@ function loader(contents) {
549549
}
550550
else {
551551
let langService = instance.languageService;
552-
552+
553553
// Make this file dependent on *all* definition files in the program
554554
this.clearDependencies();
555555
this.addDependency(filePath);
556-
556+
557557
let allDefinitionFiles = Object.keys(instance.files).filter(filePath => /\.d\.ts$/.test(filePath));
558558
allDefinitionFiles.forEach(this.addDependency.bind(this));
559559
this._module.meta.tsLoaderDefinitionFileVersions = allDefinitionFiles.map(filePath => filePath+'@'+instance.files[filePath].version);
@@ -566,9 +566,15 @@ function loader(contents) {
566566

567567
var sourceMapFile = output.outputFiles.filter(file => !!file.name.match(/\.js(x?)\.map$/)).pop();
568568
if (sourceMapFile) { sourceMapText = sourceMapFile.text }
569-
569+
570570
var declarationFile = output.outputFiles.filter(file => !!file.name.match(/\.d.ts$/)).pop();
571571
if (declarationFile) { this.emitFile(path.relative(this.options.context, declarationFile.name), declarationFile.text); }
572+
573+
// collect errors
574+
diagnostics = diagnostics
575+
.concat(langService.getSyntacticDiagnostics(filePath))
576+
.concat(langService.getSemanticDiagnostics(filePath))
577+
.concat(langService.getCompilerOptionsDiagnostics());
572578
}
573579

574580
if (outputText == null) throw new Error(`Typescript emitted no output for ${filePath}`);
@@ -586,7 +592,15 @@ function loader(contents) {
586592
// treated as new
587593
this._module.meta.tsLoaderFileVersion = file.version;
588594

589-
callback(null, outputText, sourceMap)
595+
var errorDiagnostics = diagnostics.filter(d => d.category == typescript.DiagnosticCategory.Error ),
596+
err = errorDiagnostics.length > 0
597+
? errorDiagnostics.map(d => `ERROR in ${d.file.fileName}\n(${d.start}, ${d.length}): error TS${d.code}: ${d.messageText}`)
598+
: null;
599+
if (err && err.length > 0) {
600+
console.error('ts-loader detected following errors:');
601+
err.forEach((e, i) => console.error(` ${i + 1})`, e.replace('\n', '\n '), "\n"));
602+
}
603+
callback(err, outputText, sourceMap)
590604
}
591605

592606
export = loader;

0 commit comments

Comments
 (0)