Skip to content

Commit 8986051

Browse files
committed
refactor: simplify svgo config
1 parent ee8f98a commit 8986051

File tree

6 files changed

+75
-83
lines changed

6 files changed

+75
-83
lines changed

.changeset/cyan-tables-poke.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,30 @@ Adds experimental SVGO optimization support for SVG assets
66

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

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

1313
export default defineConfig({
1414
experimental: {
15-
svg: {
16-
optimize: true,
17-
},
15+
svgo: true,
1816
},
1917
});
2018
```
2119

22-
With SVG optimization enabled, you can further customize behavior with [SVGO plugins](https://svgo.dev/docs/plugins/):
20+
To customize optimization, pass a [SVGO configuration object](https://svgo.dev/docs/plugins/):
2321

2422
```js
2523
export default defineConfig({
2624
experimental: {
27-
svg: {
28-
optimize: true,
29-
svgoConfig: {
30-
plugins: ['preset-default'],
31-
},
25+
svgo: {
26+
plugins: [
27+
'preset-default',
28+
{
29+
name: 'removeViewBox',
30+
active: false
31+
}
32+
],
3233
},
3334
},
3435
});

packages/astro/src/assets/utils/svg.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ import type { SvgComponentProps } from '../runtime.js';
66
import { dropAttributes } from '../runtime.js';
77
import type { ImageMetadata } from '../types.js';
88

9-
function parseSvg(contents: string, svgConfig: AstroConfig['experimental']['svg']) {
9+
function parseSvg(contents: string, svgoConfig: AstroConfig['experimental']['svgo']) {
1010
let processedContents = contents;
11-
if (svgConfig?.optimize) {
11+
if (svgoConfig) {
1212
try {
13-
const result = optimize(contents, svgConfig.svgoConfig);
13+
const config = typeof svgoConfig === 'boolean' ? undefined : svgoConfig;
14+
const result = optimize(contents, config);
1415
processedContents = result.data;
1516
} catch (cause) {
1617
throw new AstroError(AstroErrorData.CannotOptimizeSvg, { cause });
@@ -32,10 +33,10 @@ function parseSvg(contents: string, svgConfig: AstroConfig['experimental']['svg'
3233
export function makeSvgComponent(
3334
meta: ImageMetadata,
3435
contents: Buffer | string,
35-
svgConfig: AstroConfig['experimental']['svg'],
36+
svgoConfig: AstroConfig['experimental']['svgo'],
3637
): string {
3738
const file = typeof contents === 'string' ? contents : contents.toString('utf-8');
38-
const { attributes, body: children } = parseSvg(file, svgConfig);
39+
const { attributes, body: children } = parseSvg(file, svgoConfig);
3940
const props: SvgComponentProps = {
4041
meta,
4142
attributes: dropAttributes(attributes),

packages/astro/src/assets/vite-plugin-assets.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ export default function assets({ fs, settings, sync, logger }: Options): vite.Pl
273273
});
274274
// We know that the contents are present, as we only emit this property for SVG files
275275
return {
276-
code: makeSvgComponent(imageMetadata, contents, settings.config.experimental.svg),
276+
code: makeSvgComponent(imageMetadata, contents, settings.config.experimental.svgo),
277277
};
278278
}
279279
return {

packages/astro/src/core/config/schemas/base.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,7 @@ export const ASTRO_CONFIG_DEFAULTS = {
107107
staticImportMetaEnv: false,
108108
chromeDevtoolsWorkspace: false,
109109
failOnPrerenderConflict: false,
110-
svg: {
111-
optimize: false,
112-
},
110+
svgo: false,
113111
},
114112
} satisfies AstroUserConfig & { server: { open: boolean } };
115113

@@ -530,14 +528,10 @@ export const AstroConfigSchema = z.object({
530528
.boolean()
531529
.optional()
532530
.default(ASTRO_CONFIG_DEFAULTS.experimental.failOnPrerenderConflict),
533-
svg: z
534-
.object({
535-
optimize: z.boolean().default(ASTRO_CONFIG_DEFAULTS.experimental.svg.optimize),
536-
svgoConfig: z
537-
.custom<SvgoConfig>((value) => value && typeof value === 'object')
538-
.optional(),
539-
})
540-
.optional(),
531+
svgo: z
532+
.union([z.boolean(), z.custom<SvgoConfig>((value) => value && typeof value === 'object')])
533+
.optional()
534+
.default(ASTRO_CONFIG_DEFAULTS.experimental.svgo),
541535
})
542536
.strict(
543537
`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.`,

packages/astro/src/types/public/config.ts

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2540,49 +2540,48 @@ export interface AstroUserConfig<
25402540
chromeDevtoolsWorkspace?: boolean;
25412541

25422542
/**
2543-
* @kind heading
2544-
* @name SVG Options
2543+
* @name experimental.svgo
2544+
* @type {boolean | SvgoConfig}
2545+
* @default `false`
2546+
* @description
2547+
* Enable SVG optimization using SVGO during build time.
2548+
*
2549+
* Set to `true` to enable optimization with default settings, or pass a configuration
2550+
* object to customize SVGO behavior.
2551+
*
2552+
* When enabled, all imported SVG files will be optimized for smaller file sizes
2553+
* and better performance while maintaining visual quality.
2554+
*
2555+
* ```js
2556+
* {
2557+
* experimental: {
2558+
* // Enable with defaults
2559+
* svgo: true
2560+
* }
2561+
* }
2562+
* ```
2563+
*
2564+
* To customize optimization, pass a [SVGO configuration object](https://svgo.dev/):
2565+
*
2566+
* ```js
2567+
* {
2568+
* experimental: {
2569+
* svgo: {
2570+
* plugins: [
2571+
* 'preset-default',
2572+
* {
2573+
* name: 'removeViewBox',
2574+
* active: false
2575+
* }
2576+
* ]
2577+
* }
2578+
* }
2579+
* }
2580+
* ```
2581+
*
2582+
* See the [experimental SVGO optimization docs](https://docs.astro.build/en/reference/experimental-flags/svg-optimization/) for more information.
25452583
*/
2546-
svg?: {
2547-
/**
2548-
* @name experimental.svg.optimize
2549-
* @type {boolean}
2550-
* @default `true`
2551-
* @description
2552-
* Whether to enable SVG optimization using SVGO during build time.
2553-
*
2554-
* When enabled, all imported SVG files will be optimized for smaller file sizes
2555-
* and better performance while maintaining visual quality.
2556-
*/
2557-
optimize?: boolean;
2558-
2559-
/**
2560-
* @name experimental.svg.svgoConfig
2561-
* @type {SvgoConfig}
2562-
* @default `{}`
2563-
* @description
2564-
* Configuration object passed directly to SVGO for customizing SVG optimization.
2565-
*
2566-
* See [SVGO documentation](https://svgo.dev/) for available options.
2567-
*
2568-
* ```js
2569-
* {
2570-
* svg: {
2571-
* svgoConfig: {
2572-
* plugins: [
2573-
* 'preset-default',
2574-
* {
2575-
* name: 'removeViewBox',
2576-
* active: false
2577-
* }
2578-
* ]
2579-
* }
2580-
* }
2581-
* }
2582-
* ```
2583-
*/
2584-
svgoConfig?: SvgoConfig;
2585-
};
2584+
svgo?: boolean | SvgoConfig;
25862585
};
25872586
}
25882587

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
import { defineConfig } from 'astro/config';
22

33
export default defineConfig({
4-
experimental: {
5-
svg: {
6-
optimize: true,
7-
svgoConfig: {
8-
plugins: [
9-
'preset-default',
10-
{
11-
name: 'removeViewBox',
12-
active: false
13-
}
14-
]
15-
}
16-
}
17-
}
4+
experimental: {
5+
svgo: {
6+
plugins: [
7+
'preset-default',
8+
{
9+
name: 'removeViewBox',
10+
active: false,
11+
},
12+
],
13+
},
14+
},
1815
});

0 commit comments

Comments
 (0)