Skip to content

Commit

Permalink
TypeStrong#108 added collecting, logging and reporting errors during …
Browse files Browse the repository at this point in the history
…loader execution
  • Loading branch information
EvAlex committed Jan 21, 2016
1 parent 46e79c0 commit c19d54b
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,11 +341,11 @@ function ensureTypeScriptInstance(loaderOptions: LoaderOptions, loader: any): {
// We either load from memory or from disk
fileName = path.normalize(fileName);
var file = files[fileName];

if (!file) {
let text = readFile(fileName);
if (text == null) return;

file = files[fileName] = { version: 0, text }
}

Expand Down Expand Up @@ -465,7 +465,7 @@ function ensureTypeScriptInstance(loaderOptions: LoaderOptions, loader: any): {
pushArray(compilation.errors, formatErrors(errors, instance, {file: filePath}));
}
});

callback();
});

Expand Down Expand Up @@ -526,13 +526,13 @@ function loader(contents) {
if (!file) {
file = instance.files[filePath] = <TSFile>{ version: 0 };
}

if (file.text !== contents) {
file.version++;
file.text = contents;
instance.version++;
}

var outputText: string, sourceMapText: string, diagnostics: typescript.Diagnostic[] = [];

if (options.transpileOnly) {
Expand All @@ -549,11 +549,11 @@ function loader(contents) {
}
else {
let langService = instance.languageService;

// Make this file dependent on *all* definition files in the program
this.clearDependencies();
this.addDependency(filePath);

let allDefinitionFiles = Object.keys(instance.files).filter(filePath => /\.d\.ts$/.test(filePath));
allDefinitionFiles.forEach(this.addDependency.bind(this));
this._module.meta.tsLoaderDefinitionFileVersions = allDefinitionFiles.map(filePath => filePath+'@'+instance.files[filePath].version);
Expand All @@ -566,9 +566,15 @@ function loader(contents) {

var sourceMapFile = output.outputFiles.filter(file => !!file.name.match(/\.js(x?)\.map$/)).pop();
if (sourceMapFile) { sourceMapText = sourceMapFile.text }

var declarationFile = output.outputFiles.filter(file => !!file.name.match(/\.d.ts$/)).pop();
if (declarationFile) { this.emitFile(path.relative(this.options.context, declarationFile.name), declarationFile.text); }

// collect errors
diagnostics = diagnostics
.concat(langService.getSyntacticDiagnostics(filePath))
.concat(langService.getSemanticDiagnostics(filePath))
.concat(langService.getCompilerOptionsDiagnostics());
}

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

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

export = loader;

0 comments on commit c19d54b

Please sign in to comment.