Skip to content

Commit 122c6e7

Browse files
authored
fix: url constructor import asset no as url (#9399)
1 parent c78e409 commit 122c6e7

File tree

4 files changed

+14
-1
lines changed

4 files changed

+14
-1
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export function assetImportMetaUrlPlugin(config: ResolvedConfig): Plugin {
5555
s.overwrite(
5656
index,
5757
index + exp.length,
58-
`new URL((import.meta.glob(${pattern}, { eager: true, import: 'default' }))[${rawUrl}], self.location)`,
58+
`new URL((import.meta.glob(${pattern}, { eager: true, import: 'default', as: 'url' }))[${rawUrl}], self.location)`,
5959
{ contentOnly: true }
6060
)
6161
continue

playground/assets/__tests__/assets.spec.ts

+3
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,9 @@ test('new URL(`${dynamic}`, import.meta.url)', async () => {
288288
expect(await page.textContent('.dynamic-import-meta-url-2')).toMatch(
289289
assetMatch
290290
)
291+
expect(await page.textContent('.dynamic-import-meta-url-js')).toMatch(
292+
isBuild ? 'data:application/javascript;base64' : '/foo/nested/test.js'
293+
)
291294
})
292295

293296
test('new URL(`non-existent`, import.meta.url)', async () => {

playground/assets/index.html

+7
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ <h2>new URL(`./${dynamic}`, import.meta.url)</h2>
199199
<img class="dynamic-import-meta-url-img-2" />
200200
<code class="dynamic-import-meta-url-2"></code>
201201
</p>
202+
<p>
203+
<code class="dynamic-import-meta-url-js"></code>
204+
</p>
202205

203206
<h2>new URL(`./${dynamic}`, import.meta.url,) (with comma)</h2>
204207
<p>
@@ -390,6 +393,10 @@ <h3>style in svg</h3>
390393
testDynamicImportMetaUrlWithComma('icon', 1)
391394
testDynamicImportMetaUrlWithComma('asset', 2)
392395

396+
const name = 'test'
397+
const js = new URL(`./nested/${name}.js`, import.meta.url).href
398+
text('.dynamic-import-meta-url-js', js)
399+
393400
function text(el, text) {
394401
document.querySelector(el).textContent = text
395402
}

playground/assets/nested/test.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default class a {
2+
name = 'a'
3+
}

0 commit comments

Comments
 (0)