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

WIP: Use user supplied TypeScript installation #117

Closed
Closed
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
27 changes: 16 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,33 @@
"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",
"url": "git+https://github.com/rollup/rollup-plugin-typescript.git"
},
"bugs": {
"url": "https://github.com/rollup/rollup-plugin-typescript/issues"
},
"peerDependencies": {
"tslib": "^1.9.0",
"typescript": "1.x || 2.x"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps I am misunderstanding, but based on tslib's installation instructions (reproduced below) I believe that "tslib": "^1.9.0" would require "typescript": "^2.3.3".

# TypeScript 2.3.3 or later
npm install --save tslib

# TypeScript 2.3.2 or earlier
npm install --save tslib@1.6.1

I think it would be fair by now to just require "typescript": "^2.3.3".

}
}
5 changes: 2 additions & 3 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ export default {
input: 'src/index.js',

external: [
'compare-versions',
'path',
'fs',
'object-assign',
'rollup-pluginutils',
'tippex',
'typescript'
'typescript',
'tslib'
],

plugins: [
Expand Down
50 changes: 4 additions & 46 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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' ] );
Expand All @@ -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() );

Expand All @@ -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' ) ) {
Expand All @@ -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;

Expand Down Expand Up @@ -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
Expand Down
10 changes: 2 additions & 8 deletions src/options.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import * as path from 'path';
import compareVersions from 'compare-versions';
import {
existsSync,
readFileSync
} from 'fs';

export function getDefaultOptions () {
return {
noEmitHelpers: true,
module: 'es2015',
sourceMap: true
};
Expand Down Expand Up @@ -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;
}
72 changes: 6 additions & 66 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -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 );

Expand All @@ -18,7 +18,7 @@ async function evaluate ( bundle ) {
function bundle ( main, options ) {
return rollup.rollup({
input: main,
plugins: [ typescript( options ) ]
plugins: [ resolve(), typescript( options ) ]
});
}

Expand Down Expand Up @@ -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 );
Expand All @@ -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 {
Expand Down Expand Up @@ -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' ] );
Expand All @@ -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' );
Expand Down Expand Up @@ -220,7 +160,7 @@ describe( 'rollup-plugin-typescript', function () {
});

function fakeTypescript ( custom ) {
return assign({
return Object.assign({
transpileModule () {
return {
outputText: '',
Expand Down