/
ssrTransform.spec.ts
335 lines (309 loc) · 10.6 KB
/
ssrTransform.spec.ts
1
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import { traverseHtml } from '../../plugins/html'
import { ssrTransform } from '../ssrTransform'
test('default import', async () => {
expect(
(
await ssrTransform(
`import foo from 'vue';console.log(foo.bar)`,
null,
null
)
).code
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
console.log(__vite_ssr_import_0__.default.bar)"
`)
})
test('named import', async () => {
expect(
(
await ssrTransform(
`import { ref } from 'vue';function foo() { return ref(0) }`,
null,
null
)
).code
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
function foo() { return __vite_ssr_import_0__.ref(0) }"
`)
})
test('namespace import', async () => {
expect(
(
await ssrTransform(
`import * as vue from 'vue';function foo() { return vue.ref(0) }`,
null,
null
)
).code
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
function foo() { return __vite_ssr_import_0__.ref(0) }"
`)
})
test('export function declaration', async () => {
expect((await ssrTransform(`export function foo() {}`, null, null)).code)
.toMatchInlineSnapshot(`
"function foo() {}
Object.defineProperty(__vite_ssr_exports__, \\"foo\\", { enumerable: true, configurable: true, get(){ return foo }});"
`)
})
test('export class declaration', async () => {
expect((await ssrTransform(`export class foo {}`, null, null)).code)
.toMatchInlineSnapshot(`
"class foo {}
Object.defineProperty(__vite_ssr_exports__, \\"foo\\", { enumerable: true, configurable: true, get(){ return foo }});"
`)
})
test('export var declaration', async () => {
expect((await ssrTransform(`export const a = 1, b = 2`, null, null)).code)
.toMatchInlineSnapshot(`
"const a = 1, b = 2
Object.defineProperty(__vite_ssr_exports__, \\"a\\", { enumerable: true, configurable: true, get(){ return a }});
Object.defineProperty(__vite_ssr_exports__, \\"b\\", { enumerable: true, configurable: true, get(){ return b }});"
`)
})
test('export named', async () => {
expect(
(await ssrTransform(`const a = 1, b = 2; export { a, b as c }`, null, null))
.code
).toMatchInlineSnapshot(`
"const a = 1, b = 2;
Object.defineProperty(__vite_ssr_exports__, \\"a\\", { enumerable: true, configurable: true, get(){ return a }});
Object.defineProperty(__vite_ssr_exports__, \\"c\\", { enumerable: true, configurable: true, get(){ return b }});"
`)
})
test('export named from', async () => {
expect(
(await ssrTransform(`export { ref, computed as c } from 'vue'`, null, null))
.code
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
Object.defineProperty(__vite_ssr_exports__, \\"ref\\", { enumerable: true, configurable: true, get(){ return __vite_ssr_import_0__.ref }});
Object.defineProperty(__vite_ssr_exports__, \\"c\\", { enumerable: true, configurable: true, get(){ return __vite_ssr_import_0__.computed }});"
`)
})
test('named exports of imported binding', async () => {
expect(
(
await ssrTransform(
`import {createApp} from 'vue';export {createApp}`,
null,
null
)
).code
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
Object.defineProperty(__vite_ssr_exports__, \\"createApp\\", { enumerable: true, configurable: true, get(){ return __vite_ssr_import_0__.createApp }});"
`)
})
test('export * from', async () => {
expect(
(
await ssrTransform(
`export * from 'vue'\n` + `export * from 'react'`,
null,
null
)
).code
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
__vite_ssr_exportAll__(__vite_ssr_import_0__);
const __vite_ssr_import_1__ = await __vite_ssr_import__(\\"react\\");
__vite_ssr_exportAll__(__vite_ssr_import_1__);"
`)
})
test('export * as from', async () => {
expect((await ssrTransform(`export * as foo from 'vue'`, null, null)).code)
.toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
Object.defineProperty(__vite_ssr_exports__, \\"foo\\", { enumerable: true, configurable: true, get(){ return __vite_ssr_import_0__ }});"
`)
})
test('export default', async () => {
expect(
(await ssrTransform(`export default {}`, null, null)).code
).toMatchInlineSnapshot(`"__vite_ssr_exports__.default = {}"`)
})
test('import.meta', async () => {
expect(
(await ssrTransform(`console.log(import.meta.url)`, null, null)).code
).toMatchInlineSnapshot(`"console.log(__vite_ssr_import_meta__.url)"`)
})
test('dynamic import', async () => {
const result = await ssrTransform(
`export const i = () => import('./foo')`,
null,
null
)
expect(result.code).toMatchInlineSnapshot(`
"const i = () => __vite_ssr_dynamic_import__('./foo')
Object.defineProperty(__vite_ssr_exports__, \\"i\\", { enumerable: true, configurable: true, get(){ return i }});"
`)
expect(result.deps).toEqual([])
expect(result.dynamicDeps).toEqual(['./foo'])
})
test('do not rewrite method definition', async () => {
const result = await ssrTransform(
`import { fn } from 'vue';class A { fn() { fn() } }`,
null,
null
)
expect(result.code).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
class A { fn() { __vite_ssr_import_0__.fn() } }"
`)
expect(result.deps).toEqual(['vue'])
})
test('do not rewrite when variable is in scope', async () => {
const result = await ssrTransform(
`import { fn } from 'vue';function A(){ const fn = () => {}; return { fn }; }`,
null,
null
)
expect(result.code).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
function A(){ const fn = () => {}; return { fn }; }"
`)
expect(result.deps).toEqual(['vue'])
})
test('do not rewrite when function declaration is in scope', async () => {
const result = await ssrTransform(
`import { fn } from 'vue';function A(){ function fn() {}; return { fn }; }`,
null,
null
)
expect(result.code).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
function A(){ function fn() {}; return { fn }; }"
`)
expect(result.deps).toEqual(['vue'])
})
test('do not rewrite catch clause', async () => {
const result = await ssrTransform(
`import {error} from './dependency';try {} catch(error) {}`,
null,
null
)
expect(result.code).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"./dependency\\");
try {} catch(error) {}"
`)
expect(result.deps).toEqual(['./dependency'])
})
// #2221
test('should declare variable for imported super class', async () => {
expect(
(
await ssrTransform(
`import { Foo } from './dependency';` + `class A extends Foo {}`,
null,
null
)
).code
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"./dependency\\");
const Foo = __vite_ssr_import_0__.Foo;
class A extends Foo {}"
`)
// exported classes: should prepend the declaration at root level, before the
// first class that uses the binding
expect(
(
await ssrTransform(
`import { Foo } from './dependency';` +
`export default class A extends Foo {}\n` +
`export class B extends Foo {}`,
null,
null
)
).code
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"./dependency\\");
const Foo = __vite_ssr_import_0__.Foo;
class A extends Foo {}
class B extends Foo {}
Object.defineProperty(__vite_ssr_exports__, \\"default\\", { enumerable: true, value: A });
Object.defineProperty(__vite_ssr_exports__, \\"B\\", { enumerable: true, configurable: true, get(){ return B }});"
`)
})
// #4049
test('should handle default export variants', async () => {
// default anonymous functions
expect(
(await ssrTransform(`export default function() {}\n`, null, null)).code
).toMatchInlineSnapshot(`
"__vite_ssr_exports__.default = function() {}
"
`)
// default anonymous class
expect((await ssrTransform(`export default class {}\n`, null, null)).code)
.toMatchInlineSnapshot(`
"__vite_ssr_exports__.default = class {}
"
`)
// default named functions
expect(
(
await ssrTransform(
`export default function foo() {}\n` +
`foo.prototype = Object.prototype;`,
null,
null
)
).code
).toMatchInlineSnapshot(`
"function foo() {}
foo.prototype = Object.prototype;
Object.defineProperty(__vite_ssr_exports__, \\"default\\", { enumerable: true, value: foo });"
`)
// default named classes
expect(
(
await ssrTransform(
`export default class A {}\n` + `export class B extends A {}`,
null,
null
)
).code
).toMatchInlineSnapshot(`
"class A {}
class B extends A {}
Object.defineProperty(__vite_ssr_exports__, \\"default\\", { enumerable: true, value: A });
Object.defineProperty(__vite_ssr_exports__, \\"B\\", { enumerable: true, configurable: true, get(){ return B }});"
`)
})
test('sourcemap source', async () => {
expect(
(await ssrTransform(`export const a = 1`, null, 'input.js')).map.sources
).toStrictEqual(['input.js'])
})
test('overwrite bindings', async () => {
expect(
(
await ssrTransform(
`import { inject } from 'vue';` +
`const a = { inject }\n` +
`const b = { test: inject }\n` +
`function c() { const { test: inject } = { test: true }; console.log(inject) }\n` +
`const d = inject \n` +
`function f() { console.log(inject) }\n` +
`function e() { const { inject } = { inject: true } }\n` +
`function g() { const f = () => { const inject = true }; console.log(inject) }\n`,
null,
null
)
).code
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
const a = { inject: __vite_ssr_import_0__.inject }
const b = { test: __vite_ssr_import_0__.inject }
function c() { const { test: inject } = { test: true }; console.log(inject) }
const d = __vite_ssr_import_0__.inject
function f() { console.log(__vite_ssr_import_0__.inject) }
function e() { const { inject } = { inject: true } }
function g() { const f = () => { const inject = true }; console.log(__vite_ssr_import_0__.inject) }
"
`)
})