1 | import * as tippex from 'tippex';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | export default function fix ( code: string, id: string ): string {
|
35 |
|
36 |
|
37 | const cleanCode = getErasedCode( code, id );
|
38 |
|
39 | const re = /export\s+(default\s+)?((?:abstract\s+)?class)(?:\s+(\w+))?/g;
|
40 | let match: RegExpExecArray;
|
41 |
|
42 | while ( match = re.exec( cleanCode ) ) {
|
43 |
|
44 | code = erase( code, match.index, match[ 0 ].indexOf( match[ 2 ] ) );
|
45 |
|
46 | let name = match[ 3 ];
|
47 |
|
48 | if ( match[ 1 ] ) {
|
49 |
|
50 |
|
51 | if ( !name ) throw new Error( `TypeScript Plugin: cannot export an un-named class (module ${ id })` );
|
52 |
|
53 |
|
54 | name += ' as default';
|
55 | }
|
56 |
|
57 |
|
58 | code += `\nexport { ${ name } };`
|
59 | }
|
60 |
|
61 | return code;
|
62 | }
|
63 |
|
64 | function getErasedCode ( code: string, id: string ): string {
|
65 | try {
|
66 | return tippex.erase( code );
|
67 | } catch (e) {
|
68 | throw new Error( `rollup-plugin-typescript: ${ e.message }; when processing: '${ id }'` );
|
69 | }
|
70 | }
|
71 |
|
72 | function erase ( code: string, start: number, length: number ): string {
|
73 | const end = start + length;
|
74 |
|
75 | return code.slice( 0, start ) +
|
76 | code.slice( start, end ).replace( /[^\s]/g, ' ' ) +
|
77 | code.slice( end );
|
78 | }
|