Skip to content
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

fix: experimental svg types #12625

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/rare-cooks-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes an issue where the `experimental.svg` had incorrect type, resulting in some errors in the editors.
2 changes: 1 addition & 1 deletion packages/astro/src/assets/utils/svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export type SvgRenderMode = 'inline' | 'sprite';
export function makeSvgComponent(
meta: ImageMetadata,
contents: Buffer | string,
options?: { mode?: SvgRenderMode },
options?: { mode: SvgRenderMode },
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having an optional mode should be a bug. Users should either provide a boolean, or provide an object with explicit mode

) {
const file = typeof contents === 'string' ? contents : contents.toString('utf-8');
const { attributes, body: children } = parseSvg(file);
Expand Down
17 changes: 9 additions & 8 deletions packages/astro/src/core/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { OutgoingHttpHeaders } from 'node:http';
import path from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import { z } from 'zod';
import type { SvgRenderMode } from '../../assets/utils/svg.js';
import { EnvSchema } from '../../env/schema.js';
import type { AstroUserConfig, ViteUserConfig } from '../../types/public/config.js';
import { appendForwardSlash, prependForwardSlash, removeTrailingForwardSlash } from '../path.js';
Expand Down Expand Up @@ -96,9 +97,7 @@ export const ASTRO_CONFIG_DEFAULTS = {
clientPrerender: false,
contentIntellisense: false,
responsiveImages: false,
svg: {
mode: 'inline',
},
svg: false,
},
} satisfies AstroUserConfig & { server: { open: boolean } };

Expand Down Expand Up @@ -541,17 +540,19 @@ export const AstroConfigSchema = z.object({
.union([
z.boolean(),
z.object({
mode: z
.union([z.literal('inline'), z.literal('sprite')])
.optional()
.default(ASTRO_CONFIG_DEFAULTS.experimental.svg.mode),
mode: z.union([z.literal('inline'), z.literal('sprite')]),
}),
])
.optional()
.default(ASTRO_CONFIG_DEFAULTS.experimental.svg)
.transform((svgConfig) => {
// Handle normalization of `experimental.svg` config boolean values
if (typeof svgConfig === 'boolean') {
return svgConfig ? ASTRO_CONFIG_DEFAULTS.experimental.svg : undefined;
return svgConfig
? {
mode: 'inline' as SvgRenderMode,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cast make TS happy

}
: undefined;
}
return svgConfig;
}),
Expand Down
46 changes: 24 additions & 22 deletions packages/astro/src/types/public/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1940,28 +1940,30 @@ export interface ViteUserConfig extends OriginalViteUserConfig {
* For a complete overview, and to give feedback on this experimental API,
* see the [Feature RFC](https://github.com/withastro/roadmap/pull/1035).
*/
svg?: {
/**
*
* @name experimental.svg.mode
* @type {string}
* @default 'inline'
*
* The default technique for handling imported SVG files. Astro will inline the SVG content into your HTML output if not specified.
*
* - `inline`: Astro will inline the SVG content into your HTML output.
* - `sprite`: Astro will generate a sprite sheet with all imported SVG files.
*
* ```astro
* ---
* import Logo from './path/to/svg/file.svg';
* ---
*
* <Logo size={24} mode="sprite" />
* ```
*/
mode?: SvgRenderMode;
};
svg?:
| boolean
| {
/**
*
* @name experimental.svg.mode
* @type {string}
* @default 'inline'
*
* The default technique for handling imported SVG files. Astro will inline the SVG content into your HTML output if not specified.
*
* - `inline`: Astro will inline the SVG content into your HTML output.
* - `sprite`: Astro will generate a sprite sheet with all imported SVG files.
*
* ```astro
* ---
* import Logo from './path/to/svg/file.svg';
* ---
*
* <Logo size={24} mode="sprite" />
* ```
*/
mode: SvgRenderMode;
};
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import { defineConfig } from 'astro/config';
export default defineConfig({
integrations: [mdx()],
experimental: {
svg: {}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This way of passing the option shouldn't be valid

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC passing {} is like passing true. Couldn't we support {} without specifying mode too? It feels to me futureproofing if it ever has more optional options in the object, so you don't have to specify mode always.

Copy link
Member Author

@ematipico ematipico Dec 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can do everything, but what does svg: {} even mean to the user and to us? I don't want to be pedantic, I'll see what I can do

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I applied the change in 26c65f2 (#12625) and tested that svg: {} is still a valid configuration

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't mean a lot currently, but it's futureproofing if we add more options to it in the future, so I think it's an ok tradeoff for now as long as we don't recommend people to enable with {} only.

svg: true
}
});
6 changes: 0 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading