Fix imports using ?raw and ?url not working when experimental.assets is enabled (#7108)

This commit is contained in:
Erika 2023-05-17 17:07:57 +02:00 committed by GitHub
parent 34202616c2
commit 410428672e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 50 additions and 0 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fix imports using ?raw and ?url not working when `experimental.assets` is enabled

View file

@ -24,6 +24,9 @@ import { hashTransform, propsToFilename } from './utils/transformToPath.js';
const resolvedVirtualModuleId = '\0' + VIRTUAL_MODULE_ID;
const rawRE = /(?:\?|&)raw(?:&|$)/;
const urlRE = /(\?|&)url(?:&|$)/;
export default function assets({
settings,
logging,
@ -233,6 +236,11 @@ 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)) {
return;
}
const cleanedUrl = removeQueryString(id);
if (/\.(jpeg|jpg|png|tiff|webp|gif|svg)$/.test(cleanedUrl)) {
const meta = await emitESMImage(id, this.meta.watchMode, this.emitFile, settings);

View file

@ -116,6 +116,33 @@ describe('astro:image', () => {
});
});
describe('vite-isms', () => {
/**
* @type {cheerio.CheerioAPI}
*/
let $;
before(async () => {
let res = await fixture.fetch('/vite');
let html = await res.text();
$ = cheerio.load(html);
});
it('support ?url imports', () => {
let $url = $('#url');
expect($url.text()).to.equal('string');
});
it('support ?raw imports', () => {
let $raw = $('#raw');
expect($raw.text()).to.equal('string');
});
it('support glob import as raw', () => {
let $raw = $('#glob-import');
expect($raw.text()).to.equal('string');
});
});
describe('remote', () => {
describe('working', () => {
let $;

View file

@ -0,0 +1,10 @@
---
import imageRaw from "../assets/penguin1.jpg?raw";
import imageUrl from "../assets/penguin1.jpg?url";
const globImport = import.meta.glob('../assets/penguin1.jpg', { as: 'raw', eager: true })
---
<div id="url">{typeof imageUrl}</div>
<div id="raw">{typeof imageRaw}</div>
<div id="glob-import">{typeof globImport['../assets/penguin1.jpg']}</div>