diff --git a/packages/vite/src/node/plugins/importMetaGlob.ts b/packages/vite/src/node/plugins/importMetaGlob.ts index 7f3695178fdce9..6943bf24c56702 100644 --- a/packages/vite/src/node/plugins/importMetaGlob.ts +++ b/packages/vite/src/node/plugins/importMetaGlob.ts @@ -5,6 +5,7 @@ import type { ArrayExpression, CallExpression, Literal, + MemberExpression, Node, SequenceExpression } from 'estree' @@ -118,7 +119,7 @@ export async function parseImportGlob( return e } - let ast: CallExpression | SequenceExpression + let ast: CallExpression | SequenceExpression | MemberExpression let lastTokenPos: number | undefined try { @@ -157,6 +158,10 @@ export async function parseImportGlob( if (ast.type === 'SequenceExpression') ast = ast.expressions[0] as CallExpression + // immediate property access, call expression is nested + // import.meta.glob(...)['prop'] + if (ast.type === 'MemberExpression') ast = ast.object as CallExpression + if (ast.type !== 'CallExpression') throw err(`Expect CallExpression, got ${ast.type}`) diff --git a/playground/glob-import/__tests__/glob-import.spec.ts b/playground/glob-import/__tests__/glob-import.spec.ts index 7b9b0aa622381e..27c8dd716d0358 100644 --- a/playground/glob-import/__tests__/glob-import.spec.ts +++ b/playground/glob-import/__tests__/glob-import.spec.ts @@ -92,6 +92,12 @@ test('import glob raw', async () => { ) }) +test('import property access', async () => { + expect(await page.textContent('.property-access')).toBe( + JSON.stringify(rawResult['/dir/baz.json'], null, 2) + ) +}) + test('import relative glob raw', async () => { expect(await page.textContent('.relative-glob-raw')).toBe( JSON.stringify(relativeRawResult, null, 2) diff --git a/playground/glob-import/index.html b/playground/glob-import/index.html index ca00c065e7402d..a0ac2d866699ae 100644 --- a/playground/glob-import/index.html +++ b/playground/glob-import/index.html @@ -1,8 +1,17 @@ +

Glob import

+

Normal


+

Eager


+

node_modules


+

Raw


+

Property access

+

+

Relative raw


+

Side effect


 
 
@@ -63,6 +72,18 @@
   )
 
 
+
+