@@ -14,43 +14,39 @@ const ssrTransformSimpleCode = async (code: string, url?: string) =>
14
14
test ( 'default import' , async ( ) => {
15
15
expect (
16
16
await ssrTransformSimpleCode ( `import foo from 'vue';console.log(foo.bar)` ) ,
17
- ) . toMatchInlineSnapshot ( `
18
- "const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["default"]});
19
- console.log(__vite_ssr_import_0__.default.bar)"
20
- ` )
17
+ ) . toMatchInlineSnapshot (
18
+ `"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["default"]});console.log(__vite_ssr_import_0__.default.bar)"` ,
19
+ )
21
20
} )
22
21
23
22
test ( 'named import' , async ( ) => {
24
23
expect (
25
24
await ssrTransformSimpleCode (
26
25
`import { ref } from 'vue';function foo() { return ref(0) }` ,
27
26
) ,
28
- ) . toMatchInlineSnapshot ( `
29
- "const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["ref"]});
30
- function foo() { return (0,__vite_ssr_import_0__.ref)(0) }"
31
- ` )
27
+ ) . toMatchInlineSnapshot (
28
+ `"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["ref"]});function foo() { return (0,__vite_ssr_import_0__.ref)(0) }"` ,
29
+ )
32
30
} )
33
31
34
32
test ( 'named import: arbitrary module namespace specifier' , async ( ) => {
35
33
expect (
36
34
await ssrTransformSimpleCode (
37
35
`import { "some thing" as ref } from 'vue';function foo() { return ref(0) }` ,
38
36
) ,
39
- ) . toMatchInlineSnapshot ( `
40
- "const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["some thing"]});
41
- function foo() { return (0,__vite_ssr_import_0__["some thing"])(0) }"
42
- ` )
37
+ ) . toMatchInlineSnapshot (
38
+ `"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["some thing"]});function foo() { return (0,__vite_ssr_import_0__["some thing"])(0) }"` ,
39
+ )
43
40
} )
44
41
45
42
test ( 'namespace import' , async ( ) => {
46
43
expect (
47
44
await ssrTransformSimpleCode (
48
45
`import * as vue from 'vue';function foo() { return vue.ref(0) }` ,
49
46
) ,
50
- ) . toMatchInlineSnapshot ( `
51
- "const __vite_ssr_import_0__ = await __vite_ssr_import__("vue");
52
- function foo() { return __vite_ssr_import_0__.ref(0) }"
53
- ` )
47
+ ) . toMatchInlineSnapshot (
48
+ `"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue");function foo() { return __vite_ssr_import_0__.ref(0) }"` ,
49
+ )
54
50
} )
55
51
56
52
test ( 'export function declaration' , async ( ) => {
@@ -93,7 +89,6 @@ test('export named from', async () => {
93
89
await ssrTransformSimpleCode ( `export { ref, computed as c } from 'vue'` ) ,
94
90
) . toMatchInlineSnapshot ( `
95
91
"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["ref","computed"]});
96
-
97
92
Object.defineProperty(__vite_ssr_exports__, "ref", { enumerable: true, configurable: true, get(){ return __vite_ssr_import_0__.ref }});
98
93
Object.defineProperty(__vite_ssr_exports__, "c", { enumerable: true, configurable: true, get(){ return __vite_ssr_import_0__.computed }});"
99
94
` )
@@ -106,7 +101,6 @@ test('named exports of imported binding', async () => {
106
101
) ,
107
102
) . toMatchInlineSnapshot ( `
108
103
"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["createApp"]});
109
-
110
104
Object.defineProperty(__vite_ssr_exports__, "createApp", { enumerable: true, configurable: true, get(){ return __vite_ssr_import_0__.createApp }});"
111
105
` )
112
106
} )
@@ -117,11 +111,9 @@ test('export * from', async () => {
117
111
`export * from 'vue'\n` + `export * from 'react'` ,
118
112
) ,
119
113
) . toMatchInlineSnapshot ( `
120
- "const __vite_ssr_import_0__ = await __vite_ssr_import__("vue");
121
- __vite_ssr_exportAll__(__vite_ssr_import_0__);
114
+ "const __vite_ssr_import_0__ = await __vite_ssr_import__("vue");__vite_ssr_exportAll__(__vite_ssr_import_0__);
122
115
;
123
- const __vite_ssr_import_1__ = await __vite_ssr_import__("react");
124
- __vite_ssr_exportAll__(__vite_ssr_import_1__);
116
+ const __vite_ssr_import_1__ = await __vite_ssr_import__("react");__vite_ssr_exportAll__(__vite_ssr_import_1__);
125
117
"
126
118
` )
127
119
} )
@@ -130,7 +122,6 @@ test('export * as from', async () => {
130
122
expect ( await ssrTransformSimpleCode ( `export * as foo from 'vue'` ) )
131
123
. toMatchInlineSnapshot ( `
132
124
"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue");
133
-
134
125
Object.defineProperty(__vite_ssr_exports__, "foo", { enumerable: true, configurable: true, get(){ return __vite_ssr_import_0__ }});"
135
126
` )
136
127
} )
@@ -140,7 +131,6 @@ test('export * as from arbitrary module namespace identifier', async () => {
140
131
await ssrTransformSimpleCode ( `export * as "arbitrary string" from 'vue'` ) ,
141
132
) . toMatchInlineSnapshot ( `
142
133
"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue");
143
-
144
134
Object.defineProperty(__vite_ssr_exports__, "arbitrary string", { enumerable: true, configurable: true, get(){ return __vite_ssr_import_0__ }});"
145
135
` )
146
136
} )
@@ -163,7 +153,6 @@ test('export as from arbitrary module namespace identifier', async () => {
163
153
) ,
164
154
) . toMatchInlineSnapshot ( `
165
155
"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["arbitrary string2"]});
166
-
167
156
Object.defineProperty(__vite_ssr_exports__, "arbitrary string", { enumerable: true, configurable: true, get(){ return __vite_ssr_import_0__["arbitrary string2"] }});"
168
157
` )
169
158
} )
@@ -180,9 +169,7 @@ test('export then import minified', async () => {
180
169
`export * from 'vue';import {createApp} from 'vue';` ,
181
170
) ,
182
171
) . toMatchInlineSnapshot ( `
183
- "const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["createApp"]});
184
- const __vite_ssr_import_1__ = await __vite_ssr_import__("vue");
185
- __vite_ssr_exportAll__(__vite_ssr_import_1__);
172
+ "const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["createApp"]});const __vite_ssr_import_1__ = await __vite_ssr_import__("vue");__vite_ssr_exportAll__(__vite_ssr_import_1__);
186
173
"
187
174
` )
188
175
} )
@@ -192,9 +179,94 @@ test('hoist import to top', async () => {
192
179
await ssrTransformSimpleCode (
193
180
`path.resolve('server.js');import path from 'node:path';` ,
194
181
) ,
182
+ ) . toMatchInlineSnapshot (
183
+ `"const __vite_ssr_import_0__ = await __vite_ssr_import__("node:path", {"importedNames":["default"]});__vite_ssr_import_0__.default.resolve('server.js');"` ,
184
+ )
185
+ } )
186
+
187
+ test ( 'whitespace between imports does not trigger hoisting' , async ( ) => {
188
+ expect (
189
+ await ssrTransformSimpleCode (
190
+ `import { dirname } from 'node:path';\n\n\nimport fs from 'node:fs';` ,
191
+ ) ,
192
+ ) . toMatchInlineSnapshot ( `
193
+ "const __vite_ssr_import_0__ = await __vite_ssr_import__("node:path", {"importedNames":["dirname"]});
194
+
195
+
196
+ const __vite_ssr_import_1__ = await __vite_ssr_import__("node:fs", {"importedNames":["default"]});"
197
+ ` )
198
+ } )
199
+
200
+ test ( 'preserve line offset when rewriting imports' , async ( ) => {
201
+ // The line number of each non-import statement must not change.
202
+ const inputLines = [
203
+ `debugger;` ,
204
+ `` ,
205
+ `import {` ,
206
+ ` dirname,` ,
207
+ ` join,` ,
208
+ `} from 'node:path';` ,
209
+ `` ,
210
+ `debugger;` ,
211
+ `` ,
212
+ `import fs from 'node:fs';` ,
213
+ `` ,
214
+ `debugger;` ,
215
+ `` ,
216
+ `import {` ,
217
+ ` red,` ,
218
+ ` green,` ,
219
+ `} from 'kleur/colors';` ,
220
+ `` ,
221
+ `debugger;` ,
222
+ ]
223
+
224
+ const output = await ssrTransformSimpleCode ( inputLines . join ( '\n' ) )
225
+ expect ( output ) . toBeDefined ( )
226
+
227
+ const outputLines = output ! . split ( '\n' )
228
+ expect (
229
+ outputLines
230
+ . map ( ( line , i ) => `${ String ( i + 1 ) . padStart ( 2 ) } | ${ line } ` . trimEnd ( ) )
231
+ . join ( '\n' ) ,
232
+ ) . toMatchInlineSnapshot ( `
233
+ " 1 | const __vite_ssr_import_0__ = await __vite_ssr_import__("node:path", {"importedNames":["dirname","join"]});const __vite_ssr_import_1__ = await __vite_ssr_import__("node:fs", {"importedNames":["default"]});const __vite_ssr_import_2__ = await __vite_ssr_import__("kleur/colors", {"importedNames":["red","green"]});debugger;
234
+ 2 |
235
+ 3 |
236
+ 4 |
237
+ 5 |
238
+ 6 |
239
+ 7 |
240
+ 8 | debugger;
241
+ 9 |
242
+ 10 |
243
+ 11 |
244
+ 12 | debugger;
245
+ 13 |
246
+ 14 |
247
+ 15 |
248
+ 16 |
249
+ 17 |
250
+ 18 |
251
+ 19 | debugger;"
252
+ ` )
253
+
254
+ // Ensure the debugger statements are still on the same lines.
255
+ expect ( outputLines [ 0 ] . endsWith ( inputLines [ 0 ] ) ) . toBe ( true )
256
+ expect ( outputLines [ 7 ] ) . toBe ( inputLines [ 7 ] )
257
+ expect ( outputLines [ 11 ] ) . toBe ( inputLines [ 11 ] )
258
+ expect ( outputLines [ 18 ] ) . toBe ( inputLines [ 18 ] )
259
+ } )
260
+
261
+ // not implemented
262
+ test . skip ( 'comments between imports do not trigger hoisting' , async ( ) => {
263
+ expect (
264
+ await ssrTransformSimpleCode (
265
+ `import { dirname } from 'node:path';// comment\nimport fs from 'node:fs';` ,
266
+ ) ,
195
267
) . toMatchInlineSnapshot ( `
196
- "const __vite_ssr_import_0__ = await __vite_ssr_import__("node:path", {"importedNames":["default "]});
197
- __vite_ssr_import_0__.default.resolve('server.js' );"
268
+ "const __vite_ssr_import_0__ = await __vite_ssr_import__("node:path", {"importedNames":["dirname "]});// comment
269
+ const __vite_ssr_import_1__ = await __vite_ssr_import__("node:fs", {"importedNames":["default"]} );"
198
270
` )
199
271
} )
200
272
@@ -220,21 +292,19 @@ test('do not rewrite method definition', async () => {
220
292
const result = await ssrTransformSimple (
221
293
`import { fn } from 'vue';class A { fn() { fn() } }` ,
222
294
)
223
- expect ( result ?. code ) . toMatchInlineSnapshot ( `
224
- "const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["fn"]});
225
- class A { fn() { (0,__vite_ssr_import_0__.fn)() } }"
226
- ` )
295
+ expect ( result ?. code ) . toMatchInlineSnapshot (
296
+ `"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["fn"]});class A { fn() { (0,__vite_ssr_import_0__.fn)() } }"` ,
297
+ )
227
298
expect ( result ?. deps ) . toEqual ( [ 'vue' ] )
228
299
} )
229
300
230
301
test ( 'do not rewrite when variable is in scope' , async ( ) => {
231
302
const result = await ssrTransformSimple (
232
303
`import { fn } from 'vue';function A(){ const fn = () => {}; return { fn }; }` ,
233
304
)
234
- expect ( result ?. code ) . toMatchInlineSnapshot ( `
235
- "const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["fn"]});
236
- function A(){ const fn = () => {}; return { fn }; }"
237
- ` )
305
+ expect ( result ?. code ) . toMatchInlineSnapshot (
306
+ `"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["fn"]});function A(){ const fn = () => {}; return { fn }; }"` ,
307
+ )
238
308
expect ( result ?. deps ) . toEqual ( [ 'vue' ] )
239
309
} )
240
310
@@ -243,10 +313,9 @@ test('do not rewrite when variable is in scope with object destructuring', async
243
313
const result = await ssrTransformSimple (
244
314
`import { fn } from 'vue';function A(){ let {fn, test} = {fn: 'foo', test: 'bar'}; return { fn }; }` ,
245
315
)
246
- expect ( result ?. code ) . toMatchInlineSnapshot ( `
247
- "const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["fn"]});
248
- function A(){ let {fn, test} = {fn: 'foo', test: 'bar'}; return { fn }; }"
249
- ` )
316
+ expect ( result ?. code ) . toMatchInlineSnapshot (
317
+ `"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["fn"]});function A(){ let {fn, test} = {fn: 'foo', test: 'bar'}; return { fn }; }"` ,
318
+ )
250
319
expect ( result ?. deps ) . toEqual ( [ 'vue' ] )
251
320
} )
252
321
@@ -255,10 +324,9 @@ test('do not rewrite when variable is in scope with array destructuring', async
255
324
const result = await ssrTransformSimple (
256
325
`import { fn } from 'vue';function A(){ let [fn, test] = ['foo', 'bar']; return { fn }; }` ,
257
326
)
258
- expect ( result ?. code ) . toMatchInlineSnapshot ( `
259
- "const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["fn"]});
260
- function A(){ let [fn, test] = ['foo', 'bar']; return { fn }; }"
261
- ` )
327
+ expect ( result ?. code ) . toMatchInlineSnapshot (
328
+ `"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["fn"]});function A(){ let [fn, test] = ['foo', 'bar']; return { fn }; }"` ,
329
+ )
262
330
expect ( result ?. deps ) . toEqual ( [ 'vue' ] )
263
331
} )
264
332
@@ -267,10 +335,9 @@ test('rewrite variable in string interpolation in function nested arguments', as
267
335
const result = await ssrTransformSimple (
268
336
`import { fn } from 'vue';function A({foo = \`test\${fn}\`} = {}){ return {}; }` ,
269
337
)
270
- expect ( result ?. code ) . toMatchInlineSnapshot ( `
271
- "const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["fn"]});
272
- function A({foo = \`test\${__vite_ssr_import_0__.fn}\`} = {}){ return {}; }"
273
- ` )
338
+ expect ( result ?. code ) . toMatchInlineSnapshot (
339
+ `"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["fn"]});function A({foo = \`test\${__vite_ssr_import_0__.fn}\`} = {}){ return {}; }"` ,
340
+ )
274
341
expect ( result ?. deps ) . toEqual ( [ 'vue' ] )
275
342
} )
276
343
@@ -279,21 +346,19 @@ test('rewrite variables in default value of destructuring params', async () => {
279
346
const result = await ssrTransformSimple (
280
347
`import { fn } from 'vue';function A({foo = fn}){ return {}; }` ,
281
348
)
282
- expect ( result ?. code ) . toMatchInlineSnapshot ( `
283
- "const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["fn"]});
284
- function A({foo = __vite_ssr_import_0__.fn}){ return {}; }"
285
- ` )
349
+ expect ( result ?. code ) . toMatchInlineSnapshot (
350
+ `"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["fn"]});function A({foo = __vite_ssr_import_0__.fn}){ return {}; }"` ,
351
+ )
286
352
expect ( result ?. deps ) . toEqual ( [ 'vue' ] )
287
353
} )
288
354
289
355
test ( 'do not rewrite when function declaration is in scope' , async ( ) => {
290
356
const result = await ssrTransformSimple (
291
357
`import { fn } from 'vue';function A(){ function fn() {}; return { fn }; }` ,
292
358
)
293
- expect ( result ?. code ) . toMatchInlineSnapshot ( `
294
- "const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["fn"]});
295
- function A(){ function fn() {}; return { fn }; }"
296
- ` )
359
+ expect ( result ?. code ) . toMatchInlineSnapshot (
360
+ `"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["fn"]});function A(){ function fn() {}; return { fn }; }"` ,
361
+ )
297
362
expect ( result ?. deps ) . toEqual ( [ 'vue' ] )
298
363
} )
299
364
@@ -302,62 +367,56 @@ test('do not rewrite when function expression is in scope', async () => {
302
367
const result = await ssrTransformSimple (
303
368
`import {fn} from './vue';var a = function() { return function fn() { console.log(fn) } }` ,
304
369
)
305
- expect ( result ?. code ) . toMatchInlineSnapshot ( `
306
- "const __vite_ssr_import_0__ = await __vite_ssr_import__("./vue", {"importedNames":["fn"]});
307
- var a = function() { return function fn() { console.log(fn) } }"
308
- ` )
370
+ expect ( result ?. code ) . toMatchInlineSnapshot (
371
+ `"const __vite_ssr_import_0__ = await __vite_ssr_import__("./vue", {"importedNames":["fn"]});var a = function() { return function fn() { console.log(fn) } }"` ,
372
+ )
309
373
} )
310
374
311
375
// #16452
312
376
test ( 'do not rewrite when function expression is in global scope' , async ( ) => {
313
377
const result = await ssrTransformSimple (
314
378
`import {fn} from './vue';foo(function fn(a = fn) { console.log(fn) })` ,
315
379
)
316
- expect ( result ?. code ) . toMatchInlineSnapshot ( `
317
- "const __vite_ssr_import_0__ = await __vite_ssr_import__("./vue", {"importedNames":["fn"]});
318
- foo(function fn(a = fn) { console.log(fn) })"
319
- ` )
380
+ expect ( result ?. code ) . toMatchInlineSnapshot (
381
+ `"const __vite_ssr_import_0__ = await __vite_ssr_import__("./vue", {"importedNames":["fn"]});foo(function fn(a = fn) { console.log(fn) })"` ,
382
+ )
320
383
} )
321
384
322
385
test ( 'do not rewrite when class declaration is in scope' , async ( ) => {
323
386
const result = await ssrTransformSimple (
324
387
`import { cls } from 'vue';function A(){ class cls {} return { cls }; }` ,
325
388
)
326
- expect ( result ?. code ) . toMatchInlineSnapshot ( `
327
- "const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["cls"]});
328
- function A(){ class cls {} return { cls }; }"
329
- ` )
389
+ expect ( result ?. code ) . toMatchInlineSnapshot (
390
+ `"const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["cls"]});function A(){ class cls {} return { cls }; }"` ,
391
+ )
330
392
expect ( result ?. deps ) . toEqual ( [ 'vue' ] )
331
393
} )
332
394
333
395
test ( 'do not rewrite when class expression is in scope' , async ( ) => {
334
396
const result = await ssrTransformSimple (
335
397
`import { cls } from './vue';var a = function() { return class cls { constructor() { console.log(cls) } } }` ,
336
398
)
337
- expect ( result ?. code ) . toMatchInlineSnapshot ( `
338
- "const __vite_ssr_import_0__ = await __vite_ssr_import__("./vue", {"importedNames":["cls"]});
339
- var a = function() { return class cls { constructor() { console.log(cls) } } }"
340
- ` )
399
+ expect ( result ?. code ) . toMatchInlineSnapshot (
400
+ `"const __vite_ssr_import_0__ = await __vite_ssr_import__("./vue", {"importedNames":["cls"]});var a = function() { return class cls { constructor() { console.log(cls) } } }"` ,
401
+ )
341
402
} )
342
403
343
404
test ( 'do not rewrite when class expression is in global scope' , async ( ) => {
344
405
const result = await ssrTransformSimple (
345
406
`import { cls } from './vue';foo(class cls { constructor() { console.log(cls) } })` ,
346
407
)
347
- expect ( result ?. code ) . toMatchInlineSnapshot ( `
348
- "const __vite_ssr_import_0__ = await __vite_ssr_import__("./vue", {"importedNames":["cls"]});
349
- foo(class cls { constructor() { console.log(cls) } })"
350
- ` )
408
+ expect ( result ?. code ) . toMatchInlineSnapshot (
409
+ `"const __vite_ssr_import_0__ = await __vite_ssr_import__("./vue", {"importedNames":["cls"]});foo(class cls { constructor() { console.log(cls) } })"` ,
410
+ )
351
411
} )
352
412
353
413
test ( 'do not rewrite catch clause' , async ( ) => {
354
414
const result = await ssrTransformSimple (
355
415
`import {error} from './dependency';try {} catch(error) {}` ,
356
416
)
357
- expect ( result ?. code ) . toMatchInlineSnapshot ( `
358
- "const __vite_ssr_import_0__ = await __vite_ssr_import__("./dependency", {"importedNames":["error"]});
359
- try {} catch(error) {}"
360
- ` )
417
+ expect ( result ?. code ) . toMatchInlineSnapshot (
418
+ `"const __vite_ssr_import_0__ = await __vite_ssr_import__("./dependency", {"importedNames":["error"]});try {} catch(error) {}"` ,
419
+ )
361
420
expect ( result ?. deps ) . toEqual ( [ './dependency' ] )
362
421
} )
363
422
@@ -368,8 +427,7 @@ test('should declare variable for imported super class', async () => {
368
427
`import { Foo } from './dependency';` + `class A extends Foo {}` ,
369
428
) ,
370
429
) . toMatchInlineSnapshot ( `
371
- "const __vite_ssr_import_0__ = await __vite_ssr_import__("./dependency", {"importedNames":["Foo"]});
372
- const Foo = __vite_ssr_import_0__.Foo;
430
+ "const __vite_ssr_import_0__ = await __vite_ssr_import__("./dependency", {"importedNames":["Foo"]});const Foo = __vite_ssr_import_0__.Foo;
373
431
class A extends Foo {}"
374
432
` )
375
433
@@ -382,8 +440,7 @@ test('should declare variable for imported super class', async () => {
382
440
`export class B extends Foo {}` ,
383
441
) ,
384
442
) . toMatchInlineSnapshot ( `
385
- "const __vite_ssr_import_0__ = await __vite_ssr_import__("./dependency", {"importedNames":["Foo"]});
386
- const Foo = __vite_ssr_import_0__.Foo;
443
+ "const __vite_ssr_import_0__ = await __vite_ssr_import__("./dependency", {"importedNames":["Foo"]});const Foo = __vite_ssr_import_0__.Foo;
387
444
class A extends Foo {};
388
445
class B extends Foo {}
389
446
Object.defineProperty(__vite_ssr_exports__, "B", { enumerable: true, configurable: true, get(){ return B }});
@@ -448,9 +505,7 @@ test('sourcemap is correct for hoisted imports', async () => {
448
505
const result = ( await ssrTransform ( code , null , 'input.js' , code ) ) !
449
506
450
507
expect ( result . code ) . toMatchInlineSnapshot ( `
451
- "const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["foo"]});
452
- const __vite_ssr_import_1__ = await __vite_ssr_import__("vue2", {"importedNames":["bar"]});
453
-
508
+ "const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["foo"]});const __vite_ssr_import_1__ = await __vite_ssr_import__("vue2", {"importedNames":["bar"]});
454
509
455
510
456
511
console.log((0,__vite_ssr_import_0__.foo), (0,__vite_ssr_import_1__.bar));
@@ -465,7 +520,7 @@ test('sourcemap is correct for hoisted imports', async () => {
465
520
column : 0 ,
466
521
name : null ,
467
522
} )
468
- expect ( originalPositionFor ( traceMap , { line : 2 , column : 0 } ) ) . toStrictEqual ( {
523
+ expect ( originalPositionFor ( traceMap , { line : 1 , column : 90 } ) ) . toStrictEqual ( {
469
524
source : 'input.js' ,
470
525
line : 6 ,
471
526
column : 0 ,
@@ -532,8 +587,7 @@ test('overwrite bindings', async () => {
532
587
`function g() { const f = () => { const inject = true }; console.log(inject) }\n` ,
533
588
) ,
534
589
) . toMatchInlineSnapshot ( `
535
- "const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["inject"]});
536
- const a = { inject: __vite_ssr_import_0__.inject };
590
+ "const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["inject"]});const a = { inject: __vite_ssr_import_0__.inject };
537
591
const b = { test: __vite_ssr_import_0__.inject };
538
592
function c() { const { test: inject } = { test: true }; console.log(inject) }
539
593
const d = __vite_ssr_import_0__.inject;
@@ -561,9 +615,8 @@ function c({ _ = bar() + foo() }) {}
561
615
` ,
562
616
) ,
563
617
) . toMatchInlineSnapshot ( `
564
- "const __vite_ssr_import_0__ = await __vite_ssr_import__("foo", {"importedNames":["foo","bar"]});
565
-
566
-
618
+ "
619
+ const __vite_ssr_import_0__ = await __vite_ssr_import__("foo", {"importedNames":["foo","bar"]});
567
620
const a = ({ _ = (0,__vite_ssr_import_0__.foo)() }) => {};
568
621
function b({ _ = (0,__vite_ssr_import_0__.bar)() }) {}
569
622
function c({ _ = (0,__vite_ssr_import_0__.bar)() + (0,__vite_ssr_import_0__.foo)() }) {}
@@ -583,9 +636,8 @@ const a = () => {
583
636
` ,
584
637
) ,
585
638
) . toMatchInlineSnapshot ( `
586
- "const __vite_ssr_import_0__ = await __vite_ssr_import__("foo", {"importedNames":["n"]});
587
-
588
-
639
+ "
640
+ const __vite_ssr_import_0__ = await __vite_ssr_import__("foo", {"importedNames":["n"]});
589
641
const a = () => {
590
642
const { type: n = 'bar' } = {};
591
643
console.log(n)
@@ -606,9 +658,8 @@ const foo = {}
606
658
` ,
607
659
) ,
608
660
) . toMatchInlineSnapshot ( `
609
- "const __vite_ssr_import_0__ = await __vite_ssr_import__("foo", {"importedNames":["n","m"]});
610
-
611
-
661
+ "
662
+ const __vite_ssr_import_0__ = await __vite_ssr_import__("foo", {"importedNames":["n","m"]});
612
663
const foo = {};
613
664
614
665
{
@@ -649,9 +700,8 @@ objRest()
649
700
` ,
650
701
) ,
651
702
) . toMatchInlineSnapshot ( `
652
- "const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["remove","add","get","set","rest","objRest"]});
653
-
654
-
703
+ "
704
+ const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["remove","add","get","set","rest","objRest"]});
655
705
656
706
function a() {
657
707
const {
@@ -699,9 +749,8 @@ const obj = {
699
749
` ,
700
750
) ,
701
751
) . toMatchInlineSnapshot ( `
702
- "const __vite_ssr_import_0__ = await __vite_ssr_import__("foo", {"importedNames":["default"]});
703
-
704
-
752
+ "
753
+ const __vite_ssr_import_0__ = await __vite_ssr_import__("foo", {"importedNames":["default"]});
705
754
706
755
const bar = 'bar';
707
756
@@ -731,9 +780,8 @@ class A {
731
780
` ,
732
781
) ,
733
782
) . toMatchInlineSnapshot ( `
734
- "const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["remove","add"]});
735
-
736
-
783
+ "
784
+ const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["remove","add"]});
737
785
738
786
const add = __vite_ssr_import_0__.add;
739
787
const remove = __vite_ssr_import_0__.remove;
@@ -763,9 +811,8 @@ class A {
763
811
` ,
764
812
) ,
765
813
) . toMatchInlineSnapshot ( `
766
- "const __vite_ssr_import_0__ = await __vite_ssr_import__("foo", {"importedNames":["default"]});
767
-
768
-
814
+ "
815
+ const __vite_ssr_import_0__ = await __vite_ssr_import__("foo", {"importedNames":["default"]});
769
816
770
817
const bar = 'bar';
771
818
@@ -809,9 +856,8 @@ bbb()
809
856
` ,
810
857
) ,
811
858
) . toMatchInlineSnapshot ( `
812
- "const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["aaa","bbb","ccc","ddd"]});
813
-
814
-
859
+ "
860
+ const __vite_ssr_import_0__ = await __vite_ssr_import__("vue", {"importedNames":["aaa","bbb","ccc","ddd"]});
815
861
816
862
function foobar() {
817
863
ddd();
@@ -856,8 +902,6 @@ test('jsx', async () => {
856
902
. toMatchInlineSnapshot ( `
857
903
"const __vite_ssr_import_0__ = await __vite_ssr_import__("react", {"importedNames":["default"]});
858
904
const __vite_ssr_import_1__ = await __vite_ssr_import__("foo", {"importedNames":["Foo","Slot"]});
859
-
860
-
861
905
function Bar({ Slot: Slot2 = /* @__PURE__ */ __vite_ssr_import_0__.default.createElement((0,__vite_ssr_import_1__.Foo), null) }) {
862
906
return /* @__PURE__ */ __vite_ssr_import_0__.default.createElement(__vite_ssr_import_0__.default.Fragment, null, /* @__PURE__ */ __vite_ssr_import_0__.default.createElement(Slot2, null));
863
907
}
@@ -930,8 +974,7 @@ import foo from "foo"`,
930
974
) ,
931
975
) . toMatchInlineSnapshot ( `
932
976
"#!/usr/bin/env node
933
- const __vite_ssr_import_0__ = await __vite_ssr_import__("foo", {"importedNames":["default"]});
934
- console.log((0,__vite_ssr_import_0__.default));
977
+ const __vite_ssr_import_0__ = await __vite_ssr_import__("foo", {"importedNames":["default"]});console.log((0,__vite_ssr_import_0__.default));
935
978
"
936
979
` )
937
980
} )
@@ -946,7 +989,6 @@ foo()`,
946
989
) . toMatchInlineSnapshot ( `
947
990
"#!/usr/bin/env node
948
991
const __vite_ssr_import_0__ = await __vite_ssr_import__("foo", {"importedNames":["foo"]});
949
-
950
992
(0,__vite_ssr_import_0__.foo)()"
951
993
` )
952
994
} )
@@ -982,7 +1024,6 @@ export class Test {
982
1024
983
1025
expect ( await ssrTransformSimpleCode ( code ) ) . toMatchInlineSnapshot ( `
984
1026
"const __vite_ssr_import_0__ = await __vite_ssr_import__("foobar", {"importedNames":["foo","bar"]});
985
-
986
1027
if (false) {
987
1028
const foo = 'foo';
988
1029
console.log(foo)
@@ -1023,9 +1064,8 @@ function test() {
1023
1064
return [foo, bar]
1024
1065
}` ) ,
1025
1066
) . toMatchInlineSnapshot ( `
1026
- "const __vite_ssr_import_0__ = await __vite_ssr_import__("foobar", {"importedNames":["foo","bar"]});
1027
-
1028
-
1067
+ "
1068
+ const __vite_ssr_import_0__ = await __vite_ssr_import__("foobar", {"importedNames":["foo","bar"]});
1029
1069
function test() {
1030
1070
if (true) {
1031
1071
var foo = () => { var why = 'would' }, bar = 'someone'
@@ -1050,9 +1090,8 @@ function test() {
1050
1090
return bar;
1051
1091
}` ) ,
1052
1092
) . toMatchInlineSnapshot ( `
1053
- "const __vite_ssr_import_0__ = await __vite_ssr_import__("foobar", {"importedNames":["foo","bar","baz"]});
1054
-
1055
-
1093
+ "
1094
+ const __vite_ssr_import_0__ = await __vite_ssr_import__("foobar", {"importedNames":["foo","bar","baz"]});
1056
1095
function test() {
1057
1096
[__vite_ssr_import_0__.foo];
1058
1097
{
@@ -1082,9 +1121,8 @@ for (const test in tests) {
1082
1121
console.log(test)
1083
1122
}` ) ,
1084
1123
) . toMatchInlineSnapshot ( `
1085
- "const __vite_ssr_import_0__ = await __vite_ssr_import__("./test.js", {"importedNames":["test"]});
1086
-
1087
-
1124
+ "
1125
+ const __vite_ssr_import_0__ = await __vite_ssr_import__("./test.js", {"importedNames":["test"]});
1088
1126
1089
1127
for (const test of tests) {
1090
1128
console.log(test)
@@ -1114,9 +1152,8 @@ const Baz = class extends Foo {}
1114
1152
` ,
1115
1153
)
1116
1154
expect ( result ?. code ) . toMatchInlineSnapshot ( `
1117
- "const __vite_ssr_import_0__ = await __vite_ssr_import__("./foo", {"importedNames":["default","Bar"]});
1118
-
1119
-
1155
+ "
1156
+ const __vite_ssr_import_0__ = await __vite_ssr_import__("./foo", {"importedNames":["default","Bar"]});
1120
1157
1121
1158
console.log((0,__vite_ssr_import_0__.default), (0,__vite_ssr_import_0__.Bar));
1122
1159
const obj = {
@@ -1135,9 +1172,8 @@ test('import assertion attribute', async () => {
1135
1172
import('./bar.json', { with: { type: 'json' } });
1136
1173
` ) ,
1137
1174
) . toMatchInlineSnapshot ( `
1138
- "const __vite_ssr_import_0__ = await __vite_ssr_import__("./foo.json");
1139
-
1140
-
1175
+ "
1176
+ const __vite_ssr_import_0__ = await __vite_ssr_import__("./foo.json");
1141
1177
__vite_ssr_dynamic_import__('./bar.json', { with: { type: 'json' } });
1142
1178
"
1143
1179
` )
@@ -1157,14 +1193,11 @@ console.log(foo + 2)
1157
1193
` ) ,
1158
1194
) . toMatchInlineSnapshot ( `
1159
1195
"const __vite_ssr_import_0__ = await __vite_ssr_import__("./foo", {"importedNames":["foo"]});
1160
-
1161
1196
console.log(__vite_ssr_import_0__.foo + 1);
1162
- const __vite_ssr_import_1__ = await __vite_ssr_import__("./a");
1163
- __vite_ssr_exportAll__(__vite_ssr_import_1__);
1197
+ const __vite_ssr_import_1__ = await __vite_ssr_import__("./a");__vite_ssr_exportAll__(__vite_ssr_import_1__);
1164
1198
;
1165
1199
1166
- const __vite_ssr_import_2__ = await __vite_ssr_import__("./b");
1167
- __vite_ssr_exportAll__(__vite_ssr_import_2__);
1200
+ const __vite_ssr_import_2__ = await __vite_ssr_import__("./b");__vite_ssr_exportAll__(__vite_ssr_import_2__);
1168
1201
;
1169
1202
console.log(__vite_ssr_import_0__.foo + 2)
1170
1203
"
@@ -1180,12 +1213,10 @@ export * as bar from './bar'
1180
1213
console.log(bar)
1181
1214
` ) ,
1182
1215
) . toMatchInlineSnapshot ( `
1183
- "const __vite_ssr_import_0__ = await __vite_ssr_import__("./foo", {"importedNames":["foo"]});
1184
-
1185
-
1216
+ "
1217
+ const __vite_ssr_import_0__ = await __vite_ssr_import__("./foo", {"importedNames":["foo"]});
1186
1218
__vite_ssr_exports__.default = (0,__vite_ssr_import_0__.foo)();
1187
1219
const __vite_ssr_import_1__ = await __vite_ssr_import__("./bar");
1188
-
1189
1220
Object.defineProperty(__vite_ssr_exports__, "bar", { enumerable: true, configurable: true, get(){ return __vite_ssr_import_1__ }});;
1190
1221
console.log(bar)
1191
1222
"
@@ -1256,9 +1287,8 @@ switch (1) {
1256
1287
}
1257
1288
` ) ,
1258
1289
) . toMatchInlineSnapshot ( `
1259
- "const __vite_ssr_import_0__ = await __vite_ssr_import__("./f", {"importedNames":["f"]});
1260
-
1261
-
1290
+ "
1291
+ const __vite_ssr_import_0__ = await __vite_ssr_import__("./f", {"importedNames":["f"]});
1262
1292
1263
1293
let x = 0;
1264
1294
0 commit comments