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 for images with uppercase extensions not working #8437

Merged
merged 2 commits into from
Sep 6, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>