diff --git a/packages/vite/src/node/plugins/assetImportMetaUrl.ts b/packages/vite/src/node/plugins/assetImportMetaUrl.ts index 6d7a657706cf9c..4b52a225a68458 100644 --- a/packages/vite/src/node/plugins/assetImportMetaUrl.ts +++ b/packages/vite/src/node/plugins/assetImportMetaUrl.ts @@ -55,7 +55,7 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin { s.overwrite( index, index + exp.length, - `new URL((import.meta.glob(${pattern}, { eager: true, import: 'default' }))[${rawUrl}], self.location)`, + `new URL((import.meta.glob(${pattern}, { eager: true, import: 'default', as: 'url' }))[${rawUrl}], self.location)`, { contentOnly: true } ) continue diff --git a/playground/assets/__tests__/assets.spec.ts b/playground/assets/__tests__/assets.spec.ts index 5b265578e98751..600ea3b0bba971 100644 --- a/playground/assets/__tests__/assets.spec.ts +++ b/playground/assets/__tests__/assets.spec.ts @@ -288,6 +288,9 @@ test('new URL(`${dynamic}`, import.meta.url)', async () => { expect(await page.textContent('.dynamic-import-meta-url-2')).toMatch( assetMatch ) + expect(await page.textContent('.dynamic-import-meta-url-js')).toMatch( + isBuild ? 'data:application/javascript;base64' : '/foo/nested/test.js' + ) }) test('new URL(`non-existent`, import.meta.url)', async () => { diff --git a/playground/assets/index.html b/playground/assets/index.html index a741379e82013e..5a49d42791d058 100644 --- a/playground/assets/index.html +++ b/playground/assets/index.html @@ -199,6 +199,9 @@
+
+
+
@@ -390,6 +393,10 @@