Skip to content

Commit

Permalink
Fix imports using ?raw and ?url not working when `experimental.assets…
Browse files Browse the repository at this point in the history
…` is enabled (#7108)
  • Loading branch information
Princesseuh authored May 17, 2023
1 parent 3420261 commit 4104286
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/thirty-squids-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix imports using ?raw and ?url not working when `experimental.assets` is enabled
8 changes: 8 additions & 0 deletions packages/astro/src/assets/vite-plugin-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
27 changes: 27 additions & 0 deletions packages/astro/test/core-image.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 $;
Expand Down
10 changes: 10 additions & 0 deletions packages/astro/test/fixtures/core-image/src/pages/vite.astro
Original file line number Diff line number Diff line change
@@ -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>

0 comments on commit 4104286

Please sign in to comment.