Skip to content

Commit

Permalink
fix: no asset plugin w/ img is imported with query (#8353)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
Barthélémy Ledoux and Princesseuh authored Sep 6, 2023
1 parent 48ff785 commit 1947ef7
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 13 deletions.
13 changes: 13 additions & 0 deletions .changeset/disable-asset-with-query.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
'astro': patch
---

Astro will now skip asset optimization when there is a query in the import. Instead, it will let vite deal with it using plugins.

```vue
<script>
// This will not return an optimized asset
import Component from './Component.vue?component'
</script>
```

12 changes: 4 additions & 8 deletions packages/astro/src/assets/vite-plugin-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ import { hashTransform, propsToFilename } from './utils/transformToPath.js';

const resolvedVirtualModuleId = '\0' + VIRTUAL_MODULE_ID;

const rawRE = /(?:\?|&)raw(?:&|$)/;
const urlRE = /(\?|&)url(?:&|$)/;

export default function assets({
settings,
mode,
Expand Down Expand Up @@ -119,13 +116,12 @@ export default function assets({
resolvedConfig = viteConfig;
},
async load(id) {
// If our import has the `?raw` or `?url` Vite query params, we'll let Vite handle it
if (rawRE.test(id) || urlRE.test(id)) {
// If our import has any query params, we'll let Vite handle it
// See https://github.com/withastro/astro/issues/8333
if (id !== removeQueryString(id)) {
return;
}

const cleanedUrl = removeQueryString(id);
if (/\.(jpeg|jpg|png|tiff|webp|gif|svg)$/.test(cleanedUrl)) {
if (/\.(jpeg|jpg|png|tiff|webp|gif|svg)$/.test(id)) {
const meta = await emitESMImage(id, this.meta.watchMode, this.emitFile);
return `export default ${JSON.stringify(meta)}`;
}
Expand Down
8 changes: 8 additions & 0 deletions packages/integrations/vue/test/app-entrypoint.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,12 @@ describe('App Entrypoint', () => {
const js = await fixture.readFile(client);
expect(js).to.match(/\w+\.component\(\"Bar\"/gm);
});

it('loads svg components without transforming them to assets', async () => {
const data = await fixture.readFile('/index.html');
const { document } = parseHTML(data);
const client = document.querySelector('astro-island svg');

expect(client).not.to.be.undefined;
});
});
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { defineConfig } from 'astro/config';
import vue from '@astrojs/vue';
import ViteSvgLoader from 'vite-svg-loader'

export default defineConfig({
integrations: [vue({
appEntrypoint: '/src/pages/_app'
})]
})],
vite: {
plugins: [
ViteSvgLoader(),
],
},
})
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"private": true,
"dependencies": {
"astro": "workspace:*",
"@astrojs/vue": "workspace:*"
"@astrojs/vue": "workspace:*",
"vite-svg-loader": "4.0.0"
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<script setup>
import Circle from './Circle.svg?component'
</script>

<template>
<div id="foo">
<Bar />
<Circle/>
</div>
</template>
54 changes: 52 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1947ef7

Please sign in to comment.