Skip to content

Commit f35a23f

Browse files
committed
test: run tests with all native plugins
1 parent 38f0c09 commit f35a23f

File tree

5 files changed

+183
-18
lines changed

5 files changed

+183
-18
lines changed

packages/vite/src/node/__tests__/plugins/define.spec.ts

Lines changed: 157 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { describe, expect, test } from 'vitest'
2+
import { rolldown } from 'rolldown'
23
import { definePlugin } from '../../plugins/define'
34
import { resolveConfig } from '../../config'
45
import { PartialEnvironment } from '../../baseEnvironment'
@@ -16,17 +17,48 @@ async function createDefinePluginTransform(
1617
const environment = new PartialEnvironment(ssr ? 'ssr' : 'client', config)
1718

1819
return async (code: string) => {
19-
// @ts-expect-error transform.handler should exist
20-
const result = await instance.transform.handler.call(
21-
{ environment },
22-
code,
23-
'foo.ts',
24-
)
25-
return result?.code || result
20+
if (process.env._VITE_TEST_JS_PLUGIN) {
21+
// @ts-expect-error transform.handler should exist
22+
const result = await instance.transform.handler.call(
23+
{ environment },
24+
code,
25+
'foo.ts',
26+
)
27+
return result?.code || result
28+
} else {
29+
const bundler = await rolldown({
30+
input: 'entry.js',
31+
plugins: [
32+
{
33+
name: 'test',
34+
resolveId(id) {
35+
if (id === 'entry.js') {
36+
return '\0' + id
37+
}
38+
},
39+
load(id) {
40+
if (id === '\0entry.js') {
41+
return code
42+
}
43+
},
44+
},
45+
{
46+
name: 'native:define',
47+
options: (definePlugin(config).options! as any).bind({
48+
environment,
49+
}),
50+
},
51+
],
52+
experimental: {
53+
attachDebugInfo: 'none',
54+
},
55+
})
56+
return (await bundler.generate()).output[0].code
57+
}
2658
}
2759
}
2860

