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

Report diagnostics #16

Merged
merged 3 commits into from
Jan 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
50 changes: 41 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -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 ) {
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
1 change: 1 addition & 0 deletions test/sample/syntax-error/missing-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var a: ;
27 changes: 12 additions & 15 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,44 @@ 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 );
assert.ok( code.indexOf( 'const' ) === -1, code );
});
});

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 );
assert.ok( code.indexOf( '...' ) === -1, code );
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.' );
});
});
});