Skip to content

Commit

Permalink
fix: url constructor import asset no as url (#9399)
Browse files Browse the repository at this point in the history
  • Loading branch information
poyoho committed Jul 28, 2022
1 parent c78e409 commit 122c6e7
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/assetImportMetaUrl.ts
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions playground/assets/__tests__/assets.spec.ts
Expand Up @@ -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 () => {
Expand Down
7 changes: 7 additions & 0 deletions playground/assets/index.html
Expand Up @@ -199,6 +199,9 @@ <h2>new URL(`./${dynamic}`, import.meta.url)</h2>
<img class="dynamic-import-meta-url-img-2" />
<code class="dynamic-import-meta-url-2"></code>
</p>
<p>
<code class="dynamic-import-meta-url-js"></code>
</p>

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

const name = 'test'
const js = new URL(`./nested/${name}.js`, import.meta.url).href
text('.dynamic-import-meta-url-js', js)

function text(el, text) {
document.querySelector(el).textContent = text
}
Expand Down
3 changes: 3 additions & 0 deletions playground/assets/nested/test.js
@@ -0,0 +1,3 @@
export default class a {
name = 'a'
}

0 comments on commit 122c6e7

Please sign in to comment.