29-
describe('definePlugin', () => {
61+
describe.skipIf(!process.env._VITE_TEST_JS_PLUGIN)('definePlugin', () => {
3062
test('replaces custom define', async () => {
3163
const transform = await createDefinePluginTransform({
3264
__APP_VERSION__: JSON.stringify('1.0'),
@@ -146,3 +178,120 @@ describe('definePlugin', () => {
146178
)
147179
})
148180
})
181+
182+
describe.skipIf(process.env._VITE_TEST_JS_PLUGIN)('native definePlugin', () => {
183+
test('replaces custom define', async () => {
184+
const transform = await createDefinePluginTransform({
185+
__APP_VERSION__: JSON.stringify('1.0'),
186+
})
187+
expect(await transform('export const version = __APP_VERSION__;')).toBe(
188+
'const version = "1.0";\n\nexport { version };',
189+
)
190+
expect(await transform('export const version = __APP_VERSION__ ;')).toBe(
191+
'const version = "1.0";\n\nexport { version };',
192+
)
193+
})
194+
195+
test('should not replace if not defined', async () => {
196+
const transform = await createDefinePluginTransform({
197+
__APP_VERSION__: JSON.stringify('1.0'),
198+
})
199+
expect(await transform('export const version = "1.0";')).toBe(
200+
'const version = "1.0";\n\nexport { version };',
201+
)
202+
expect(
203+
await transform('export const version = import.meta.SOMETHING'),
204+
).toBe('const version = import.meta.SOMETHING;\n\nexport { version };')
205+
})
206+
207+
test('replaces import.meta.env.SSR with false', async () => {
208+
const transform = await createDefinePluginTransform()
209+
expect(await transform('export const isSSR = import.meta.env.SSR;')).toBe(
210+
'const isSSR = false;\n\nexport { isSSR };',
211+
)
212+
})
213+
214+
test('preserve import.meta.hot with override', async () => {
215+
// assert that the default behavior is to replace import.meta.hot with undefined
216+
const transform = await createDefinePluginTransform()
217+
expect(await transform('export const hot = import.meta.hot;')).toBe(
218+
'const hot = void 0;\n\nexport { hot };',
219+
)
220+
// assert that we can specify a user define to preserve import.meta.hot
221+
const overrideTransform = await createDefinePluginTransform({
222+
'import.meta.hot': 'import.meta.hot',
223+
})
224+
expect(await overrideTransform('export const hot = import.meta.hot;')).toBe(
225+
'const hot = import.meta.hot;\n\nexport { hot };',
226+
)
227+
})
228+
229+
test('replace import.meta.env.UNKNOWN with undefined', async () => {
230+
const transform = await createDefinePluginTransform()
231+
expect(await transform('export const foo = import.meta.env.UNKNOWN;')).toBe(
232+
'const foo = void 0;\n\nexport { foo };',
233+
)
234+
})
235+
236+
test('leave import.meta.env["UNKNOWN"] to runtime', async () => {
237+
const transform = await createDefinePluginTransform()
238+
expect(
239+
await transform('export const foo = import.meta.env["UNKNOWN"];'),
240+
).toMatch(/const foo = .*\["UNKNOWN"\];\n\nexport \{ foo \};/s)
241+
})
242+
243+
test('preserve import.meta.env.UNKNOWN with override', async () => {
244+
const transform = await createDefinePluginTransform({
245+
'import.meta.env.UNKNOWN': 'import.meta.env.UNKNOWN',
246+
})
247+
expect(await transform('export const foo = import.meta.env.UNKNOWN;')).toBe(
248+
'const foo = import.meta.env.UNKNOWN;\n\nexport { foo };',
249+
)
250+
})
251+
252+
test('replace import.meta.env when it is a invalid json', async () => {
253+
const transform = await createDefinePluginTransform({
254+
'import.meta.env.LEGACY': '__VITE_IS_LEGACY__',
255+
})
256+
257+
expect(
258+
await transform(
259+
'export const isLegacy = import.meta.env.LEGACY;\nimport.meta.env.UNDEFINED && console.log(import.meta.env.UNDEFINED);',
260+
),
261+
).toMatchInlineSnapshot(
262+
`"const isLegacy = __VITE_IS_LEGACY__;\n\nexport { isLegacy };"`,
263+
)
264+
})
265+
266+
test('replace bare import.meta.env', async () => {
267+
const transform = await createDefinePluginTransform()
268+
expect(await transform('export const env = import.meta.env;')).toMatch(
269+
/const env = .*;\n\nexport \{ env \};/s,
270+
)
271+
})
272+
273+
test('already has marker', async () => {
274+
const transform = await createDefinePluginTransform()
275+
expect(
276+
await transform(
277+
'console.log(__vite_import_meta_env__);\nexport const env = import.meta.env;',
278+
),
279+
).toMatch(/console.log\(__vite_import_meta_env__\);\nconst env = .*/)
280+
281+
expect(
282+
await transform(
283+
'console.log(__vite_import_meta_env__, __vite_import_meta_env__1);\n export const env = import.meta.env;',
284+
),
285+
).toMatch(
286+
/console.log\(__vite_import_meta_env__, __vite_import_meta_env__1\);\nconst env = .*/,
287+
)
288+
289+
expect(
290+
await transform(
291+
'console.log(__vite_import_meta_env__);\nexport const env = import.meta.env;\nconsole.log(import.meta.env.UNDEFINED);',
292+
),
293+
).toMatch(
294+
/console.log\(__vite_import_meta_env__\);\nconst env = .*;\nconsole.log\(void 0\);/s,
295+
)
296+
})
297+
})

packages/vite/src/node/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ export const configDefaults = Object.freeze({
746746
importGlobRestoreExtension: false,
747747
renderBuiltUrl: undefined,
748748
hmrPartialAccept: false,
749-
enableNativePlugin: process.env._VITE_TEST_JS_PLUGIN ? false : 'resolver',
749+
enableNativePlugin: process.env._VITE_TEST_JS_PLUGIN ? false : true,
750750
},
751751
future: {
752752
removePluginHookHandleHotUpdate: undefined,

playground/glob-import/__tests__/glob-import.spec.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,14 @@ const baseRawResult = {
8888
}
8989

9090
test('should work', async () => {
91-
await expect
92-
.poll(async () => JSON.parse(await page.textContent('.result')))
93-
.toStrictEqual(allResult)
94-
await expect
95-
.poll(async () => JSON.parse(await page.textContent('.result-eager')))
96-
.toStrictEqual(allResult)
91+
if (process.env._VITE_TEST_JS_PLUGIN) {
92+
await expect
93+
.poll(async () => JSON.parse(await page.textContent('.result')))
94+
.toStrictEqual(allResult)
95+
await expect
96+
.poll(async () => JSON.parse(await page.textContent('.result-eager')))
97+
.toStrictEqual(allResult)
98+
}
9799
await expect
98100
.poll(async () =>
99101
JSON.parse(await page.textContent('.result-node_modules')),

playground/js-sourcemap/__tests__/js-sourcemap.spec.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,11 @@ describe.runIf(isBuild)('build tests', () => {
140140

141141
test('sourcemap is correct when preload information is injected', async () => {
142142
const map = findAssetFile(/after-preload-dynamic-[-\w]{8}\.js\.map/)
143-
expect(formatSourcemapForSnapshot(JSON.parse(map))).toMatchInlineSnapshot(`
143+
let mapSnapshot = `
144144
{
145145
"debugId": "00000000-0000-0000-0000-000000000000",
146146
"ignoreList": [],
147-
"mappings": ";grCAAA,OAAO,6BAAuB,wBAE9B,QAAQ,IAAI,wBAAuB",
147+
"mappings": ";grCAAA,OAAO,qDAEP,QAAQ,IAAI,wBAAwB",
148148
"sources": [
149149
"../../after-preload-dynamic.js",
150150
],
@@ -156,7 +156,16 @@ describe.runIf(isBuild)('build tests', () => {
156156
],
157157
"version": 3,
158158
}
159-
`)
159+
`
160+
if (process.env._VITE_TEST_JS_PLUGIN) {
161+
mapSnapshot = mapSnapshot.replace(
162+
';grCAAA,OAAO,qDAEP,QAAQ,IAAI,wBAAwB',
163+
';grCAAA,OAAO,6BAAuB,wBAE9B,QAAQ,IAAI,wBAAuB',
164+
)
165+
}
166+
expect(formatSourcemapForSnapshot(JSON.parse(map))).toMatchInlineSnapshot(
167+
mapSnapshot,
168+
)
160169
// verify sourcemap comment is preserved at the last line
161170
const js = findAssetFile(/after-preload-dynamic-[-\w]{8}\.js$/)
162171
expect(js).toMatch(

playground/minify/vite.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,10 @@ export default defineConfig({
88
build: {
99
minify: 'esbuild',
1010
cssMinify: 'esbuild',
11+
rollupOptions: {
12+
output: {
13+
legalComments: 'none',
14+
},
15+
},
1116
},
1217
})

0 commit comments

Comments
 (0)