Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: url constructor import asset no as url #9399

Merged
merged 1 commit into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/assetImportMetaUrl.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default class a {
name = 'a'
}