Skip to content

Commit 42f2ab3

Browse files
authored
feat: introduce v2 native plugins and enable it by default (#21268)
1 parent 181d870 commit 42f2ab3

File tree

7 files changed

+55
-5
lines changed

7 files changed

+55
-5
lines changed

packages/vite/src/node/config.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -558,12 +558,13 @@ export interface ExperimentalOptions {
558558
*
559559
* - 'resolver' (deprecated, will be removed in v8 stable): Enable only the native resolver plugin.
560560
* - 'v1' (will be deprecated, will be removed in v8 stable): Enable the first stable set of native plugins (including resolver).
561-
* - true: Enable all native plugins (currently an alias of 'v1', it will map to a newer one in the future versions).
561+
* - 'v2' (will be deprecated, will be removed in v8 stable): Enable the improved dynamicImportVarsPlugin and importGlobPlugin.
562+
* - true: Enable all native plugins (currently an alias of 'v2', it will map to a newer one in the future versions).
562563
*
563564
* @experimental
564-
* @default 'v1'
565+
* @default 'v2'
565566
*/
566-
enableNativePlugin?: boolean | 'resolver' | 'v1'
567+
enableNativePlugin?: boolean | 'resolver' | 'v1' | 'v2'
567568
/**
568569
* Enable full bundle mode.
569570
*
@@ -787,7 +788,7 @@ const configDefaults = Object.freeze({
787788
importGlobRestoreExtension: false,
788789
renderBuiltUrl: undefined,
789790
hmrPartialAccept: false,
790-
enableNativePlugin: process.env._VITE_TEST_JS_PLUGIN ? false : 'v1',
791+
enableNativePlugin: process.env._VITE_TEST_JS_PLUGIN ? false : 'v2',
791792
bundledDev: false,
792793
},
793794
future: {
@@ -2092,8 +2093,10 @@ function resolveNativePluginEnabledLevel(
20922093
case 'resolver':
20932094
return 0
20942095
case 'v1':
2095-
case true:
20962096
return 1
2097+
case 'v2':
2098+
case true:
2099+
return 2
20972100
case false:
20982101
return -1
20992102
default:

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,12 @@ export function dynamicImportVarsPlugin(config: ResolvedConfig): Plugin {
184184
resolver(id, importer) {
185185
return resolve(environment, id, importer)
186186
},
187+
isV2:
188+
config.nativePluginEnabledLevel >= 2
189+
? {
190+
sourcemap: !!environment.config.build.sourcemap,
191+
}
192+
: undefined,
187193
})
188194
})
189195
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ export function importGlobPlugin(config: ResolvedConfig): Plugin {
4646
return nativeImportGlobPlugin({
4747
root: config.root,
4848
restoreQueryExtension: config.experimental.importGlobRestoreExtension,
49+
isV2:
50+
config.nativePluginEnabledLevel >= 2
51+
? {
52+
sourcemap: !!config.build.sourcemap,
53+
}
54+
: undefined,
4955
})
5056
}
5157

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,3 +303,11 @@ test('import base glob raw', async () => {
303303
.poll(async () => await page.textContent('.result-base'))
304304
.toBe(JSON.stringify(baseRawResult, null, 2))
305305
})
306+
307+
test('import.meta.glob and dynamic import vars transformations should be visible to post transform plugins', async () => {
308+
await expect
309+
.poll(async () => await page.textContent('.transform-visibility'))
310+
.toBe(
311+
JSON.stringify({ globTransformed: true, dynamicImportTransformed: true }),
312+
)
313+
})

playground/glob-import/index.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,12 @@ <h2>Array Pattern with Exclusions</h2>
205205
2,
206206
)
207207
</script>
208+
209+
<h2>Transform visibility</h2>
210+
<pre class="transform-visibility"></pre>
211+
212+
<script type="module">
213+
import result from './transform-visibility.js'
214+
document.querySelector('.transform-visibility').textContent =
215+
JSON.stringify(result)
216+
</script>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const name = 'foo'
2+
export const globResult = import.meta.glob('./dir/*.js')
3+
export const dynamicResult = import(`./dir/${name}.js`)

playground/glob-import/vite.config.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,22 @@ const escapeAliases = fs
1414
return aliases
1515
}, {})
1616

17+
const transformVisibilityPlugin = {
18+
name: 'test:transform-visibility',
19+
enforce: 'post',
20+
transform(code: string, id: string) {
21+
if (id.endsWith('transform-visibility.js')) {
22+
const globTransformed = !code.includes('import.meta.glob')
23+
const dynamicImportTransformed = code.includes(
24+
'__variableDynamicImportRuntimeHelper',
25+
)
26+
return `export default ${JSON.stringify({ globTransformed, dynamicImportTransformed })}`
27+
}
28+
},
29+
}
30+
1731
export default defineConfig({
32+
plugins: [transformVisibilityPlugin],
1833
resolve: {
1934
alias: {
2035
...escapeAliases,

0 commit comments

Comments
 (0)