From e14fc30df388c02b35197e4e0685bcaf9070d30a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Segersv=C3=A4rd?= Date: Sun, 19 Jun 2016 22:45:05 +0200 Subject: [PATCH 1/2] Ignore the declaration option --- CHANGELOG.md | 3 +++ package.json | 2 +- src/index.ts | 18 +++++++++++------- test/test.js | 4 ++++ 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b0dcc44..e2b5416 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## master +## 0.7.6 +* Ignore the `declaration` option ([#45](https://github.com/rollup/rollup-plugin-typescript/issues/45)) + ## 0.7.5 * Ensure NPM doesn't ignore typescript-helpers diff --git a/package.json b/package.json index 7e8b568..2bed14b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rollup-plugin-typescript", - "version": "0.7.5", + "version": "0.7.6", "description": "Seamless integration between Rollup and TypeScript.", "main": "dist/rollup-plugin-typescript.cjs.js", "jsnext:main": "dist/rollup-plugin-typescript.es6.js", diff --git a/src/index.ts b/src/index.ts index de5a9f1..733f75e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -76,13 +76,17 @@ function compilerOptionsFromTsConfig( typescript: typeof ts ): ts.CompilerOption return tsconfig.config.compilerOptions; } -// Set `sourceMap` to `inlineSourceMap` if it's a boolean, -// under the assumption that both are never specified simultaneously. -function fixSourceMapOption( options: any ) { +function adjustCompilerOptions( options: any ) { + // Set `sourceMap` to `inlineSourceMap` if it's a boolean + // under the assumption that both are never specified simultaneously. if ( typeof options.inlineSourceMap === 'boolean' ) { options.sourceMap = options.inlineSourceMap; delete options.inlineSourceMap; } + + // Delete the `declaration` option to prevent compilation error. + // See: https://github.com/rollup/rollup-plugin-typescript/issues/45 + delete options.declaration; } export default function typescript ( options: Options ) { @@ -106,10 +110,10 @@ export default function typescript ( options: Options ) { delete options.tsconfig; - // Since Rollup handles the source maps; we equate the - // `sourceMap` and `inlineSourceMap` options. - fixSourceMapOption( tsconfig ); - fixSourceMapOption( options ); + // Since the CompilerOptions aren't designed for the Rollup + // use case, we'll adjust them for use with Rollup. + adjustCompilerOptions( tsconfig ); + adjustCompilerOptions( options ); // Merge all options. options = assign( tsconfig, getDefaultOptions(), options ); diff --git a/test/test.js b/test/test.js index df0c698..387e72f 100644 --- a/test/test.js +++ b/test/test.js @@ -33,6 +33,10 @@ describe( 'rollup-plugin-typescript', function () { }); }); + it( 'ignores the declaration option', function () { + return bundle( 'sample/basic/main.ts', { declaration: true }); + }); + it( 'handles async functions', function () { return bundle( 'sample/async/main.ts' ) .then( function ( bundle ) { From 50115986a874a16dd6b44571eaa896f8a306a756 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Segersv=C3=A4rd?= Date: Mon, 20 Jun 2016 00:16:19 +0200 Subject: [PATCH 2/2] Disable strictNullChecks for unsupported versions with a warning --- CHANGELOG.md | 1 + src/index.ts | 27 +++++++++++--- test/test.js | 102 +++++++++++++++++++++++++++++++++++++++++---------- 3 files changed, 106 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e2b5416..b2adbf7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ## 0.7.6 * Ignore the `declaration` option ([#45](https://github.com/rollup/rollup-plugin-typescript/issues/45)) +* Disable `strictNullChecks` with a warning for TypeScript versions that don't support it ([#46](https://github.com/rollup/rollup-plugin-typescript/issues/46)) ## 0.7.5 * Ensure NPM doesn't ignore typescript-helpers diff --git a/src/index.ts b/src/index.ts index 733f75e..e7abb72 100644 --- a/src/index.ts +++ b/src/index.ts @@ -21,6 +21,13 @@ interface Options { } const resolveHost = { + directoryExists ( dirPath: string ): boolean { + try { + return statSync( dirPath ).isDirectory(); + } catch ( err ) { + return false; + } + }, fileExists ( filePath: string ): boolean { try { return statSync( filePath ).isFile(); @@ -63,6 +70,7 @@ function findFile( cwd: string, filename: string ): string { return fp; } } + return null; } @@ -76,7 +84,7 @@ function compilerOptionsFromTsConfig( typescript: typeof ts ): ts.CompilerOption return tsconfig.config.compilerOptions; } -function adjustCompilerOptions( options: any ) { +function adjustCompilerOptions( typescript: typeof ts, options: any ) { // Set `sourceMap` to `inlineSourceMap` if it's a boolean // under the assumption that both are never specified simultaneously. if ( typeof options.inlineSourceMap === 'boolean' ) { @@ -87,6 +95,13 @@ function adjustCompilerOptions( options: any ) { // Delete the `declaration` option to prevent compilation error. // See: https://github.com/rollup/rollup-plugin-typescript/issues/45 delete options.declaration; + + const tsVersion = typescript.version.split('-')[0]; + if ( 'strictNullChecks' in options && compareVersions( tsVersion, '1.9.0' ) < 0 ) { + delete options.strictNullChecks; + + console.warn( `rollup-plugin-typescript: 'strictNullChecks' is not supported; disabling it` ); + } } export default function typescript ( options: Options ) { @@ -112,8 +127,8 @@ export default function typescript ( options: Options ) { // Since the CompilerOptions aren't designed for the Rollup // use case, we'll adjust them for use with Rollup. - adjustCompilerOptions( tsconfig ); - adjustCompilerOptions( options ); + adjustCompilerOptions( typescript, tsconfig ); + adjustCompilerOptions( typescript, options ); // Merge all options. options = assign( tsconfig, getDefaultOptions(), options ); @@ -171,7 +186,9 @@ export default function typescript ( options: Options ) { compilerOptions }); - const diagnostics = transformed.diagnostics.filter( goodErrors ); + const diagnostics = transformed.diagnostics ? + transformed.diagnostics.filter( goodErrors ) : []; + let fatalError = false; diagnostics.forEach( diagnostic => { @@ -200,7 +217,7 @@ export default function typescript ( options: Options ) { `\nimport { __extends, __decorate, __metadata, __param, __awaiter } from 'typescript-helpers';`, // Rollup expects `map` to be an object so we must parse the string - map: JSON.parse(transformed.sourceMapText) + map: JSON.parse(transformed.sourceMapText as string) }; } }; diff --git a/test/test.js b/test/test.js index 387e72f..1336f0a 100644 --- a/test/test.js +++ b/test/test.js @@ -1,5 +1,6 @@ var assert = require( 'assert' ); var rollup = require( 'rollup' ); +var assign = require( 'object-assign' ); var typescript = require( '..' ); process.chdir( __dirname ); @@ -107,7 +108,9 @@ describe( 'rollup-plugin-typescript', function () { tsconfig: false, // test with a mocked version of TypeScript - typescript: { + typescript: fakeTypescript({ + version: '1.8.0-fake', + transpileModule: function ( code ) { // Ignore the code to transpile. Always return the same thing. return { @@ -115,28 +118,59 @@ describe( 'rollup-plugin-typescript', function () { diagnostics: [], sourceMapText: JSON.stringify({ mappings: '' }) }; - }, - - // return empty compiler options - convertCompilerOptionsFromJson: function ( options ) { - [ - 'include', - 'exclude', - 'typescript', - 'tsconfig', - ].forEach( function ( option ) { - if ( option in options ) { - throw new Error( 'unrecognized compiler option "' + option + '"' ); - } - }); - - return { options: {}, errors: [] }; } - } + }) }) ] }).then( function ( bundle ) { - assert.equal( bundle.generate().code.indexOf( 'var main = 1337;' ), 0 ); + assert.equal( evaluate( bundle ), 1337 ); + }); + }); + + describe( 'strictNullChecks', function () { + it( 'is enabled for versions >= 1.9.0', function () { + return bundle( 'sample/overriding-typescript/main.ts', { + tsconfig: false, + strictNullChecks: true, + + typescript: fakeTypescript({ + version: '1.9.0-fake', + transpileModule: function ( code, options ) { + assert.ok( options.compilerOptions.strictNullChecks, + 'strictNullChecks should be passed through' ); + + return { + outputText: '', + diagnostics: [], + sourceMapText: JSON.stringify({ mappings: '' }) + }; + } + }), + }); + }); + + it( 'is disabled with a warning < 1.9.0', function () { + var warning = ''; + + console.warn = function (msg) { + warning = msg; + }; + + return rollup.rollup({ + entry: 'sample/overriding-typescript/main.ts', + plugins: [ + typescript({ + tsconfig: false, + strictNullChecks: true, + + typescript: fakeTypescript({ + version: '1.8.0-fake', + }) + }) + ] + }).then( function () { + assert.notEqual( warning.indexOf( "'strictNullChecks' is not supported" ), -1 ); + }); }); }); @@ -168,3 +202,33 @@ describe( 'rollup-plugin-typescript', function () { }); }); }); + +function fakeTypescript( custom ) { + return assign({ + transpileModule: function ( code, options ) { + return { + outputText: '', + diagnostics: [], + sourceMapText: JSON.stringify({ mappings: '' }) + }; + }, + + convertCompilerOptionsFromJson: function ( options ) { + [ + 'include', + 'exclude', + 'typescript', + 'tsconfig', + ].forEach( function ( option ) { + if ( option in options ) { + throw new Error( 'unrecognized compiler option "' + option + '"' ); + } + }); + + return { + options: options, + errors: [] + }; + } + }, custom); +}