forked from vitejs/vite-plugin-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrap-export.test.ts
More file actions
307 lines (284 loc) · 7.88 KB
/
wrap-export.test.ts
File metadata and controls
307 lines (284 loc) · 7.88 KB
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
import { parseAstAsync } from 'vite'
import { describe, expect, test } from 'vitest'
import { debugSourceMap } from './test-utils'
import {
type TransformWrapExportOptions,
transformWrapExport,
} from './wrap-export'
async function testTransform(
input: string,
options?: Omit<TransformWrapExportOptions, 'runtime'>,
) {
const ast = await parseAstAsync(input)
const { output } = transformWrapExport(input, ast, {
runtime: (value, name) =>
`$$wrap(${value}, "<id>", ${JSON.stringify(name)})`,
ignoreExportAllDeclaration: true,
...options,
})
if (process.env['DEBUG_SOURCEMAP']) {
await debugSourceMap(output)
}
return output.hasChanged() && output.toString()
}
async function testTransformNames(input: string) {
const ast = await parseAstAsync(input)
const result = transformWrapExport(input, ast, {
runtime: (value, name) =>
`$$wrap(${value}, "<id>", ${JSON.stringify(name)})`,
ignoreExportAllDeclaration: true,
})
return result.exportNames
}
describe(transformWrapExport, () => {
test('basic', async () => {
const input = `
export const Arrow = () => {};
export default "hi";
export function Fn() {};
export async function AsyncFn() {};
export class Cls {};
`
expect(await testTransform(input)).toMatchInlineSnapshot(`
"
let Arrow = () => {};
const $$default = "hi";
function Fn() {};
async function AsyncFn() {};
class Cls {};
Arrow = /* #__PURE__ */ $$wrap(Arrow, "<id>", "Arrow");
export { Arrow };
Fn = /* #__PURE__ */ $$wrap(Fn, "<id>", "Fn");
export { Fn };
AsyncFn = /* #__PURE__ */ $$wrap(AsyncFn, "<id>", "AsyncFn");
export { AsyncFn };
Cls = /* #__PURE__ */ $$wrap(Cls, "<id>", "Cls");
export { Cls };
;
const $$wrap_$$default = /* #__PURE__ */ $$wrap($$default, "<id>", "default");
export { $$wrap_$$default as default };
"
`)
expect(await testTransformNames(input)).toMatchInlineSnapshot(`
[
"Arrow",
"default",
"Fn",
"AsyncFn",
"Cls",
]
`)
})
test('preserve reference', async () => {
const input = `
export let count = 0;
export function changeCount() {
count += 1;
}
`
expect(await testTransform(input)).toMatchInlineSnapshot(`
"
let count = 0;
function changeCount() {
count += 1;
}
count = /* #__PURE__ */ $$wrap(count, "<id>", "count");
export { count };
changeCount = /* #__PURE__ */ $$wrap(changeCount, "<id>", "changeCount");
export { changeCount };
"
`)
})
test('export destructuring', async () => {
const input = `
export const { x, y: [z] } = { x: 0, y: [1] };
`
expect(await testTransform(input)).toMatchInlineSnapshot(`
"
let { x, y: [z] } = { x: 0, y: [1] };
x = /* #__PURE__ */ $$wrap(x, "<id>", "x");
export { x };
z = /* #__PURE__ */ $$wrap(z, "<id>", "z");
export { z };
"
`)
})
test('default function', async () => {
const input = `export default function Fn() {}`
expect(await testTransform(input)).toMatchInlineSnapshot(
`
"function Fn() {};
const $$wrap_Fn = /* #__PURE__ */ $$wrap(Fn, "<id>", "default");
export { $$wrap_Fn as default };
"
`,
)
})
test('default anonymous function', async () => {
const input = `export default function () {}`
expect(await testTransform(input)).toMatchInlineSnapshot(
`
"const $$default = function () {};
const $$wrap_$$default = /* #__PURE__ */ $$wrap($$default, "<id>", "default");
export { $$wrap_$$default as default };
"
`,
)
})
test('default class', async () => {
const input = `export default class Cls {}`
expect(await testTransform(input)).toMatchInlineSnapshot(
`
"class Cls {};
const $$wrap_Cls = /* #__PURE__ */ $$wrap(Cls, "<id>", "default");
export { $$wrap_Cls as default };
"
`,
)
})
test('export simple', async () => {
const input = `
const x = 0;
export { x }
`
expect(await testTransform(input)).toMatchInlineSnapshot(`
"
const x = 0;
;
const $$wrap_x = /* #__PURE__ */ $$wrap(x, "<id>", "x");
export { $$wrap_x as x };
"
`)
})
test('export rename', async () => {
const input = `
const x = 0;
export { x as y }
`
expect(await testTransform(input)).toMatchInlineSnapshot(`
"
const x = 0;
;
const $$wrap_x = /* #__PURE__ */ $$wrap(x, "<id>", "y");
export { $$wrap_x as y };
"
`)
})
test('re-export simple', async () => {
const input = `export { x } from "./dep"`
expect(await testTransform(input)).toMatchInlineSnapshot(`
";
import { x as $$import_x } from "./dep";
const $$wrap_$$import_x = /* #__PURE__ */ $$wrap($$import_x, "<id>", "x");
export { $$wrap_$$import_x as x };
"
`)
})
test('re-export rename', async () => {
const input = `export { x as y } from "./dep"`
expect(await testTransform(input)).toMatchInlineSnapshot(`
";
import { x as $$import_x } from "./dep";
const $$wrap_$$import_x = /* #__PURE__ */ $$wrap($$import_x, "<id>", "y");
export { $$wrap_$$import_x as y };
"
`)
})
test('re-export all simple', async () => {
const input = `export * from "./dep"`
expect(await testTransform(input)).toMatchInlineSnapshot(`false`)
})
test('re-export all rename', async () => {
const input = `export * as all from "./dep"`
expect(await testTransform(input)).toMatchInlineSnapshot(`false`)
})
test('filter', async () => {
const input = `
export const a = 0;
export const b = 0, b_no = 0;
export { c } from "./c";
export { a as aa };
`
const result = await testTransform(input, {
filter: (name) => !name.endsWith('no'),
})
expect(result).toMatchInlineSnapshot(`
"
let a = 0;
let b = 0, b_no = 0;
a = /* #__PURE__ */ $$wrap(a, "<id>", "a");
export { a };
b = /* #__PURE__ */ $$wrap(b, "<id>", "b");
export { b };
export { b_no };
;
import { c as $$import_c } from "./c";
const $$wrap_$$import_c = /* #__PURE__ */ $$wrap($$import_c, "<id>", "c");
export { $$wrap_$$import_c as c };
const $$wrap_a = /* #__PURE__ */ $$wrap(a, "<id>", "aa");
export { $$wrap_a as aa };
"
`)
})
test('filter meta', async () => {
const input = `
export const a = 0;
export const b = function() {}
export const c = () => {}
export default function d() {}
`
const result = await testTransform(input, {
filter: (_name, meta) => !!(meta.isFunction && meta.declName),
})
expect(result).toMatchInlineSnapshot(`
"
let a = 0;
let b = function() {}
let c = () => {}
function d() {}
export { a };
b = /* #__PURE__ */ $$wrap(b, "<id>", "b");
export { b };
c = /* #__PURE__ */ $$wrap(c, "<id>", "c");
export { c };
;
const $$wrap_d = /* #__PURE__ */ $$wrap(d, "<id>", "default");
export { $$wrap_d as default };
"
`)
})
test('filter meta 2', async () => {
const input = `
export default () => {}
`
const result = await testTransform(input, {
filter: (_name, meta) => !!(meta.isFunction && meta.declName),
})
expect(result).toMatchInlineSnapshot(`
"
const $$default = () => {}
;
export { $$default as default };
"
`)
})
test('filter defaultExportIdentifierName', async () => {
const input = `
const Page = () => {}
export default Page;
`
expect(
await testTransform(input, {
filter: (_name, meta) => meta.defaultExportIdentifierName === 'Page',
}),
).toMatchInlineSnapshot(`
"
const Page = () => {}
const $$default = Page;
;
const $$wrap_$$default = /* #__PURE__ */ $$wrap($$default, "<id>", "default");
export { $$wrap_$$default as default };
"
`)
})
})