fix: imports for images with uppercase extensions not working (#8437)

This commit is contained in:
Erika 2023-09-06 20:52:52 +02:00 committed by GitHub
parent 6df4f3bd9d
commit b3cf1b3276
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 3 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fix imports of images with uppercased file extensions not working

View file

@ -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,
@ -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)}`;
}

View file

@ -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} */
@ -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', () => {

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

View file

@ -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>