From 4b9434bb9f7571d8c8dbc71fb23f746a39799870 Mon Sep 17 00:00:00 2001 From: shulaoda <165626830+shulaoda@users.noreply.github.com> Date: Fri, 12 Dec 2025 10:32:17 +0800 Subject: [PATCH 1/2] feat: introduce v2 stable native plugins --- packages/vite/src/node/config.ts | 13 ++++++++----- packages/vite/src/node/plugins/dynamicImportVars.ts | 6 ++++++ packages/vite/src/node/plugins/importMetaGlob.ts | 6 ++++++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index 30af7eb433ea95..67c906f9c32343 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -558,12 +558,13 @@ export interface ExperimentalOptions { * * - 'resolver' (deprecated, will be removed in v8 stable): Enable only the native resolver plugin. * - 'v1' (will be deprecated, will be removed in v8 stable): Enable the first stable set of native plugins (including resolver). - * - true: Enable all native plugins (currently an alias of 'v1', it will map to a newer one in the future versions). + * - 'v2' (will be deprecated, will be removed in v8 stable): Enable the improved dynamicImportVarsPlugin and importGlobPlugin. + * - true: Enable all native plugins (currently an alias of 'v2', it will map to a newer one in the future versions). * * @experimental - * @default 'v1' + * @default 'v2' */ - enableNativePlugin?: boolean | 'resolver' | 'v1' + enableNativePlugin?: boolean | 'resolver' | 'v1' | 'v2' /** * Enable full bundle mode. * @@ -787,7 +788,7 @@ const configDefaults = Object.freeze({ importGlobRestoreExtension: false, renderBuiltUrl: undefined, hmrPartialAccept: false, - enableNativePlugin: process.env._VITE_TEST_JS_PLUGIN ? false : 'v1', + enableNativePlugin: process.env._VITE_TEST_JS_PLUGIN ? false : 'v2', bundledDev: false, }, future: { @@ -2092,8 +2093,10 @@ function resolveNativePluginEnabledLevel( case 'resolver': return 0 case 'v1': - case true: return 1 + case 'v2': + case true: + return 2 case false: return -1 default: diff --git a/packages/vite/src/node/plugins/dynamicImportVars.ts b/packages/vite/src/node/plugins/dynamicImportVars.ts index 9200f393ec7907..85c807d059891d 100644 --- a/packages/vite/src/node/plugins/dynamicImportVars.ts +++ b/packages/vite/src/node/plugins/dynamicImportVars.ts @@ -184,6 +184,12 @@ export function dynamicImportVarsPlugin(config: ResolvedConfig): Plugin { resolver(id, importer) { return resolve(environment, id, importer) }, + isV2: + config.nativePluginEnabledLevel >= 2 + ? { + sourcemap: !!environment.config.build.sourcemap, + } + : undefined, }) }) } diff --git a/packages/vite/src/node/plugins/importMetaGlob.ts b/packages/vite/src/node/plugins/importMetaGlob.ts index e661f800439412..c110ae732057d2 100644 --- a/packages/vite/src/node/plugins/importMetaGlob.ts +++ b/packages/vite/src/node/plugins/importMetaGlob.ts @@ -46,6 +46,12 @@ export function importGlobPlugin(config: ResolvedConfig): Plugin { return nativeImportGlobPlugin({ root: config.root, restoreQueryExtension: config.experimental.importGlobRestoreExtension, + isV2: + config.nativePluginEnabledLevel >= 2 + ? { + sourcemap: !!config.build.sourcemap, + } + : undefined, }) } From 73ad5545631b0154a67fe245b9110ec398f82ab2 Mon Sep 17 00:00:00 2001 From: shulaoda <165626830+shulaoda@users.noreply.github.com> Date: Fri, 12 Dec 2025 16:13:36 +0800 Subject: [PATCH 2/2] test: add related test case --- .../glob-import/__tests__/glob-import.spec.ts | 8 ++++++++ playground/glob-import/index.html | 9 +++++++++ playground/glob-import/transform-visibility.js | 3 +++ playground/glob-import/vite.config.ts | 15 +++++++++++++++ 4 files changed, 35 insertions(+) create mode 100644 playground/glob-import/transform-visibility.js diff --git a/playground/glob-import/__tests__/glob-import.spec.ts b/playground/glob-import/__tests__/glob-import.spec.ts index 452b518ef46957..90b973e501270a 100644 --- a/playground/glob-import/__tests__/glob-import.spec.ts +++ b/playground/glob-import/__tests__/glob-import.spec.ts @@ -303,3 +303,11 @@ test('import base glob raw', async () => { .poll(async () => await page.textContent('.result-base')) .toBe(JSON.stringify(baseRawResult, null, 2)) }) + +test('import.meta.glob and dynamic import vars transformations should be visible to post transform plugins', async () => { + await expect + .poll(async () => await page.textContent('.transform-visibility')) + .toBe( + JSON.stringify({ globTransformed: true, dynamicImportTransformed: true }), + ) +}) diff --git a/playground/glob-import/index.html b/playground/glob-import/index.html index 072023442c20e6..20d4467361e2d7 100644 --- a/playground/glob-import/index.html +++ b/playground/glob-import/index.html @@ -205,3 +205,12 @@

Array Pattern with Exclusions

2, ) + +

Transform visibility

+

+
+
diff --git a/playground/glob-import/transform-visibility.js b/playground/glob-import/transform-visibility.js
new file mode 100644
index 00000000000000..fd409c9b419646
--- /dev/null
+++ b/playground/glob-import/transform-visibility.js
@@ -0,0 +1,3 @@
+const name = 'foo'
+export const globResult = import.meta.glob('./dir/*.js')
+export const dynamicResult = import(`./dir/${name}.js`)
diff --git a/playground/glob-import/vite.config.ts b/playground/glob-import/vite.config.ts
index 99317f08a0031e..40779dca525d93 100644
--- a/playground/glob-import/vite.config.ts
+++ b/playground/glob-import/vite.config.ts
@@ -14,7 +14,22 @@ const escapeAliases = fs
     return aliases
   }, {})
 
+const transformVisibilityPlugin = {
+  name: 'test:transform-visibility',
+  enforce: 'post',
+  transform(code: string, id: string) {
+    if (id.endsWith('transform-visibility.js')) {
+      const globTransformed = !code.includes('import.meta.glob')
+      const dynamicImportTransformed = code.includes(
+        '__variableDynamicImportRuntimeHelper',
+      )
+      return `export default ${JSON.stringify({ globTransformed, dynamicImportTransformed })}`
+    }
+  },
+}
+
 export default defineConfig({
+  plugins: [transformVisibilityPlugin],
   resolve: {
     alias: {
       ...escapeAliases,