forked from vitejs/vite
-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdefine.spec.ts
146 lines (131 loc) · 5.09 KB
/
define.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
import { describe, expect, test } from 'vitest'
import { definePlugin } from '../../plugins/define'
import { resolveConfig } from '../../config'
import { PartialEnvironment } from '../../baseEnvironment'
async function createDefinePluginTransform(
define: Record<string, any> = {},
build = true,
ssr = false,
) {
const config = await resolveConfig(
{ configFile: false, define },
build ? 'build' : 'serve',
)
const instance = definePlugin(config)
const environment = new PartialEnvironment(ssr ? 'ssr' : 'client', config)
return async (code: string) => {
// @ts-expect-error transform should exist
const result = await instance.transform.call(
{ environment },
code,
'foo.ts',
)
return result?.code || result
}
}
describe('definePlugin', () => {
test('replaces custom define', async () => {
const transform = await createDefinePluginTransform({
__APP_VERSION__: JSON.stringify('1.0'),
})
expect(await transform('const version = __APP_VERSION__ ;')).toBe(
'const version = "1.0";\n',
)
expect(await transform('const version = __APP_VERSION__;')).toBe(
'const version = "1.0";\n',
)
})
test('should not replace if not defined', async () => {
const transform = await createDefinePluginTransform({
__APP_VERSION__: JSON.stringify('1.0'),
})
expect(await transform('const version = "1.0";')).toBe(undefined)
expect(await transform('const version = import.meta.SOMETHING')).toBe(
undefined,
)
})
test('replaces import.meta.env.SSR with false', async () => {
const transform = await createDefinePluginTransform()
expect(await transform('const isSSR = import.meta.env.SSR;')).toBe(
'const isSSR = false;\n',
)
})
test('preserve import.meta.hot with override', async () => {
// assert that the default behavior is to replace import.meta.hot with undefined
const transform = await createDefinePluginTransform()
expect(await transform('const hot = import.meta.hot;')).toBe(
'const hot = undefined;\n',
)
// assert that we can specify a user define to preserve import.meta.hot
const overrideTransform = await createDefinePluginTransform({
'import.meta.hot': 'import.meta.hot',
})
expect(await overrideTransform('const hot = import.meta.hot;')).toBe(
'const hot = import.meta.hot;\n',
)
})
test('replace import.meta.env.UNKNOWN with undefined', async () => {
const transform = await createDefinePluginTransform()
expect(await transform('const foo = import.meta.env.UNKNOWN;')).toBe(
'const foo = undefined ;\n',
)
})
test('leave import.meta.env["UNKNOWN"] to runtime', async () => {
const transform = await createDefinePluginTransform()
expect(await transform('const foo = import.meta.env["UNKNOWN"];')).toMatch(
/const __vite_import_meta_env__ = .*;\nconst foo = __vite_import_meta_env__\["UNKNOWN"\];/,
)
})
test('preserve import.meta.env.UNKNOWN with override', async () => {
const transform = await createDefinePluginTransform({
'import.meta.env.UNKNOWN': 'import.meta.env.UNKNOWN',
})
expect(await transform('const foo = import.meta.env.UNKNOWN;')).toBe(
'const foo = import.meta.env.UNKNOWN;\n',
)
})
test('replace import.meta.env when it is a invalid json', async () => {
const transform = await createDefinePluginTransform({
'import.meta.env.LEGACY': '__VITE_IS_LEGACY__',
})
expect(
await transform(
'const isLegacy = import.meta.env.LEGACY;\nimport.meta.env.UNDEFINED && console.log(import.meta.env.UNDEFINED);',
),
).toMatchInlineSnapshot(`
"const isLegacy = __VITE_IS_LEGACY__;
undefined && console.log(undefined );
"
`)
})
test('replace bare import.meta.env', async () => {
const transform = await createDefinePluginTransform()
expect(await transform('const env = import.meta.env;')).toMatch(
/const __vite_import_meta_env__ = .*;\nconst env = __vite_import_meta_env__;/,
)
})
test('already has marker', async () => {
const transform = await createDefinePluginTransform()
expect(
await transform(
'console.log(__vite_import_meta_env__);\nconst env = import.meta.env;',
),
).toMatch(
/const __vite_import_meta_env__1 = .*;\nconsole.log\(__vite_import_meta_env__\);\nconst env = __vite_import_meta_env__1;/,
)
expect(
await transform(
'console.log(__vite_import_meta_env__, __vite_import_meta_env__1);\n const env = import.meta.env;',
),
).toMatch(
/const __vite_import_meta_env__2 = .*;\nconsole.log\(__vite_import_meta_env__, __vite_import_meta_env__1\);\nconst env = __vite_import_meta_env__2;/,
)
expect(
await transform(
'console.log(__vite_import_meta_env__);\nconst env = import.meta.env;\nconsole.log(import.meta.env.UNDEFINED);',
),
).toMatch(
/const __vite_import_meta_env__1 = .*;\nconsole.log\(__vite_import_meta_env__\);\nconst env = __vite_import_meta_env__1;\nconsole.log\(undefined {26}\);/,
)
})
})