Skip to content
Closed
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/sour-zoos-punch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': major
---

Removes the `experimental.svgo` flag and replaces it with a new configuration option `svgo` - ([v6 upgrade guidance](https://deploy-preview-12322--astro-docs-2.netlify.app/en/guides/upgrade-to/v6/TODO:))
34 changes: 34 additions & 0 deletions .changeset/thin-weeks-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
'astro': minor
---

Adds SVGO optimization support for SVG assets

Astro now supports automatic SVG optimization using SVGO during build time. This feature helps reduce SVG file sizes while maintaining visual quality, improving your site's performance.

To enable SVG optimization with default settings, add the following to your `astro.config.mjs`:
```js
import { defineConfig } from 'astro/config';

export default defineConfig({
svgo: true,
});
```

To customize optimization, pass a [SVGO configuration object](https://svgo.dev/docs/plugins/):

```js
export default defineConfig({
svgo: {
plugins: [
'preset-default',
{
name: 'removeViewBox',
active: false
}
],
},
});
```

For more information on enabling and using this feature in your project, see the [configuration reference](https://docs.astro.build/en/reference/configuration/#svgo).
4 changes: 2 additions & 2 deletions packages/astro/src/assets/utils/svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function parseSvg({
}: {
path: string;
contents: string;
svgoConfig: AstroConfig['experimental']['svgo'];
svgoConfig: AstroConfig['svgo'];
}) {
let processedContents = contents;
if (svgoConfig) {
Expand Down Expand Up @@ -47,7 +47,7 @@ function parseSvg({
export function makeSvgComponent(
meta: ImageMetadata,
contents: Buffer | string,
svgoConfig: AstroConfig['experimental']['svgo'],
svgoConfig: AstroConfig['svgo'],
): string {
const file = typeof contents === 'string' ? contents : contents.toString('utf-8');
const { attributes, body: children } = parseSvg({
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/assets/vite-plugin-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ export default function assets({ fs, settings, sync, logger }: Options): vite.Pl
});
// We know that the contents are present, as we only emit this property for SVG files
return {
code: makeSvgComponent(imageMetadata, contents, settings.config.experimental.svgo),
code: makeSvgComponent(imageMetadata, contents, settings.config.svgo),
};
}
return {
Expand Down
10 changes: 5 additions & 5 deletions packages/astro/src/core/config/schemas/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ export const ASTRO_CONFIG_DEFAULTS = {
csp: false,
chromeDevtoolsWorkspace: false,
failOnPrerenderConflict: false,
svgo: false,
},
svgo: false,
} satisfies AstroUserConfig & { server: { open: boolean } };

const highlighterTypesSchema = z
Expand Down Expand Up @@ -463,6 +463,10 @@ export const AstroConfigSchema = z.object({
ttl: z.number().optional(),
})
.optional(),
svgo: z
.union([z.boolean(), z.custom<SvgoConfig>((value) => value && typeof value === 'object')])
.optional()
.default(ASTRO_CONFIG_DEFAULTS.svgo),
experimental: z
.object({
clientPrerender: z
Expand Down Expand Up @@ -505,10 +509,6 @@ export const AstroConfigSchema = z.object({
.boolean()
.optional()
.default(ASTRO_CONFIG_DEFAULTS.experimental.failOnPrerenderConflict),
svgo: z
.union([z.boolean(), z.custom<SvgoConfig>((value) => value && typeof value === 'object')])
.optional()
.default(ASTRO_CONFIG_DEFAULTS.experimental.svgo),
})
.strict(
`Invalid or outdated experimental feature.\nCheck for incorrect spelling or outdated Astro version.\nSee https://docs.astro.build/en/reference/experimental-flags/ for a list of all current experiments.`,
Expand Down
84 changes: 40 additions & 44 deletions packages/astro/src/types/public/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,46 @@ export interface AstroUserConfig<
*/
scopedStyleStrategy?: 'where' | 'class' | 'attribute';

/**
* @name svgo
* @type {boolean | SvgoConfig}
* @default `false`
* @description
* Enable SVG optimization using SVGO during build time.
*
* Set to `true` to enable optimization with default settings, or pass a configuration
* object to customize SVGO behavior.
*
* When enabled, all imported SVG files will be optimized for smaller file sizes
* and better performance while maintaining visual quality.
*
* ```js
* {
* // Enable with defaults
* svgo: true
* }
* ```
*
* To customize optimization, pass a [SVGO configuration object](https://svgo.dev/):
*
* ```js
* {
* svgo: {
* plugins: [
* 'preset-default',
* {
* name: 'removeViewBox',
* active: false
* }
* ]
* }
* }
* ```
*
* See the [SVGO optimization docs](https://docs.astro.build/en/reference/configuration/#svgo) for more information.
*/
svgo?: boolean | SvgoConfig;

/**
* @docs
* @name security
Expand Down Expand Up @@ -2412,50 +2452,6 @@ export interface AstroUserConfig<
* See the [experimental Chrome DevTools workspace feature documentation](https://docs.astro.build/en/reference/experimental-flags/chrome-devtools-workspace/) for more information.
*/
chromeDevtoolsWorkspace?: boolean;

/**
* @name experimental.svgo
* @type {boolean | SvgoConfig}
* @default `false`
* @description
* Enable SVG optimization using SVGO during build time.
*
* Set to `true` to enable optimization with default settings, or pass a configuration
* object to customize SVGO behavior.
*
* When enabled, all imported SVG files will be optimized for smaller file sizes
* and better performance while maintaining visual quality.
*
* ```js
* {
* experimental: {
* // Enable with defaults
* svgo: true
* }
* }
* ```
*
* To customize optimization, pass a [SVGO configuration object](https://svgo.dev/):
*
* ```js
* {
* experimental: {
* svgo: {
* plugins: [
* 'preset-default',
* {
* name: 'removeViewBox',
* active: false
* }
* ]
* }
* }
* }
* ```
*
* See the [experimental SVGO optimization docs](https://docs.astro.build/en/reference/experimental-flags/svg-optimization/) for more information.
*/
svgo?: boolean | SvgoConfig;
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { defineConfig } from 'astro/config';

export default defineConfig({
experimental: {
svgo: {
plugins: [
'preset-default',
{
name: 'removeViewBox',
active: false,
},
],
},
svgo: {
plugins: [
'preset-default',
{
name: 'removeViewBox',
active: false,
},
],
},
});