Skip to content
Open
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/dull-baths-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': minor
---

Add an option to specify default image output format
10 changes: 5 additions & 5 deletions packages/astro/src/assets/services/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { isRemoteAllowed } from '@astrojs/internal-helpers/remote';
import { AstroError, AstroErrorData } from '../../core/errors/index.js';
import { isRemotePath, joinPaths } from '../../core/path.js';
import type { AstroConfig } from '../../types/public/config.js';
import { DEFAULT_HASH_PROPS, DEFAULT_OUTPUT_FORMAT, VALID_SUPPORTED_FORMATS } from '../consts.js';
import { DEFAULT_HASH_PROPS, VALID_SUPPORTED_FORMATS } from '../consts.js';
import type {
ImageFit,
ImageOutputFormat,
Expand Down Expand Up @@ -215,7 +215,7 @@ export function verifyOptions(options: ImageTransform): void {
*/
export const baseService: Omit<LocalImageService, 'transform'> = {
propertiesToHash: DEFAULT_HASH_PROPS,
validateOptions(options) {
validateOptions(options, imageConfig) {
// We currently do not support processing SVGs, so whenever the input format is a SVG, force the output to also be one
if (isESMImportedImage(options.src) && options.src.format === 'svg') {
options.format = 'svg';
Expand All @@ -226,7 +226,7 @@ export const baseService: Omit<LocalImageService, 'transform'> = {

// Apply defaults and normalization separate from verification
if (!options.format) {
options.format = DEFAULT_OUTPUT_FORMAT;
options.format = imageConfig.outputFormat;
}
if (options.width) options.width = Math.round(options.width);
if (options.height) options.height = Math.round(options.height);
Expand Down Expand Up @@ -264,11 +264,11 @@ export const baseService: Omit<LocalImageService, 'transform'> = {
decoding: attributes.decoding ?? 'async',
};
},
getSrcSet(options): Array<UnresolvedSrcSetValue> {
getSrcSet(options, imageConfig): Array<UnresolvedSrcSetValue> {
const { targetWidth, targetHeight } = getTargetDimensions(options);
const aspectRatio = targetWidth / targetHeight;
const { widths, densities } = options;
const targetFormat = options.format ?? DEFAULT_OUTPUT_FORMAT;
const targetFormat = options.format ?? imageConfig.outputFormat;

let transformedWidths = (widths ?? []).sort(sortNumeric);

Expand Down
3 changes: 3 additions & 0 deletions packages/astro/src/core/config/schemas/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { markdownConfigDefaults, syntaxHighlightDefaults } from '@astrojs/markdo
import { type BuiltinTheme, bundledThemes } from 'shiki';
import type { Config as SvgoConfig } from 'svgo';
import { z } from 'zod';
import { DEFAULT_OUTPUT_FORMAT, VALID_OUTPUT_FORMATS } from '../../../assets/consts.js';
import { localFontFamilySchema, remoteFontFamilySchema } from '../../../assets/fonts/config.js';
import { EnvSchema } from '../../../env/schema.js';
import type { AstroUserConfig, ViteUserConfig } from '../../../types/public/config.js';
Expand Down Expand Up @@ -68,6 +69,7 @@ export const ASTRO_CONFIG_DEFAULTS = {
},
image: {
endpoint: { entrypoint: undefined, route: '/_image' },
outputFormat: DEFAULT_OUTPUT_FORMAT,
service: { entrypoint: 'astro/assets/services/sharp', config: {} },
responsiveStyles: false,
},
Expand Down Expand Up @@ -264,6 +266,7 @@ export const AstroConfigSchema = z.object({
entrypoint: z.string().optional(),
})
.default(ASTRO_CONFIG_DEFAULTS.image.endpoint),
outputFormat: z.enum(VALID_OUTPUT_FORMATS).default(ASTRO_CONFIG_DEFAULTS.image.outputFormat),
service: z
.object({
entrypoint: z
Expand Down
21 changes: 20 additions & 1 deletion packages/astro/src/types/public/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { Config as SvgoConfig } from 'svgo';
import type { BuiltinDriverName, BuiltinDriverOptions, Driver, Storage } from 'unstorage';
import type { UserConfig as OriginalViteUserConfig, SSROptions as ViteSSROptions } from 'vite';
import type { FontFamily, FontProvider } from '../../assets/fonts/types.js';
import type { ImageFit, ImageLayout } from '../../assets/types.js';
import type { ImageFit, ImageLayout, ImageOutputFormat } from '../../assets/types.js';
import type { AssetsPrefix } from '../../core/app/types.js';
import type { AstroConfigType } from '../../core/config/schemas/index.js';
import type { REDIRECT_STATUS_CODES } from '../../core/constants.js';
Expand Down Expand Up @@ -1271,6 +1271,25 @@ export interface AstroUserConfig<
entrypoint: undefined | string;
};

/**
* @docs
* @name image.outputFormat
* @type {ImageOutputFormat}
* @default `webp`
* @description
* The default output format for images.
*
* ```js
* {
* image: {
* // Example: Output `avif` files by default
* outputFormat: 'avif',
* },
* }
* ```
*/
outputFormat?: ImageOutputFormat;

/**
* @docs
* @name image.service
Expand Down