-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
feat: svg optimizer #16333
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
Merged
Merged
feat: svg optimizer #16333
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c12ad46
feat: svg optimizer
florian-lefebvre 38551f2
update test
florian-lefebvre 410cd93
changeset
florian-lefebvre bf66d26
export type
florian-lefebvre cf94256
Update packages/astro/src/assets/svg/svgo.ts
florian-lefebvre e428b9a
Merge branch 'main' into feat/svg-optimizer
florian-lefebvre 6a3b17f
Update .changeset/twelve-times-slide.md
florian-lefebvre 46888fa
Merge branch 'main' into feat/svg-optimizer
Princesseuh 52d4201
fix: test
Princesseuh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| --- | ||
| 'astro': minor | ||
| --- | ||
|
|
||
| Adds an experimental flag `svgOptimizer` that enables automatic optimization of your SVG components using the provided optimizer. This supersedes the `svgo` experimental flag, which is now removed. | ||
|
|
||
| When enabled, your imported SVG files used as components will be optimized for smaller file sizes and better performance while maintaining visual quality. This can significantly reduce the size of your SVG assets by removing unnecessary metadata, comments, and redundant code. | ||
|
|
||
| Astro ships with a [SVGO](https://svgo.dev/) based optimizer but any can be used. | ||
|
|
||
| To enable this feature, add the experimental flag in your Astro config and remove `svgo` if it was enabled: | ||
|
|
||
|
|
||
| ```diff | ||
| // astro.config.mjs | ||
| -import { defineConfig } from "astro/config"; | ||
| +import { defineConfig, svgoOptimizer } from "astro/config"; | ||
|
|
||
| export default defineConfig({ | ||
| + experimental: { | ||
| + svgOptimizer: svgoOptimizer() | ||
| - svgo: true | ||
| + } | ||
| }); | ||
| ``` | ||
|
|
||
| For more information on enabling and using this feature in your project, see the [experimental SVG optimization docs](https://docs.astro.build/en/reference/experimental-flags/svg-optimization/). | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import * as z from 'zod/v4'; | ||
| import type { SvgOptimizer } from './types.js'; | ||
|
|
||
| export const SvgOptimizerSchema = z.object({ | ||
| name: z.string(), | ||
| optimize: z.custom<SvgOptimizer['optimize']>((v) => typeof v === 'function'), | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import type { SvgOptimizer } from './types.js'; | ||
| import { optimize, type Config } from 'svgo'; | ||
|
|
||
| /** SVG optimizer using pass a [SVGO](https://svgo.dev/). */ | ||
|
florian-lefebvre marked this conversation as resolved.
Outdated
|
||
| export function svgoOptimizer(config?: Config): SvgOptimizer { | ||
| return { | ||
| name: 'svgo', | ||
| optimize: (contents) => optimize(contents, config).data, | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| export interface SvgOptimizer { | ||
| name: string; | ||
| optimize: (contents: string) => string | Promise<string>; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
packages/astro/test/fixtures/core-image-svg-optimized/astro.config.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,15 @@ | ||
| import { defineConfig } from 'astro/config'; | ||
| import { defineConfig, svgoOptimizer } from 'astro/config'; | ||
|
|
||
| export default defineConfig({ | ||
| experimental: { | ||
| svgo: { | ||
| svgOptimizer: svgoOptimizer({ | ||
| plugins: [ | ||
| 'preset-default', | ||
| { | ||
| name: 'removeViewBox', | ||
| active: false, | ||
| }, | ||
| ], | ||
| }, | ||
| }), | ||
| }, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.