Skip to content

Commit 4104286

Browse files
authored
Fix imports using ?raw and ?url not working when experimental.assets is enabled (#7108)
1 parent 3420261 commit 4104286

4 files changed

Lines changed: 50 additions & 0 deletions

File tree

.changeset/thirty-squids-relax.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fix imports using ?raw and ?url not working when `experimental.assets` is enabled

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

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

2525
const resolvedVirtualModuleId = '\0' + VIRTUAL_MODULE_ID;
2626

27+
const rawRE = /(?:\?|&)raw(?:&|$)/;
28+
const urlRE = /(\?|&)url(?:&|$)/;
29+
2730
export default function assets({
2831
settings,
2932
logging,
@@ -233,6 +236,11 @@ export default function assets({
233236
resolvedConfig = viteConfig;
234237
},
235238
async load(id) {
239+
// If our import has the `?raw` or `?url` Vite query params, we'll let Vite handle it
240+
if (rawRE.test(id) || urlRE.test(id)) {
241+
return;
242+
}
243+
236244
const cleanedUrl = removeQueryString(id);
237245
if (/\.(jpeg|jpg|png|tiff|webp|gif|svg)$/.test(cleanedUrl)) {
238246
const meta = await emitESMImage(id, this.meta.watchMode, this.emitFile, settings);

packages/astro/test/core-image.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,33 @@ describe('astro:image', () => {
116116
});
117117
});
118118

119+
describe('vite-isms', () => {
120+
/**
121+
* @type {cheerio.CheerioAPI}
122+
*/
123+
let $;
124+
before(async () => {
125+
let res = await fixture.fetch('/vite');
126+
let html = await res.text();
127+
$ = cheerio.load(html);
128+
});
129+
130+
it('support ?url imports', () => {
131+
let $url = $('#url');
132+
expect($url.text()).to.equal('string');
133+
});
134+
135+
it('support ?raw imports', () => {
136+
let $raw = $('#raw');
137+
expect($raw.text()).to.equal('string');
138+
});
139+
140+
it('support glob import as raw', () => {
141+
let $raw = $('#glob-import');
142+
expect($raw.text()).to.equal('string');
143+
});
144+
});
145+
119146
describe('remote', () => {
120147
describe('working', () => {
121148
let $;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
import imageRaw from "../assets/penguin1.jpg?raw";
3+
import imageUrl from "../assets/penguin1.jpg?url";
4+
5+
const globImport = import.meta.glob('../assets/penguin1.jpg', { as: 'raw', eager: true })
6+
---
7+
8+
<div id="url">{typeof imageUrl}</div>
9+
<div id="raw">{typeof imageRaw}</div>
10+
<div id="glob-import">{typeof globImport['../assets/penguin1.jpg']}</div>

0 commit comments

Comments
 (0)