1
- var typescript = require ( 'typescript' ) ;
1
+ var ts = require ( 'typescript' ) ;
2
2
var createFilter = require ( 'rollup-pluginutils' ) . createFilter ;
3
3
4
4
var assign = Object . assign || function ( target , source ) {
@@ -9,25 +9,57 @@ var assign = Object.assign || function ( target, source ) {
9
9
return target ;
10
10
} ;
11
11
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 ) {
13
18
options = assign ( { } , options || { } ) ;
14
19
15
- var filter = createFilter ( options . include , options . exclude ) ;
20
+ var filter = createFilter ( options . include || [ '*.ts+(|x)' , '**/*.ts+(|x)' ] , options . exclude ) ;
16
21
delete options . include ;
17
22
delete options . exclude ;
18
23
24
+ options = assign ( {
25
+ target : ts . ScriptTarget . ES5 ,
26
+ module : ts . ModuleKind . ES6 ,
27
+ sourceMap : true
28
+ } , options ) ;
29
+
19
30
return {
20
31
transform : function ( code , id ) {
21
32
if ( ! filter ( id ) ) return null ;
22
33
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
+ }
29
58
} ) ;
30
59
60
+ if ( fatalError ) {
61
+ throw new Error ( 'There were TypeScript errors transpiling "' + id + '"' ) ;
62
+ }
31
63
32
64
return {
33
65
code : transformed . outputText ,
0 commit comments