1
1
import deindent from '../utils/deindent' ;
2
2
import list from '../utils/list' ;
3
3
import { CompileOptions , ModuleFormat , Node } from '../interfaces' ;
4
- import Stats from '../Stats' ;
5
4
6
5
interface Dependency {
7
6
name : string ;
8
7
statements : string [ ] ;
9
8
source : string ;
10
9
}
11
10
12
- const wrappers = { esm, cjs, eval : expr } ;
11
+ const wrappers = { esm, cjs } ;
13
12
14
13
type Export = {
15
14
name : string ;
@@ -21,7 +20,6 @@ export default function wrapModule(
21
20
format : ModuleFormat ,
22
21
name : string ,
23
22
options : CompileOptions ,
24
- stats : Stats ,
25
23
banner : string ,
26
24
sveltePath = 'svelte' ,
27
25
helpers : { name : string , alias : string } [ ] ,
@@ -36,7 +34,6 @@ export default function wrapModule(
36
34
}
37
35
38
36
if ( format === 'cjs' ) return cjs ( code , name , banner , sveltePath , internalPath , helpers , imports , module_exports ) ;
39
- if ( format === 'eval' ) return expr ( code , name , options , stats , banner , imports ) ;
40
37
41
38
throw new Error ( `options.format is invalid (must be ${ list ( Object . keys ( wrappers ) ) } )` ) ;
42
39
}
@@ -142,114 +139,4 @@ function cjs(
142
139
${ code }
143
140
144
141
${ exports } `
145
- }
146
-
147
- function expr (
148
- code : string ,
149
- name : string ,
150
- options : CompileOptions ,
151
- stats : Stats ,
152
- banner : string ,
153
- imports : Node [ ]
154
- ) {
155
- const dependencies = imports . map ( ( declaration , i ) => {
156
- const defaultImport = declaration . specifiers . find (
157
- ( x : Node ) =>
158
- x . type === 'ImportDefaultSpecifier' ||
159
- ( x . type === 'ImportSpecifier' && x . imported . name === 'default' )
160
- ) ;
161
-
162
- const namespaceImport = declaration . specifiers . find (
163
- ( x : Node ) => x . type === 'ImportNamespaceSpecifier'
164
- ) ;
165
-
166
- const namedImports = declaration . specifiers . filter (
167
- ( x : Node ) =>
168
- x . type === 'ImportSpecifier' && x . imported . name !== 'default'
169
- ) ;
170
-
171
- const name = defaultImport || namespaceImport
172
- ? ( defaultImport || namespaceImport ) . local . name
173
- : `__import${ i } ` ;
174
-
175
- const statements : string [ ] = [ ] ;
176
-
177
- namedImports . forEach ( ( specifier : Node ) => {
178
- statements . push (
179
- `var ${ specifier . local . name } = ${ name } .${ specifier . imported . name } ;`
180
- ) ;
181
- } ) ;
182
-
183
- if ( defaultImport ) {
184
- statements . push (
185
- `${ name } = (${ name } && ${ name } .__esModule) ? ${ name } ["default"] : ${ name } ;`
186
- ) ;
187
- }
188
-
189
- return { name, statements, source : declaration . source . value } ;
190
- } ) ;
191
-
192
- const globals = getGlobals ( dependencies , options , stats ) ;
193
-
194
- return deindent `
195
- (function (${ paramString ( dependencies ) } ) { "use strict";
196
- ${ banner }
197
-
198
- ${ getCompatibilityStatements ( dependencies ) }
199
-
200
- ${ code }
201
-
202
- return ${ name } ;
203
- }(${ globals . join ( ', ' ) } ))` ;
204
- }
205
-
206
- function paramString ( dependencies : Dependency [ ] ) {
207
- return dependencies . map ( dep => dep . name ) . join ( ', ' ) ;
208
- }
209
-
210
- function getCompatibilityStatements ( dependencies : Dependency [ ] ) {
211
- if ( ! dependencies . length ) return null ;
212
-
213
- const statements : string [ ] = [ ] ;
214
-
215
- dependencies . forEach ( dependency => {
216
- statements . push ( ...dependency . statements ) ;
217
- } ) ;
218
-
219
- return statements . join ( '\n' ) ;
220
- }
221
-
222
- function getGlobals ( dependencies : Dependency [ ] , options : CompileOptions , stats : Stats ) {
223
- const { globals } = options ;
224
- const globalFn = getGlobalFn ( globals ) ;
225
-
226
- return dependencies . map ( d => {
227
- let name = globalFn ( d . source ) ;
228
-
229
- if ( ! name ) {
230
- if ( d . name . startsWith ( '__import' ) ) {
231
- throw new Error (
232
- `Could not determine name for imported module '${ d . source } ' – use options.globals`
233
- ) ;
234
- } else {
235
- stats . warn ( {
236
- code : `options-missing-globals` ,
237
- message : `No name was supplied for imported module '${ d . source } '. Guessing '${ d . name } ', but you should use options.globals` ,
238
- } ) ;
239
- }
240
-
241
- name = d . name ;
242
- }
243
-
244
- return name ;
245
- } ) ;
246
- }
247
-
248
- function getGlobalFn ( globals : any ) : ( id : string ) => string {
249
- if ( typeof globals === 'function' ) return globals ;
250
- if ( typeof globals === 'object' ) {
251
- return id => globals [ id ] ;
252
- }
253
-
254
- return ( ) => undefined ;
255
- }
142
+ }
0 commit comments