Skip to content

Commit

Permalink
fix: imports for images with uppercase extensions not working (#8437)
Browse files Browse the repository at this point in the history
  • Loading branch information
Princesseuh committed Sep 6, 2023
1 parent 6df4f3b commit b3cf1b3
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/nice-sheep-bathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix imports of images with uppercased file extensions not working
6 changes: 4 additions & 2 deletions packages/astro/src/assets/vite-plugin-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import {
prependForwardSlash,
removeQueryString,
} from '../core/path.js';
import { VIRTUAL_MODULE_ID, VIRTUAL_SERVICE_ID } from './consts.js';
import { VALID_INPUT_FORMATS, VIRTUAL_MODULE_ID, VIRTUAL_SERVICE_ID } from './consts.js';
import { emitESMImage } from './utils/emitAsset.js';
import { hashTransform, propsToFilename } from './utils/transformToPath.js';

const resolvedVirtualModuleId = '\0' + VIRTUAL_MODULE_ID;

const assetRegex = new RegExp(`\.(${VALID_INPUT_FORMATS.join('|')})$`, 'i');

export default function assets({
settings,
mode,
Expand Down Expand Up @@ -121,7 +123,7 @@ export default function assets({
if (id !== removeQueryString(id)) {
return;
}
if (/\.(jpeg|jpg|png|tiff|webp|gif|svg)$/.test(id)) {
if (assetRegex.test(id)) {
const meta = await emitESMImage(id, this.meta.watchMode, this.emitFile);
return `export default ${JSON.stringify(meta)}`;
}
Expand Down
17 changes: 16 additions & 1 deletion packages/astro/test/core-image.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import * as cheerio from 'cheerio';
import { basename } from 'node:path';
import { Writable } from 'node:stream';
import { removeDir } from '../dist/core/fs/index.js';
import { Logger } from '../dist/core/logger/core.js';
import testAdapter from './test-adapter.js';
import { testImageService } from './test-image-service.js';
import { loadFixture } from './test-utils.js';
import { Logger } from '../dist/core/logger/core.js';

describe('astro:image', () => {
/** @type {import('./test-utils').Fixture} */
Expand Down Expand Up @@ -159,6 +159,21 @@ describe('astro:image', () => {
res = await fixture.fetch(src);
expect(res.status).to.equal(200);
});

it('supports uppercased imports', async () => {
let res = await fixture.fetch('/uppercase');
let html = await res.text();
$ = cheerio.load(html);

let $img = $('img');
expect($img).to.have.a.lengthOf(1);

let src = $img.attr('src');
let loading = $img.attr('loading');
res = await fixture.fetch(src);
expect(res.status).to.equal(200);
expect(loading).to.not.be.undefined;
});
});

describe('vite-isms', () => {
Expand Down
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
@@ -0,0 +1,6 @@
---
import { Image } from "astro:assets";
import walrus from "../assets/walrus.JPG";
---

<Image src={walrus} alt="My favorite animal, the walrus"></Image>

0 comments on commit b3cf1b3

Please sign in to comment.