14
14
* limitations under the License.
15
15
*/
16
16
17
- import {
18
- Transform ,
19
- Range ,
20
- IMPORT_SPECIFIER ,
21
- IMPORT_NAMESPACE_SPECIFIER ,
22
- IMPORT_DEFAULT_SPECIFIER ,
23
- } from '../types' ;
17
+ import { Transform , Range } from '../types' ;
24
18
import { literalName } from '../parsing/literal-name' ;
19
+ import { FormatSpecifiers , Specifiers } from '../parsing/import-specifiers' ;
25
20
import { TransformSourceDescription } from 'rollup' ;
26
21
import MagicString from 'magic-string' ;
27
- import { ImportDeclaration , Identifier , ImportSpecifier } from 'estree' ;
22
+ import { ImportDeclaration , Identifier } from 'estree' ;
28
23
import { parse , walk } from '../acorn' ;
29
24
30
25
const DYNAMIC_IMPORT_KEYWORD = 'import' ;
@@ -42,19 +37,6 @@ interface RangedImport {
42
37
range : Range ;
43
38
}
44
39
45
- const VALID_SPECIFIERS = [ IMPORT_SPECIFIER , IMPORT_NAMESPACE_SPECIFIER , IMPORT_DEFAULT_SPECIFIER ] ;
46
- function importLocalNames ( declaration : ImportDeclaration ) : Array < string > {
47
- const returnableSpecifiers : Array < string > = [ ] ;
48
-
49
- for ( const specifier of declaration . specifiers || [ ] ) {
50
- if ( VALID_SPECIFIERS . includes ( specifier . type ) ) {
51
- returnableSpecifiers . push ( specifier . local . name ) ;
52
- }
53
- }
54
-
55
- return returnableSpecifiers ;
56
- }
57
-
58
40
export default class ImportTransform extends Transform {
59
41
private importedExternalsSyntax : { [ key : string ] : string } = { } ;
60
42
private importedExternalsLocalNames : Array < string > = [ ] ;
@@ -105,43 +87,11 @@ window['${DYNAMIC_IMPORT_REPLACEMENT}'] = ${DYNAMIC_IMPORT_REPLACEMENT};`;
105
87
async ImportDeclaration ( node : ImportDeclaration ) {
106
88
const name = literalName ( node . source ) ;
107
89
const range : Range = node . range as Range ;
90
+ const specifiers : Specifiers = Specifiers ( node . specifiers ) ;
108
91
109
- let defaultSpecifier : string | null = null ;
110
- const specificSpecifiers : Array < string > = [ ] ;
111
- for ( const specifier of node . specifiers ) {
112
- switch ( specifier . type ) {
113
- case IMPORT_SPECIFIER :
114
- case IMPORT_NAMESPACE_SPECIFIER :
115
- const { name : local } = ( specifier as ImportSpecifier ) . local ;
116
- const { name : imported } = ( specifier as ImportSpecifier ) . imported ;
117
- specificSpecifiers . push ( local === imported ? local : `${ imported } as ${ local } ` ) ;
118
- break ;
119
- case IMPORT_DEFAULT_SPECIFIER :
120
- defaultSpecifier = specifier . local . name ;
121
- break ;
122
- }
123
- }
124
-
125
- const multipleSpecifiers = specificSpecifiers . length > 0 ;
126
- if ( defaultSpecifier !== null ) {
127
- if ( multipleSpecifiers ) {
128
- self . importedExternalsSyntax [
129
- name
130
- ] = `import ${ defaultSpecifier } ,{${ specificSpecifiers . join ( ',' ) } } from '${ name } ';` ;
131
- } else {
132
- self . importedExternalsSyntax [ name ] = `import ${ defaultSpecifier } from '${ name } ';` ;
133
- }
134
- } else if ( multipleSpecifiers ) {
135
- self . importedExternalsSyntax [ name ] = `import {${ specificSpecifiers . join (
136
- ',' ,
137
- ) } } from '${ name } ';`;
138
- }
139
-
92
+ self . importedExternalsSyntax [ name ] = FormatSpecifiers ( specifiers , name ) ;
93
+ self . importedExternalsLocalNames . push ( ...specifiers . local ) ;
140
94
source . remove ( ...range ) ;
141
-
142
- self . importedExternalsLocalNames = self . importedExternalsLocalNames . concat (
143
- importLocalNames ( node ) ,
144
- ) ;
145
95
} ,
146
96
Import ( node : RangedImport ) {
147
97
const [ start , end ] = node . range ;
0 commit comments