diff --git a/CHANGELOG.md b/CHANGELOG.md index 39676a3..0d39bfb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # rollup-plugin-typescript changelog +## 0.2.1 +* Enable source maps per default + ## 0.2.0 * Use (_prerelease version of_) TypeScript 1.7.0 to generate ES5 while preserving ES2015 imports for efficient bundling. diff --git a/index.js b/index.js index 4a3af5c..229e385 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,4 @@ -var typescript = require( 'typescript' ); +var ts = require( 'typescript' ); var createFilter = require( 'rollup-pluginutils' ).createFilter; var assign = Object.assign || function ( target, source ) { @@ -9,25 +9,57 @@ var assign = Object.assign || function ( target, source ) { return target; }; -module.exports = function ( options ) { +function goodErrors ( diagnostic ) { + // All errors except `Cannot compile modules into 'es6' when targeting 'ES5' or lower.` + return diagnostic.code !== 1204; +} + +module.exports = function typescript ( options ) { options = assign( {}, options || {} ); - var filter = createFilter( options.include, options.exclude ); + var filter = createFilter( options.include || [ '*.ts+(|x)', '**/*.ts+(|x)' ], options.exclude ); delete options.include; delete options.exclude; + options = assign( { + target: ts.ScriptTarget.ES5, + module: ts.ModuleKind.ES6, + sourceMap: true + }, options ); + return { transform: function ( code, id ) { if ( !filter( id ) ) return null; - var transformed = typescript.transpileModule( code, { - compilerOptions: assign( { - target: typescript.ScriptTarget.ES5, - module: typescript.ModuleKind.ES6, - sourceMap: true - }, options ) + var transformed = ts.transpileModule( code, { + reportDiagnostics: true, + compilerOptions: options + }); + + var diagnostics = transformed.diagnostics.filter( goodErrors ); + var fatalError = false; + + diagnostics.forEach(function ( diagnostic ) { + var message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'); + + if ( diagnostic.file ) { + var pos = diagnostic.file.getLineAndCharacterOfPosition( diagnostic.start ); + + console.error( diagnostic.file.fileName + + '(' + (pos.line + 1) + ',' + (pos.character + 1) + '): error ES' + + diagnostic.code + ': ' + message ); + } else { + console.error( 'Error: ' + message ); + } + + if ( diagnostic.category === ts.DiagnosticCategory.Error ) { + fatalError = true; + } }); + if ( fatalError ) { + throw new Error( 'There were TypeScript errors transpiling "' + id + '"' ); + } return { code: transformed.outputText, diff --git a/package.json b/package.json index 9ff3cd0..2c8fcd2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rollup-plugin-typescript", - "version": "0.2.0", + "version": "0.2.1", "description": "Seamless integration between Rollup and TypeScript.", "keywords": [ "rollup-plugin", diff --git a/test/sample/syntax-error/missing-type.ts b/test/sample/syntax-error/missing-type.ts new file mode 100644 index 0000000..49d803c --- /dev/null +++ b/test/sample/syntax-error/missing-type.ts @@ -0,0 +1 @@ +var a: ; diff --git a/test/test.js b/test/test.js index fc5dfb1..15f29a2 100644 --- a/test/test.js +++ b/test/test.js @@ -8,20 +8,13 @@ describe( 'rollup-plugin-typescript', function () { this.timeout( 5000 ); it( 'runs code through typescript', function () { - var start = Date.now(); - return rollup.rollup({ entry: 'sample/basic/main.ts', plugins: [ typescript() ] }).then( function ( bundle ) { - console.log( 'bundled in %s ms', Date.now() - start ); - - start = Date.now(); const generated = bundle.generate(); - console.log( 'generated in %s ms', Date.now() - start ); - const code = generated.code; assert.ok( code.indexOf( 'number' ) === -1, code ); @@ -29,21 +22,14 @@ describe( 'rollup-plugin-typescript', function () { }); }); - it( 'transpiles ES6 features to ES5', function () { - var start = Date.now(); - + it( 'transpiles ES6 features to ES5 with source maps', function () { return rollup.rollup({ entry: 'sample/import-class/main.ts', plugins: [ typescript() ] }).then( function ( bundle ) { - console.log( 'bundled in %s ms', Date.now() - start ); - - start = Date.now(); const generated = bundle.generate(); - console.log( 'generated in %s ms', Date.now() - start ); - const code = generated.code; assert.ok( code.indexOf( 'class' ) === -1, code ); @@ -51,4 +37,15 @@ describe( 'rollup-plugin-typescript', function () { assert.ok( code.indexOf( '=>' ) === -1, code ); }); }); + + it( 'reports diagnostics and throws if errors occur during transpilation', function () { + return rollup.rollup({ + entry: 'sample/syntax-error/missing-type.ts', + plugins: [ + typescript() + ] + }).catch( function ( error ) { + assert.ok( error.message.indexOf( 'There were TypeScript errors' ) === 0, 'Should reject erroneous code.' ); + }); + }); });