This repository has been archived by the owner on Aug 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from rollup/export-class-name
Handle TypeScript's `export class Name`
- Loading branch information
Showing
10 changed files
with
133 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ export default { | |
'fs', | ||
'object-assign', | ||
'rollup-pluginutils', | ||
'tippex', | ||
'typescript' | ||
], | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import * as tippex from 'tippex'; | ||
|
||
// Hack around TypeScript's broken handling of `export class` with | ||
// ES6 modules and ES5 script target. | ||
// | ||
// It works because TypeScript transforms | ||
// | ||
// export class A {} | ||
// | ||
// into something like CommonJS, when we wanted ES6 modules. | ||
// | ||
// var A = (function () { | ||
// function A() { | ||
// } | ||
// return A; | ||
// }()); | ||
// exports.A = A; | ||
// | ||
// But | ||
// | ||
// class A {} | ||
// export { A }; | ||
// | ||
// is transformed into this beauty. | ||
// | ||
// var A = (function () { | ||
// function A() { | ||
// } | ||
// return A; | ||
// }()); | ||
// export { A }; | ||
// | ||
// The solution is to replace the previous export syntax with the latter. | ||
export default function fix ( code: string, id: string ): string { | ||
|
||
// Erase comments, strings etc. to avoid erroneous matches for the Regex. | ||
const cleanCode = tippex.erase( code ); | ||
|
||
const re = /export\s+(default\s+)?class(?:\s+(\w+))?/g; | ||
let match; | ||
|
||
while ( match = re.exec( cleanCode ) ) { | ||
// To keep source maps intact, replace non-whitespace characters with spaces. | ||
code = erase( code, match.index, match[ 0 ].indexOf( 'class' ) ); | ||
|
||
let name = match[ 2 ]; | ||
|
||
if ( match[ 1 ] ) { // it is a default export | ||
|
||
// TODO: support this too | ||
if ( !name ) throw new Error( `TypeScript Plugin: cannot export an un-named class (module ${ id })` ); | ||
|
||
// Export the name ` as default`. | ||
name += ' as default'; | ||
} | ||
|
||
// To keep source maps intact, append the injected exports last. | ||
code += `\nexport { ${ name } };` | ||
} | ||
|
||
return code; | ||
} | ||
|
||
function erase ( code: string, start: number, length: number ): string { | ||
const end = start + length; | ||
|
||
return code.slice( 0, start ) + | ||
code.slice( start, end ).replace( /[^\s]/g, ' ' ) + | ||
code.slice( end ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// the odd spacing is intentional | ||
export default | ||
class A {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import A from './default'; | ||
import { B } from './named'; | ||
|
||
export { A, B }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// the odd spacing is intentional | ||
export class B {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export class Foo { | ||
foo = 'bar'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import {Foo} from './Foo.ts'; | ||
|
||
export default new Foo(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters