Skip to content

Commit 3537876

Browse files
authored
fix: passthroughImageService generate webp (#14776)
* test: Add passthrough-image-service fixture with config, assets, and page * test: Add tests to verify passthrough image service preserves formats and tags * fix: Add validateOptions to noopService to handle transform images * chore: Add changeset * chore: Fix changeset * fix: Simplify noop service by removing ESM image check and format field
1 parent 048e4dc commit 3537876

8 files changed

Lines changed: 114 additions & 0 deletions

File tree

.changeset/ripe-friends-play.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fixes the behavior of `passthroughImageService` so it does not generate webp.

packages/astro/src/assets/services/noop.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ import { baseService, type LocalImageService } from './service.js';
44
const noopService: LocalImageService = {
55
...baseService,
66
propertiesToHash: ['src'],
7+
async validateOptions(options, imageConfig) {
8+
const newOptions = await (baseService.validateOptions?.(options, imageConfig) ?? options);
9+
delete newOptions.format;
10+
11+
return newOptions;
12+
},
713
async transform(inputBuffer, transformOptions) {
814
return {
915
data: inputBuffer,
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineConfig, passthroughImageService } from 'astro/config';
2+
3+
export default defineConfig({
4+
image: {
5+
service: passthroughImageService(),
6+
},
7+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "@test/passthrough-image-service",
3+
"version": "0.0.0",
4+
"private": true,
5+
"dependencies": {
6+
"astro": "workspace:*"
7+
}
8+
}
11.3 KB
Loading
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
import { Image, Picture } from 'astro:assets';
3+
import myImage from '../assets/penguin1.jpg';
4+
---
5+
<html>
6+
<head></head>
7+
<body>
8+
<div id="image">
9+
<Image src={myImage} alt="a penguin" />
10+
</div>
11+
<div id="picture">
12+
<Picture src={myImage} alt="a penguin" />
13+
</div>
14+
</body>
15+
</html>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import assert from 'node:assert/strict';
2+
import { before, describe, it } from 'node:test';
3+
import * as cheerio from 'cheerio';
4+
import { loadFixture } from './test-utils.js';
5+
6+
describe('passthroughImageService', () => {
7+
/** @type {import('./test-utils.js').Fixture} */
8+
let fixture;
9+
10+
describe('build', () => {
11+
let $;
12+
13+
before(async () => {
14+
fixture = await loadFixture({
15+
root: './fixtures/passthrough-image-service/',
16+
});
17+
18+
await fixture.build();
19+
20+
const html = await fixture.readFile('/index.html');
21+
$ = cheerio.load(html);
22+
});
23+
24+
describe('Assets', () => {
25+
it('does not generate webp images', async () => {
26+
const webpImages = await fixture.glob('_astro/**/*.webp');
27+
assert.equal(webpImages.length, 0);
28+
});
29+
30+
it('preserves original image format', async () => {
31+
const jpgImages = await fixture.glob('_astro/**/*.jpg');
32+
assert.ok(jpgImages.length > 0);
33+
});
34+
});
35+
36+
describe('Image component', () => {
37+
it('includes img element', () => {
38+
const $img = $('#image img');
39+
assert.equal($img.length, 1);
40+
});
41+
42+
it('preserves original format', () => {
43+
const $img = $('#image img');
44+
const src = $img.attr('src');
45+
assert.ok(src.endsWith('.jpg'), `Should preserve jpg format, got: ${src}`);
46+
});
47+
});
48+
49+
describe('Picture component', () => {
50+
it('includes <picture> element', () => {
51+
const $picture = $('#picture picture');
52+
assert.equal($picture.length, 1);
53+
});
54+
55+
it('includes <img> inside picture', () => {
56+
const $img = $('#picture img');
57+
assert.equal($img.length, 1);
58+
});
59+
60+
it('preserves original format', () => {
61+
const $img = $('#picture img');
62+
const src = $img.attr('src');
63+
assert.ok(src.endsWith('.jpg'), `Should preserve jpg format, got: ${src}`);
64+
});
65+
});
66+
});
67+
});

pnpm-lock.yaml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)