Skip to content
This repository was archived by the owner on Aug 4, 2021. It is now read-only.

Commit 37bfc90

Browse files
committed
Add diagnostics
1 parent 461e0cd commit 37bfc90

File tree

1 file changed

+41
-9
lines changed

1 file changed

+41
-9
lines changed

index.js

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var typescript = require( 'typescript' );
1+
var ts = require( 'typescript' );
22
var createFilter = require( 'rollup-pluginutils' ).createFilter;
33

44
var assign = Object.assign || function ( target, source ) {
@@ -9,25 +9,57 @@ var assign = Object.assign || function ( target, source ) {
99
return target;
1010
};
1111

12-
module.exports = function ( options ) {
12+
function goodErrors ( diagnostic ) {
13+
// All errors except `Cannot compile modules into 'es6' when targeting 'ES5' or lower.`
14+
return diagnostic.code !== 1204;
15+
}
16+
17+
module.exports = function typescript ( options ) {
1318
options = assign( {}, options || {} );
1419

15-
var filter = createFilter( options.include, options.exclude );
20+
var filter = createFilter( options.include || [ '*.ts+(|x)', '**/*.ts+(|x)' ], options.exclude );
1621
delete options.include;
1722
delete options.exclude;
1823

24+
options = assign( {
25+
target: ts.ScriptTarget.ES5,
26+
module: ts.ModuleKind.ES6,
27+
sourceMap: true
28+
}, options );
29+
1930
return {
2031
transform: function ( code, id ) {
2132
if ( !filter( id ) ) return null;
2233

23-
var transformed = typescript.transpileModule( code, {
24-
compilerOptions: assign( {
25-
target: typescript.ScriptTarget.ES5,
26-
module: typescript.ModuleKind.ES6,
27-
sourceMap: true
28-
}, options )
34+
var transformed = ts.transpileModule( code, {
35+
reportDiagnostics: true,
36+
compilerOptions: options
37+
});
38+
39+
var diagnostics = transformed.diagnostics.filter( goodErrors );
40+
var fatalError = false;
41+
42+
diagnostics.forEach(function ( diagnostic ) {
43+
var message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
44+
45+
if ( diagnostic.file ) {
46+
var pos = diagnostic.file.getLineAndCharacterOfPosition( diagnostic.start );
47+
48+
console.error( diagnostic.file.fileName +
49+
'(' + (pos.line + 1) + ',' + (pos.character + 1) + '): error ES' +
50+
diagnostic.code + ': ' + message );
51+
} else {
52+
console.error( 'Error: ' + message );
53+
}
54+
55+
if ( diagnostic.category === ts.DiagnosticCategory.Error ) {
56+
fatalError = true;
57+
}
2958
});
3059

60+
if ( fatalError ) {
61+
throw new Error( 'There were TypeScript errors transpiling "' + id + '"' );
62+
}
3163

3264
return {
3365
code: transformed.outputText,

0 commit comments

Comments
 (0)