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

Commit 7091e45

Browse files
NaridaLlukastaegert
authored andcommitted
Remove fixExportClass (#107)
* Added failing tests. * I'm not entirely sure what fixExportClass.js was supposed to do, in any case the current version of TS handles export class correctly. Some new ts helpers were added, so the new version caused some things to fail. I've changed it to use the helpers in the offical tslib package. Also there is now an "importHelper" option which only import the correct ones, so it isn't necessary to import them in the plugin. I've changed the helperId to "tslib", and am doing the resolve myself so it doesn't fail if the user isn't using rollup-plugin-node-resolve. I'm not entirely sure this is the best way of doing it. * Update package.json * Make it node 7 compatible
1 parent f61b187 commit 7091e45

File tree

10 files changed

+67
-145
lines changed

10 files changed

+67
-145
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ The following options are unique to `rollup-plugin-typescript`:
4141
* `typescript` overrides TypeScript used for transpilation
4242

4343
### TypeScript version
44-
[TypeScript 1.8.9](https://github.com/Microsoft/TypeScript/wiki/Roadmap#18) is used by default. Should your project require it, you can override the TypeScript version used for _transpiling the sources_.
44+
[TypeScript 2.6.2](https://github.com/Microsoft/TypeScript/wiki/Roadmap#26-october-2017) is used by default. Should your project require it, you can override the TypeScript version used for _transpiling the sources_.
4545

4646
```js
4747
typescript({

package-lock.json

Lines changed: 8 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,17 @@
3030
"compare-versions": "2.0.1",
3131
"object-assign": "^4.0.1",
3232
"rollup-pluginutils": "^1.3.1",
33-
"tippex": "^2.1.1"
33+
"tippex": "^2.1.1",
34+
"tslib": ">=1.9.0",
35+
"typescript": ">=2.6.2"
3436
},
3537
"devDependencies": {
3638
"buble": "^0.13.1",
3739
"eslint": "^2.13.1",
3840
"mocha": "^3.0.0",
3941
"rimraf": "^2.5.4",
4042
"rollup": "^0.49.3",
41-
"rollup-plugin-buble": "^0.13.0",
42-
"typescript": "^1.8.9"
43-
},
44-
"peerDependencies": {
45-
"typescript": ">=1.8.9"
43+
"rollup-plugin-buble": "^0.13.0"
4644
},
4745
"repository": {
4846
"type": "git",

src/fixExportClass.js

Lines changed: 0 additions & 78 deletions
This file was deleted.

src/index.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import compareVersions from 'compare-versions';
77

88
import { endsWith } from './string';
99
import { getDefaultOptions, compilerOptionsFromTsConfig, adjustCompilerOptions } from './options.js';
10-
import fixExportClass from './fixExportClass';
1110
import resolveHost from './resolveHost';
1211

1312
/*
@@ -21,8 +20,8 @@ interface Options {
2120
*/
2221

2322
// The injected id for helpers. Intentially invalid to prevent helpers being included in source maps.
24-
const helpersId = '\0typescript-helpers';
25-
const helpersSource = fs.readFileSync( path.resolve( __dirname, '../src/typescript-helpers.js' ), 'utf-8' );
23+
const helpersId = 'tslib';
24+
const helpersSource = fs.readFileSync( path.resolve( __dirname, '../node_modules/tslib/tslib.es6.js' ), 'utf-8' );
2625

2726
export default function typescript ( options ) {
2827
options = assign( {}, options || {} );
@@ -108,7 +107,7 @@ export default function typescript ( options ) {
108107
transform ( code, id ) {
109108
if ( !filter( id ) ) return null;
110109

111-
const transformed = typescript.transpileModule( fixExportClass( code, id ), {
110+
const transformed = typescript.transpileModule( code, {
112111
fileName: id,
113112
reportDiagnostics: true,
114113
compilerOptions
@@ -141,9 +140,7 @@ export default function typescript ( options ) {
141140
}
142141

143142
return {
144-
// Always append an import for the helpers.
145-
code: transformed.outputText +
146-
`\nimport { __assign, __awaiter, __extends, __decorate, __metadata, __param } from '${helpersId}';`,
143+
code: transformed.outputText,
147144

148145
// Rollup expects `map` to be an object so we must parse the string
149146
map: transformed.sourceMapText ? JSON.parse(transformed.sourceMapText) : null

src/options.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ export function getDefaultOptions () {
99
return {
1010
noEmitHelpers: true,
1111
module: 'es2015',
12-
sourceMap: true
12+
sourceMap: true,
13+
importHelpers: true
1314
};
1415
}
1516

src/typescript-helpers.js

Lines changed: 0 additions & 41 deletions
This file was deleted.

test/sample/export-fodule/main.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export function test() {
2+
return 0;
3+
}
4+
export namespace test {
5+
export const foo = "2"
6+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// https://github.com/rollup/rollup-plugin-typescript/issues/70#issuecomment-336216349
2+
3+
// test.ts
4+
export namespace MODE {
5+
export class MODE {
6+
}
7+
}

test/test.js

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const assert = require( 'assert' );
22
const rollup = require( 'rollup' );
33
const assign = require( 'object-assign' );
44
const typescript = require( '..' );
5+
const fs = require('fs');
56

67
process.chdir( __dirname );
78

@@ -15,11 +16,17 @@ async function evaluate ( bundle ) {
1516
}
1617

1718
// Short-hand for rollup using the typescript plugin.
18-
function bundle ( main, options ) {
19-
return rollup.rollup({
19+
function bundle (main, options) {
20+
const result = rollup.rollup({
2021
input: main,
21-
plugins: [ typescript( options ) ]
22+
plugins: [typescript(options)]
2223
});
24+
result
25+
.then(bundle => bundle.generate({ format: 'cjs' }))
26+
.then(res => {
27+
fs.writeFileSync(main + '.out', res.code);
28+
});
29+
return result;
2330
}
2431

2532
describe( 'rollup-plugin-typescript', function () {
@@ -59,17 +66,21 @@ describe( 'rollup-plugin-typescript', function () {
5966
const b = await bundle( 'sample/export-class-fix/main.ts' );
6067
const { code } = await b.generate({ format: 'es' });
6168

62-
assert.equal( code.indexOf( 'class' ), -1, code );
63-
assert.ok( code.indexOf( 'var A = (function' ) !== -1, code );
64-
assert.ok( code.indexOf( 'var B = (function' ) !== -1, code );
6569
assert.ok( code.indexOf( 'export { A, B };' ) !== -1, code );
70+
71+
const { A, B } = await evaluate(b);
72+
const aInst = new A();
73+
const bInst = new B();
74+
assert.ok(aInst instanceof A);
75+
assert.ok(bInst instanceof B);
76+
77+
6678
});
6779

6880
it( 'transpiles ES6 features to ES5 with source maps', async () => {
6981
const b = await bundle( 'sample/import-class/main.ts' );
7082
const { code } = await b.generate({ format: 'es' });
7183

72-
assert.equal( code.indexOf( 'class' ), -1, code );
7384
assert.equal( code.indexOf( '...' ), -1, code );
7485
assert.equal( code.indexOf( '=>' ), -1, code );
7586
});
@@ -176,7 +187,7 @@ describe( 'rollup-plugin-typescript', function () {
176187
const b = await bundle( 'sample/jsx/main.tsx', { jsx: 'react' });
177188
const { code } = await b.generate({ format: 'es' });
178189

179-
assert.notEqual( code.indexOf( 'const __assign = ' ), -1,
190+
assert.notEqual( code.indexOf( 'var __assign = ' ), -1,
180191
'should contain __assign definition' );
181192

182193
const usage = code.indexOf( 'React.createElement("span", __assign({}, props), "Yo!")' );
@@ -217,6 +228,22 @@ describe( 'rollup-plugin-typescript', function () {
217228

218229
assert.ok( map.sources.every( source => source.indexOf( 'typescript-helpers' ) === -1) );
219230
});
231+
232+
it( 'should allow a namespace containing a class', async () => {
233+
const b = await bundle( 'sample/export-namespace-export-class/test.ts');
234+
const MODE = (await evaluate(b)).MODE.MODE;
235+
const mode = new MODE();
236+
237+
assert.ok(mode instanceof MODE);
238+
});
239+
240+
it( 'should allow merging an exported function and namespace', async () => {
241+
const b = await bundle( 'sample/export-fodule/main.ts');
242+
const f = (await evaluate(b)).test;
243+
244+
assert.equal(f(), 0);
245+
assert.equal(f.foo, "2");
246+
});
220247
});
221248

222249
function fakeTypescript ( custom ) {

0 commit comments

Comments
 (0)