Skip to content

Commit 1947ef7

Browse files
Barthélémy LedouxPrincesseuh
andauthored
fix: no asset plugin w/ img is imported with query (#8353)
* fix: no asset plugin w/ img is imported with query * add changeset * add test for the new feature * remove exp * use removeQueryString instead of `includes('?')` it's more explicit --------- Co-authored-by: Erika <3019731+Princesseuh@users.noreply.github.com>
1 parent 48ff785 commit 1947ef7

8 files changed

Lines changed: 95 additions & 13 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Astro will now skip asset optimization when there is a query in the import. Instead, it will let vite deal with it using plugins.
6+
7+
```vue
8+
<script>
9+
// This will not return an optimized asset
10+
import Component from './Component.vue?component'
11+
</script>
12+
```
13+

packages/astro/src/assets/vite-plugin-assets.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ import { hashTransform, propsToFilename } from './utils/transformToPath.js';
1414

1515
const resolvedVirtualModuleId = '\0' + VIRTUAL_MODULE_ID;
1616

17-
const rawRE = /(?:\?|&)raw(?:&|$)/;
18-
const urlRE = /(\?|&)url(?:&|$)/;
19-
2017
export default function assets({
2118
settings,
2219
mode,
@@ -119,13 +116,12 @@ export default function assets({
119116
resolvedConfig = viteConfig;
120117
},
121118
async load(id) {
122-
// If our import has the `?raw` or `?url` Vite query params, we'll let Vite handle it
123-
if (rawRE.test(id) || urlRE.test(id)) {
119+
// If our import has any query params, we'll let Vite handle it
120+
// See https://github.com/withastro/astro/issues/8333
121+
if (id !== removeQueryString(id)) {
124122
return;
125123
}
126-
127-
const cleanedUrl = removeQueryString(id);
128-
if (/\.(jpeg|jpg|png|tiff|webp|gif|svg)$/.test(cleanedUrl)) {
124+
if (/\.(jpeg|jpg|png|tiff|webp|gif|svg)$/.test(id)) {
129125
const meta = await emitESMImage(id, this.meta.watchMode, this.emitFile);
130126
return `export default ${JSON.stringify(meta)}`;
131127
}

packages/integrations/vue/test/app-entrypoint.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,12 @@ describe('App Entrypoint', () => {
3030
const js = await fixture.readFile(client);
3131
expect(js).to.match(/\w+\.component\(\"Bar\"/gm);
3232
});
33+
34+
it('loads svg components without transforming them to assets', async () => {
35+
const data = await fixture.readFile('/index.html');
36+
const { document } = parseHTML(data);
37+
const client = document.querySelector('astro-island svg');
38+
39+
expect(client).not.to.be.undefined;
40+
});
3341
});
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
import { defineConfig } from 'astro/config';
22
import vue from '@astrojs/vue';
3+
import ViteSvgLoader from 'vite-svg-loader'
34

45
export default defineConfig({
56
integrations: [vue({
67
appEntrypoint: '/src/pages/_app'
7-
})]
8+
})],
9+
vite: {
10+
plugins: [
11+
ViteSvgLoader(),
12+
],
13+
},
814
})

packages/integrations/vue/test/fixtures/app-entrypoint/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"private": true,
55
"dependencies": {
66
"astro": "workspace:*",
7-
"@astrojs/vue": "workspace:*"
7+
"@astrojs/vue": "workspace:*",
8+
"vite-svg-loader": "4.0.0"
89
}
9-
}
10+
}
Lines changed: 3 additions & 0 deletions
Loading
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
<script setup>
2+
import Circle from './Circle.svg?component'
3+
</script>
4+
15
<template>
26
<div id="foo">
37
<Bar />
8+
<Circle/>
49
</div>
510
</template>

pnpm-lock.yaml

Lines changed: 52 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)