Skip to content

Commit c35ec68

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

File tree

17 files changed

+229
-59
lines changed

17 files changed

+229
-59
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/build.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -505,14 +505,14 @@ export async function resolveBuildPlugins(config: ResolvedConfig): Promise<{
505505
],
506506
post: [
507507
...buildImportAnalysisPlugin(config),
508-
...(config.experimental.enableNativePlugin !== true
509-
? [
508+
...(config.nativePluginEnabledLevel >= 1
509+
? []
510+
: [
510511
buildOxcPlugin(),
511512
...(config.build.minify === 'esbuild'
512513
? [buildEsbuildPlugin()]
513514
: []),
514-
]
515-
: []),
515+
]),
516516
terserPlugin(config),
517517
...(!config.isWorker
518518
? [

packages/vite/src/node/config.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -542,10 +542,14 @@ export interface ExperimentalOptions {
542542
/**
543543
* Enable builtin plugin that written by rust, which is faster than js plugin.
544544
*
545+
* - 'resolver': Enable only the native resolver plugin.
546+
* - 'v1': Enable the first stable set of native plugins (including resolver).
547+
* - true: Enable all native plugins (currently an alias of 'v1', it will map to a newer one in the future versions).
548+
*
545549
* @experimental
546-
* @default 'resolver'
550+
* @default 'v1'
547551
*/
548-
enableNativePlugin?: boolean | 'resolver'
552+
enableNativePlugin?: boolean | 'resolver' | 'v1'
549553
}
550554

551555
export interface LegacyOptions {
@@ -681,6 +685,8 @@ export interface ResolvedConfig
681685
safeModulePaths: Set<string>
682686
/** @internal */
683687
[SYMBOL_RESOLVED_CONFIG]: true
688+
/** @internal */
689+
nativePluginEnabledLevel: number
684690
} & PluginHookUtils
685691
> {}
686692

@@ -746,7 +752,7 @@ export const configDefaults = Object.freeze({
746752
importGlobRestoreExtension: false,
747753
renderBuiltUrl: undefined,
748754
hmrPartialAccept: false,
749-
enableNativePlugin: process.env._VITE_TEST_JS_PLUGIN ? false : 'resolver',
755+
enableNativePlugin: process.env._VITE_TEST_JS_PLUGIN ? false : 'v1',
750756
},
751757
future: {
752758
removePluginHookHandleHotUpdate: undefined,
@@ -1881,10 +1887,25 @@ export async function resolveConfig(
18811887
),
18821888
safeModulePaths: new Set<string>(),
18831889
[SYMBOL_RESOLVED_CONFIG]: true,
1890+
nativePluginEnabledLevel: -1,
1891+
}
1892+
1893+
function resolveNativePluginLevel() {
1894+
switch (resolved.experimental.enableNativePlugin) {
1895+
case 'resolver':
1896+
return 0
1897+
case 'v1':
1898+
case true:
1899+
return 1
1900+
default:
1901+
return -1
1902+
}
18841903
}
1904+
18851905
resolved = {
18861906
...config,
18871907
...resolved,
1908+
nativePluginEnabledLevel: resolveNativePluginLevel(),
18881909
}
18891910

18901911
// Backward compatibility hook, modify the resolved config before it is used

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export function definePlugin(config: ResolvedConfig): Plugin {
115115
return pattern
116116
}
117117

118-
if (config.experimental.enableNativePlugin === true && isBuild) {
118+
if (isBuild && config.nativePluginEnabledLevel >= 1) {
119119
return {
120120
name: 'vite:define',
121121
options(option) {

packages/vite/src/node/plugins/dynamicImportVars.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,7 @@ export function dynamicImportVarsPlugin(config: ResolvedConfig): Plugin {
173173
extensions: [],
174174
})
175175

176-
if (
177-
config.experimental.enableNativePlugin === true &&
178-
config.command === 'build'
179-
) {
176+
if (config.command === 'build' && config.nativePluginEnabledLevel >= 1) {
180177
return perEnvironmentPlugin('native:dynamic-import-vars', (environment) => {
181178
const { include, exclude } =
182179
environment.config.build.dynamicImportVarsOptions

packages/vite/src/node/plugins/importAnalysisBuild.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin[] {
749749
},
750750
}
751751

752-
if (config.experimental.enableNativePlugin === true) {
752+
if (config.nativePluginEnabledLevel >= 1) {
753753
delete plugin.transform
754754
delete plugin.resolveId
755755
delete plugin.load

packages/vite/src/node/plugins/importMetaGlob.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@ interface ParsedGeneralImportGlobOptions extends GeneralImportGlobOptions {
4242
}
4343

4444
export function importGlobPlugin(config: ResolvedConfig): Plugin {
45-
if (
46-
config.experimental.enableNativePlugin === true &&
47-
config.command === 'build'
48-
) {
45+
if (config.command === 'build' && config.nativePluginEnabledLevel >= 1) {
4946
return nativeImportGlobPlugin({
5047
root: config.root,
5148
restoreQueryExtension: config.experimental.importGlobRestoreExtension,

packages/vite/src/node/plugins/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,15 @@ export async function resolvePlugins(
4646
? await (await import('../build')).resolveBuildPlugins(config)
4747
: { pre: [], post: [] }
4848
const { modulePreload } = config.build
49-
const enableNativePlugin = config.experimental.enableNativePlugin
49+
const enableNativePlugin = config.nativePluginEnabledLevel >= 0
50+
const enableNativePluginV1 = config.nativePluginEnabledLevel >= 1
5051

5152
return [
5253
!isBuild ? optimizedDepsPlugin() : null,
5354
!isWorker ? watchPackageDataPlugin(config.packageCache) : null,
5455
!isBuild ? preAliasPlugin(config) : null,
55-
enableNativePlugin === true &&
5656
isBuild &&
57+
enableNativePluginV1 &&
5758
!config.resolve.alias.some((v) => v.customResolver)
5859
? nativeAliasPlugin({
5960
entries: config.resolve.alias.map((item) => {
@@ -104,7 +105,7 @@ export async function resolvePlugins(
104105
cssPlugin(config),
105106
esbuildBannerFooterCompatPlugin(config),
106107
config.oxc !== false ? oxcPlugin(config) : null,
107-
jsonPlugin(config.json, isBuild, enableNativePlugin === true),
108+
jsonPlugin(config.json, isBuild, enableNativePluginV1),
108109
wasmHelperPlugin(config),
109110
webWorkerPlugin(config),
110111
assetPlugin(config),

packages/vite/src/node/plugins/manifest.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@ export function manifestPlugin(config: ResolvedConfig): Plugin {
3535
},
3636
}
3737
})
38-
if (
39-
config.build.manifest &&
40-
config.experimental.enableNativePlugin === true
41-
) {
38+
if (config.build.manifest && config.nativePluginEnabledLevel >= 1) {
4239
return perEnvironmentPlugin('native:manifest', (environment) => {
4340
if (!environment.config.build.manifest) return false
4441

packages/vite/src/node/plugins/modulePreloadPolyfill.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ export const modulePreloadPolyfillId = 'vite/modulepreload-polyfill'
88
const resolvedModulePreloadPolyfillId = '\0' + modulePreloadPolyfillId + '.js'
99

1010
export function modulePreloadPolyfillPlugin(config: ResolvedConfig): Plugin {
11-
if (
12-
config.experimental.enableNativePlugin === true &&
13-
config.command === 'build'
14-
) {
11+
if (config.command === 'build' && config.nativePluginEnabledLevel >= 1) {
1512
return perEnvironmentPlugin(
1613
'native:modulepreload-polyfill',
1714
(environment) => {

0 commit comments

Comments
 (0)