From ccac7d4dc7e19f5512112e42fbf70a8cacfbe013 Mon Sep 17 00:00:00 2001 From: Wick Date: Mon, 10 Mar 2025 18:21:45 +0800 Subject: [PATCH] fix: make glob pattern supports nested paths --- .../plugins/assetImportMetaUrl.spec.ts | 16 ++++++++--- .../src/node/plugins/assetImportMetaUrl.ts | 28 ++++++++++++++----- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/packages/vite/src/node/__tests__/plugins/assetImportMetaUrl.spec.ts b/packages/vite/src/node/__tests__/plugins/assetImportMetaUrl.spec.ts index 37dc870372da0f..c66bf95d00b01b 100644 --- a/packages/vite/src/node/__tests__/plugins/assetImportMetaUrl.spec.ts +++ b/packages/vite/src/node/__tests__/plugins/assetImportMetaUrl.spec.ts @@ -23,11 +23,19 @@ async function createAssetImportMetaurlPluginTransform() { describe('assetImportMetaUrlPlugin', async () => { const transform = await createAssetImportMetaurlPluginTransform() + test('no file extension specified', async () => { + expect( + await transform('new URL(`./foo/${filePath}`, import.meta.url)'), + ).toMatchInlineSnapshot( + `"new URL((import.meta.glob("./foo/**", {"eager":true,"import":"default","query":"?url"}))[\`./foo/\${filePath}\`], import.meta.url)"`, + ) + }) + test('variable between /', async () => { expect( await transform('new URL(`./foo/${dir}/index.js`, import.meta.url)'), ).toMatchInlineSnapshot( - `"new URL((import.meta.glob("./foo/*/index.js", {"eager":true,"import":"default","query":"?url"}))[\`./foo/\${dir}/index.js\`], import.meta.url)"`, + `"new URL((import.meta.glob("./foo/**/index.js", {"eager":true,"import":"default","query":"?url"}))[\`./foo/\${dir}/index.js\`], import.meta.url)"`, ) }) @@ -35,7 +43,7 @@ describe('assetImportMetaUrlPlugin', async () => { expect( await transform('new URL(`./foo/${dir}.js`, import.meta.url)'), ).toMatchInlineSnapshot( - `"new URL((import.meta.glob("./foo/*.js", {"eager":true,"import":"default","query":"?url"}))[\`./foo/\${dir}.js\`], import.meta.url)"`, + `"new URL((import.meta.glob("./foo/**/*.js", {"eager":true,"import":"default","query":"?url"}))[\`./foo/\${dir}.js\`], import.meta.url)"`, ) }) @@ -43,7 +51,7 @@ describe('assetImportMetaUrlPlugin', async () => { expect( await transform('new URL(`./foo/${dir}${file}.js`, import.meta.url)'), ).toMatchInlineSnapshot( - `"new URL((import.meta.glob("./foo/*.js", {"eager":true,"import":"default","query":"?url"}))[\`./foo/\${dir}\${file}.js\`], import.meta.url)"`, + `"new URL((import.meta.glob("./foo/**/*.js", {"eager":true,"import":"default","query":"?url"}))[\`./foo/\${dir}\${file}.js\`], import.meta.url)"`, ) }) @@ -53,7 +61,7 @@ describe('assetImportMetaUrlPlugin', async () => { 'new URL(`./foo/${dir}${dir2}/index.js`, import.meta.url)', ), ).toMatchInlineSnapshot( - `"new URL((import.meta.glob("./foo/*/index.js", {"eager":true,"import":"default","query":"?url"}))[\`./foo/\${dir}\${dir2}/index.js\`], import.meta.url)"`, + `"new URL((import.meta.glob("./foo/**/index.js", {"eager":true,"import":"default","query":"?url"}))[\`./foo/\${dir}\${dir2}/index.js\`], import.meta.url)"`, ) }) diff --git a/packages/vite/src/node/plugins/assetImportMetaUrl.ts b/packages/vite/src/node/plugins/assetImportMetaUrl.ts index a772c8531b4f47..6f9ac4f202b0ac 100644 --- a/packages/vite/src/node/plugins/assetImportMetaUrl.ts +++ b/packages/vite/src/node/plugins/assetImportMetaUrl.ts @@ -169,21 +169,35 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin { } } +const extRE = /^\.[a-zA-Z0-9]+$/ function buildGlobPattern(ast: any) { let pattern = '' - let lastIsGlob = false + let hasGlob = false for (let i = 0; i < ast.quasis.length; i++) { const str = ast.quasis[i].value.raw - if (str) { - pattern += str - lastIsGlob = false + if (!str) { + continue } - if (ast.expressions[i] && !lastIsGlob) { - pattern += '*' - lastIsGlob = true + const isExt = extRE.test(str) + + if (isExt) { + const endsWithStar = pattern.endsWith('*') + if (endsWithStar && hasGlob) { + pattern += '/*' + } else if (!endsWithStar) { + pattern += '*' + } + } + + pattern += str + + if (!hasGlob && !isExt) { + pattern += '**' + hasGlob = true } } + return pattern }