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 imports using ?raw and ?url not working when experimental.assets is enabled #7108

Merged
merged 3 commits into from
May 17, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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>