1
1
import { stringifyRequest } from 'loader-utils' ;
2
2
3
3
function resolveImports ( type , item ) {
4
+ const defaultSyntax = type === 'module' ? 'default' : 'single' ;
4
5
let result ;
5
6
6
7
if ( typeof item === 'string' ) {
@@ -13,7 +14,7 @@ function resolveImports(type, item) {
13
14
if ( splittedItem . length === 1 ) {
14
15
result = {
15
16
type,
16
- syntax : 'default' ,
17
+ syntax : defaultSyntax ,
17
18
moduleName : splittedItem [ 0 ] ,
18
19
name : splittedItem [ 0 ] ,
19
20
// eslint-disable-next-line no-undefined
@@ -29,9 +30,9 @@ function resolveImports(type, item) {
29
30
} ;
30
31
}
31
32
} else {
32
- result = { syntax : 'default' , ...item } ;
33
+ result = { syntax : defaultSyntax , ...item } ;
33
34
34
- if ( result . syntax === 'default' && ! result . name ) {
35
+ if ( result . syntax === defaultSyntax && ! result . name ) {
35
36
result . name = result . moduleName ;
36
37
}
37
38
}
@@ -43,7 +44,7 @@ function resolveImports(type, item) {
43
44
}
44
45
45
46
if (
46
- [ 'default' , 'side-effect' ] . includes ( result . syntax ) &&
47
+ [ 'default' , 'single' , ' side-effect' , 'pure '] . includes ( result . syntax ) &&
47
48
typeof result . alias !== 'undefined'
48
49
) {
49
50
throw new Error (
@@ -52,22 +53,34 @@ function resolveImports(type, item) {
52
53
}
53
54
54
55
if (
55
- [ 'side-effect' ] . includes ( result . syntax ) &&
56
+ [ 'side-effect' , 'pure' ] . includes ( result . syntax ) &&
56
57
typeof result . name !== 'undefined'
57
58
) {
58
59
throw new Error (
59
60
`The "${ result . syntax } " syntax can't have "${ result . name } " name in "${ item } " value`
60
61
) ;
61
62
}
62
63
63
- if ( [ 'namespace' ] . includes ( result . syntax ) && type === 'commonjs' ) {
64
+ if (
65
+ [ 'default' , 'namespace' , 'named' , 'side-effect' ] . includes ( result . syntax ) &&
66
+ type === 'commonjs'
67
+ ) {
64
68
throw new Error (
65
- `The "commonjs" type not support "namespace " syntax import in "${ item } " value`
69
+ `The "commonjs" type not support "${ result . syntax } " syntax import in "${ item } " value`
66
70
) ;
67
71
}
68
72
69
73
if (
70
- [ 'namespace' , 'named' ] . includes ( result . syntax ) &&
74
+ [ 'single' , 'multiple' , 'pure' ] . includes ( result . syntax ) &&
75
+ type === 'module'
76
+ ) {
77
+ throw new Error (
78
+ `The "module" type not support "${ result . syntax } " syntax import in "${ item } " value`
79
+ ) ;
80
+ }
81
+
82
+ if (
83
+ [ 'namespace' , 'named' , 'multiple' ] . includes ( result . syntax ) &&
71
84
typeof result . name === 'undefined'
72
85
) {
73
86
throw new Error (
@@ -97,18 +110,23 @@ function getImports(type, imports) {
97
110
sortedResults [ item . moduleName ] . push ( item ) ;
98
111
}
99
112
113
+ const defaultSyntax = type === 'module' ? 'default' : 'single' ;
114
+
100
115
for ( const item of Object . entries ( sortedResults ) ) {
101
116
const defaultImports = item [ 1 ] . filter (
102
- ( entry ) => entry . syntax === 'default'
117
+ ( entry ) => entry . syntax === defaultSyntax
103
118
) ;
119
+
104
120
const namespaceImports = item [ 1 ] . filter (
105
121
( entry ) => entry . syntax === 'namespace'
106
122
) ;
107
123
const sideEffectImports = item [ 1 ] . filter (
108
124
( entry ) => entry . syntax === 'side-effect'
109
125
) ;
110
126
111
- [ defaultImports , namespaceImports , sideEffectImports ] . forEach (
127
+ const pure = item [ 1 ] . filter ( ( entry ) => entry . syntax === 'pure' ) ;
128
+
129
+ [ defaultImports , namespaceImports , sideEffectImports , pure ] . forEach (
112
130
( importsSyntax ) => {
113
131
if ( importsSyntax . length > 1 ) {
114
132
const [ { syntax } ] = importsSyntax ;
@@ -127,37 +145,48 @@ function getImports(type, imports) {
127
145
function renderImports ( loaderContext , type , imports ) {
128
146
const [ { moduleName } ] = imports ;
129
147
const defaultImports = imports . filter ( ( item ) => item . syntax === 'default' ) ;
148
+ const singleImports = imports . filter ( ( item ) => item . syntax === 'single' ) ;
130
149
const namedImports = imports . filter ( ( item ) => item . syntax === 'named' ) ;
150
+ const multipleImports = imports . filter ( ( item ) => item . syntax === 'multiple' ) ;
131
151
const namespaceImports = imports . filter (
132
152
( item ) => item . syntax === 'namespace'
133
153
) ;
134
154
const sideEffectImports = imports . filter (
135
155
( item ) => item . syntax === 'side-effect'
136
156
) ;
157
+ const pure = imports . filter ( ( item ) => item . syntax === 'pure' ) ;
137
158
const isModule = type === 'module' ;
138
159
139
- // 1. Import -side-effect
160
+ // 1. Module import -side-effect
140
161
if ( sideEffectImports . length > 0 ) {
141
- return isModule
142
- ? `import ${ stringifyRequest ( loaderContext , moduleName ) } ;`
143
- : `require(${ stringifyRequest ( loaderContext , moduleName ) } );` ;
162
+ return `import ${ stringifyRequest ( loaderContext , moduleName ) } ;` ;
163
+ }
164
+
165
+ // 2. CommonJs pure
166
+ if ( pure . length > 0 ) {
167
+ return `require(${ stringifyRequest ( loaderContext , moduleName ) } );` ;
144
168
}
145
169
146
170
let code = isModule ? 'import' : '' ;
147
171
148
- // 2. Default import
172
+ // 3. Module default import
149
173
if ( defaultImports . length > 0 ) {
150
174
const [ { name } ] = defaultImports ;
151
175
152
- code += isModule
153
- ? ` ${ name } `
154
- : `var ${ name } = require(${ stringifyRequest (
155
- loaderContext ,
156
- moduleName
157
- ) } );`;
176
+ code += ` ${ name } ` ;
158
177
}
159
178
160
- // 3. Namespace import
179
+ // 4. CommonJs single import
180
+ if ( singleImports . length > 0 ) {
181
+ const [ { name } ] = singleImports ;
182
+
183
+ code += `var ${ name } = require(${ stringifyRequest (
184
+ loaderContext ,
185
+ moduleName
186
+ ) } );`;
187
+ }
188
+
189
+ // 5. Module namespace import
161
190
if ( namespaceImports . length > 0 ) {
162
191
if ( defaultImports . length > 0 ) {
163
192
code += `,` ;
@@ -168,25 +197,42 @@ function renderImports(loaderContext, type, imports) {
168
197
code += ` * as ${ name } ` ;
169
198
}
170
199
171
- // 4. Named import
200
+ // 6. Module named import
172
201
if ( namedImports . length > 0 ) {
173
202
if ( defaultImports . length > 0 ) {
174
- code += isModule ? ', { ' : '\nvar { ';
203
+ code += ', { ';
175
204
} else {
176
- code += isModule ? ' { ' : 'var { ';
205
+ code += ' { ';
177
206
}
178
207
179
208
namedImports . forEach ( ( namedImport , i ) => {
180
209
const comma = i > 0 ? ', ' : '' ;
181
210
const { name, alias } = namedImport ;
182
- const sep = isModule ? ' as ' : ': ' ;
211
+ const sep = ' as ' ;
212
+
213
+ code += alias ? `${ comma } ${ name } ${ sep } ${ alias } ` : `${ comma } ${ name } ` ;
214
+ } ) ;
215
+
216
+ code += ' }' ;
217
+ }
218
+
219
+ // 7. CommonJs multiple import
220
+ if ( multipleImports . length > 0 ) {
221
+ if ( singleImports . length > 0 ) {
222
+ code += '\nvar { ' ;
223
+ } else {
224
+ code += 'var { ' ;
225
+ }
226
+
227
+ multipleImports . forEach ( ( multipleImport , i ) => {
228
+ const comma = i > 0 ? ', ' : '' ;
229
+ const { name, alias } = multipleImport ;
230
+ const sep = ': ' ;
183
231
184
232
code += alias ? `${ comma } ${ name } ${ sep } ${ alias } ` : `${ comma } ${ name } ` ;
185
233
} ) ;
186
234
187
- code += isModule
188
- ? ' }'
189
- : ` } = require(${ stringifyRequest ( loaderContext , moduleName ) } );` ;
235
+ code += ` } = require(${ stringifyRequest ( loaderContext , moduleName ) } );` ;
190
236
}
191
237
192
238
if ( ! isModule ) {
0 commit comments