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: no asset plugin w/ img is imported with query #8353

Merged
merged 11 commits into from
Sep 6, 2023
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
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.