diff --git a/package.json b/package.json index 012ccd3..913a0d2 100644 --- a/package.json +++ b/package.json @@ -24,22 +24,23 @@ "pretest": "npm run build", "test": "mocha", "posttest": "npm run lint", - "prepublish": "npm run test" + "prepublishOnly": "npm run test" }, "dependencies": { - "compare-versions": "2.0.1", - "object-assign": "^4.0.1", - "rollup-pluginutils": "^1.3.1", - "tippex": "^2.1.1", - "typescript": "^1.8.9" + "rollup-pluginutils": "^2.0.1", + "tippex": "^3.0.0" }, "devDependencies": { - "buble": "^0.13.1", - "eslint": "^2.13.1", - "mocha": "^3.0.0", + "@types/node": "9.6.7", + "buble": "^0.19.3", + "eslint": "^4.19.1", + "mocha": "^5.1.1", "rimraf": "^2.5.4", - "rollup": "^0.49.3", - "rollup-plugin-buble": "^0.13.0" + "rollup": "^0.58.2", + "rollup-plugin-buble": "^0.19.2", + "rollup-plugin-node-resolve": "^3.3.0", + "tslib": "^1.9.0", + "typescript": "^2.8.3" }, "repository": { "type": "git", @@ -47,5 +48,9 @@ }, "bugs": { "url": "https://github.com/rollup/rollup-plugin-typescript/issues" + }, + "peerDependencies": { + "tslib": "^1.9.0", + "typescript": "1.x || 2.x" } } diff --git a/rollup.config.js b/rollup.config.js index 661d839..3a5d032 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -5,13 +5,12 @@ export default { input: 'src/index.js', external: [ - 'compare-versions', 'path', 'fs', - 'object-assign', 'rollup-pluginutils', 'tippex', - 'typescript' + 'typescript', + 'tslib' ], plugins: [ diff --git a/src/index.js b/src/index.js index 425b3c2..f99e830 100644 --- a/src/index.js +++ b/src/index.js @@ -1,32 +1,12 @@ import * as ts from 'typescript'; import { createFilter } from 'rollup-pluginutils'; -import * as path from 'path'; -import * as fs from 'fs'; -import assign from 'object-assign'; -import compareVersions from 'compare-versions'; import { endsWith } from './string'; import { getDefaultOptions, compilerOptionsFromTsConfig, adjustCompilerOptions } from './options.js'; import fixExportClass from './fixExportClass'; import resolveHost from './resolveHost'; -/* -interface Options { - tsconfig?: boolean; - include?: string | string[]; - exclude?: string | string[]; - typescript?: typeof ts; - module?: string; -} -*/ - -// The injected id for helpers. Intentially invalid to prevent helpers being included in source maps. -const helpersId = '\0typescript-helpers'; -const helpersSource = fs.readFileSync( path.resolve( __dirname, '../src/typescript-helpers.js' ), 'utf-8' ); - -export default function typescript ( options ) { - options = assign( {}, options || {} ); - +export default function typescript ( options = {} ) { const filter = createFilter( options.include || [ '*.ts+(|x)', '**/*.ts+(|x)' ], options.exclude || [ '*.d.ts', '**/*.d.ts' ] ); @@ -51,12 +31,7 @@ export default function typescript ( options ) { adjustCompilerOptions( typescript, options ); // Merge all options. - options = assign( tsconfig, getDefaultOptions(), options ); - - // Verify that we're targeting ES2015 modules. - if ( options.module !== 'es2015' && options.module !== 'es6' ) { - throw new Error( `rollup-plugin-typescript: The module kind should be 'es2015', found: '${ options.module }'` ); - } + options = Object.assign( tsconfig, getDefaultOptions(), options ); const parsed = typescript.convertCompilerOptionsFromJson( options, process.cwd() ); @@ -70,23 +45,13 @@ export default function typescript ( options ) { return { resolveId ( importee, importer ) { - // Handle the special `typescript-helpers` import itself. - if ( importee === helpersId ) { - return helpersId; - } - if ( !importer ) return null; let result; importer = importer.split('\\').join('/'); - if ( compareVersions( typescript.version, '1.8.0' ) < 0 ) { - // Suppress TypeScript warnings for function call. - result = typescript.nodeModuleNameResolver( importee, importer, resolveHost ); - } else { - result = typescript.nodeModuleNameResolver( importee, importer, compilerOptions, resolveHost ); - } + result = typescript.nodeModuleNameResolver( importee, importer, compilerOptions, resolveHost ); if ( result.resolvedModule && result.resolvedModule.resolvedFileName ) { if ( endsWith( result.resolvedModule.resolvedFileName, '.d.ts' ) ) { @@ -99,12 +64,6 @@ export default function typescript ( options ) { return null; }, - load ( id ) { - if ( id === helpersId ) { - return helpersSource; - } - }, - transform ( code, id ) { if ( !filter( id ) ) return null; @@ -142,8 +101,7 @@ export default function typescript ( options ) { return { // Always append an import for the helpers. - code: transformed.outputText + - `\nimport { __assign, __awaiter, __extends, __decorate, __metadata, __param } from '${helpersId}';`, + code: transformed.outputText, // Rollup expects `map` to be an object so we must parse the string map: transformed.sourceMapText ? JSON.parse(transformed.sourceMapText) : null diff --git a/src/options.js b/src/options.js index 5502ba4..21a86df 100644 --- a/src/options.js +++ b/src/options.js @@ -1,5 +1,4 @@ import * as path from 'path'; -import compareVersions from 'compare-versions'; import { existsSync, readFileSync @@ -7,7 +6,6 @@ import { export function getDefaultOptions () { return { - noEmitHelpers: true, module: 'es2015', sourceMap: true }; @@ -59,10 +57,6 @@ export function adjustCompilerOptions ( typescript, options ) { // 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` ); - } + // Prevent duplication of helpers + options.importHelpers = true; } diff --git a/test/test.js b/test/test.js index ecaf43f..37486da 100644 --- a/test/test.js +++ b/test/test.js @@ -1,7 +1,7 @@ const assert = require( 'assert' ); const rollup = require( 'rollup' ); -const assign = require( 'object-assign' ); const typescript = require( '..' ); +const resolve = require('rollup-plugin-node-resolve'); process.chdir( __dirname ); @@ -18,7 +18,7 @@ async function evaluate ( bundle ) { function bundle ( main, options ) { return rollup.rollup({ input: main, - plugins: [ typescript( options ) ] + plugins: [ resolve(), typescript( options ) ] }); } @@ -49,7 +49,7 @@ describe( 'rollup-plugin-typescript', function () { const { code } = await b.generate({ format: 'es' }); // The `__extends` function is defined in the bundle. - assert.ok( code.indexOf( 'function __extends' ) > -1, code ); + assert.ok( code.indexOf( '__extends' ) > -1, code ); // No duplicate `__extends` helper is defined. assert.equal( code.indexOf( '__extends$1' ), -1, code ); @@ -59,21 +59,11 @@ describe( 'rollup-plugin-typescript', function () { const b = await bundle( 'sample/export-class-fix/main.ts' ); const { code } = await b.generate({ format: 'es' }); - assert.equal( code.indexOf( 'class' ), -1, code ); - assert.ok( code.indexOf( 'var A = (function' ) !== -1, code ); - assert.ok( code.indexOf( 'var B = (function' ) !== -1, code ); + assert.ok( code.indexOf( 'var A = /** @class */ (function' ) !== -1, code ); + assert.ok( code.indexOf( 'var B = /** @class */ (function' ) !== -1, code ); assert.ok( code.indexOf( 'export { A, B };' ) !== -1, code ); }); - it( 'transpiles ES6 features to ES5 with source maps', async () => { - const b = await bundle( 'sample/import-class/main.ts' ); - const { code } = await b.generate({ format: 'es' }); - - assert.equal( code.indexOf( 'class' ), -1, code ); - assert.equal( code.indexOf( '...' ), -1, code ); - assert.equal( code.indexOf( '=>' ), -1, code ); - }); - it( 'reports diagnostics and throws if errors occur during transpilation', async () => { let errored; try { @@ -120,53 +110,6 @@ describe( 'rollup-plugin-typescript', function () { assert.equal( await evaluate( b ), 1337 ); }); - describe( 'strictNullChecks', () => { - it( 'is enabled for versions >= 1.9.0', async () => { - await bundle( 'sample/overriding-typescript/main.ts', { - tsconfig: false, - strictNullChecks: true, - - typescript: fakeTypescript({ - version: '1.9.0-fake', - transpileModule ( 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', async () => { - let warning = ''; - - console.warn = function (msg) { - warning = msg; - }; - - await rollup.rollup({ - input: 'sample/overriding-typescript/main.ts', - plugins: [ - typescript({ - tsconfig: false, - strictNullChecks: true, - - typescript: fakeTypescript({ - version: '1.8.0-fake' - }) - }) - ] - }); - - assert.notEqual( warning.indexOf( "'strictNullChecks' is not supported" ), -1 ); - }); - }); - it( 'should not resolve .d.ts files', async () => { const b = await bundle( 'sample/dts/main.ts' ); assert.deepEqual( b.imports, [ 'an-import' ] ); @@ -176,9 +119,6 @@ describe( 'rollup-plugin-typescript', function () { const b = await bundle( 'sample/jsx/main.tsx', { jsx: 'react' }); const { code } = await b.generate({ format: 'es' }); - assert.notEqual( code.indexOf( 'const __assign = ' ), -1, - 'should contain __assign definition' ); - const usage = code.indexOf( 'React.createElement("span", __assign({}, props), "Yo!")' ); assert.notEqual( usage, -1, 'should contain usage' ); @@ -220,7 +160,7 @@ describe( 'rollup-plugin-typescript', function () { }); function fakeTypescript ( custom ) { - return assign({ + return Object.assign({ transpileModule () { return { outputText: '',