0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

fix(svg): conditional opt-in (#12694)

* fix(svg): conditional opt-in

* add todo

* Update packages/astro/src/assets/utils/node/emitAsset.ts

Co-authored-by: Erika <3019731+Princesseuh@users.noreply.github.com>

---------

Co-authored-by: Erika <3019731+Princesseuh@users.noreply.github.com>
This commit is contained in:
Emanuele Stoppa 2024-12-10 09:53:58 +00:00 committed by GitHub
parent 7dc2fca2ee
commit 495f46bca7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 30 additions and 3 deletions

View file

@ -0,0 +1,6 @@
---
'@astrojs/markdoc': patch
'astro': patch
---
Fixes a bug where the experimental feature `experimental.svg` was incorrectly used when generating ESM images

View file

@ -15,6 +15,7 @@ export async function emitESMImage(
_watchMode: boolean, _watchMode: boolean,
// FIX: in Astro 6, this function should not be passed in dev mode at all. // FIX: in Astro 6, this function should not be passed in dev mode at all.
// Or rethink the API so that a function that throws isn't passed through. // Or rethink the API so that a function that throws isn't passed through.
experimentalSvgEnabled: boolean,
fileEmitter?: FileEmitter, fileEmitter?: FileEmitter,
): Promise<ImageMetadataWithContents | undefined> { ): Promise<ImageMetadataWithContents | undefined> {
if (!id) { if (!id) {
@ -44,7 +45,8 @@ export async function emitESMImage(
}); });
// Attach file data for SVGs // Attach file data for SVGs
if (fileMetadata.format === 'svg') { // TODO: this is a workaround to prevent a memory leak, and it must be fixed before we remove the experimental flag, see
if (fileMetadata.format === 'svg' && experimentalSvgEnabled === true) {
emittedImage.contents = fileData; emittedImage.contents = fileData;
} }

View file

@ -205,7 +205,12 @@ export default function assets({ settings }: { settings: AstroSettings }): vite.
} }
const emitFile = shouldEmitFile ? this.emitFile : undefined; const emitFile = shouldEmitFile ? this.emitFile : undefined;
const imageMetadata = await emitESMImage(id, this.meta.watchMode, emitFile); const imageMetadata = await emitESMImage(
id,
this.meta.watchMode,
!!settings.config.experimental.svg,
emitFile,
);
if (!imageMetadata) { if (!imageMetadata) {
throw new AstroError({ throw new AstroError({

View file

@ -206,6 +206,7 @@ export class ContentLayer {
}, },
collectionWithResolvedSchema, collectionWithResolvedSchema,
false, false,
!!this.#settings.config.experimental.svg
); );
return parsedData; return parsedData;

View file

@ -7,6 +7,7 @@ export function createImage(
pluginContext: PluginContext, pluginContext: PluginContext,
shouldEmitFile: boolean, shouldEmitFile: boolean,
entryFilePath: string, entryFilePath: string,
experimentalSvgEnabled: boolean,
) { ) {
return () => { return () => {
return z.string().transform(async (imagePath, ctx) => { return z.string().transform(async (imagePath, ctx) => {
@ -14,6 +15,7 @@ export function createImage(
const metadata = (await emitESMImage( const metadata = (await emitESMImage(
resolvedFilePath, resolvedFilePath,
pluginContext.meta.watchMode, pluginContext.meta.watchMode,
experimentalSvgEnabled,
shouldEmitFile ? pluginContext.emitFile : undefined, shouldEmitFile ? pluginContext.emitFile : undefined,
)) as OmitBrand<ImageMetadata>; )) as OmitBrand<ImageMetadata>;

View file

@ -164,6 +164,7 @@ export async function getEntryDataAndImages<
}, },
collectionConfig: CollectionConfig, collectionConfig: CollectionConfig,
shouldEmitFile: boolean, shouldEmitFile: boolean,
experimentalSvgEnabled: boolean,
pluginContext?: PluginContext, pluginContext?: PluginContext,
): Promise<{ data: TOutputData; imageImports: Array<string> }> { ): Promise<{ data: TOutputData; imageImports: Array<string> }> {
let data: TOutputData; let data: TOutputData;
@ -182,7 +183,12 @@ export async function getEntryDataAndImages<
if (typeof schema === 'function') { if (typeof schema === 'function') {
if (pluginContext) { if (pluginContext) {
schema = schema({ schema = schema({
image: createImage(pluginContext, shouldEmitFile, entry._internal.filePath), image: createImage(
pluginContext,
shouldEmitFile,
entry._internal.filePath,
experimentalSvgEnabled,
),
}); });
} else if (collectionConfig.type === CONTENT_LAYER_TYPE) { } else if (collectionConfig.type === CONTENT_LAYER_TYPE) {
schema = schema({ schema = schema({
@ -257,12 +263,14 @@ export async function getEntryData(
}, },
collectionConfig: CollectionConfig, collectionConfig: CollectionConfig,
shouldEmitFile: boolean, shouldEmitFile: boolean,
experimentalSvgEnabled: boolean,
pluginContext?: PluginContext, pluginContext?: PluginContext,
) { ) {
const { data } = await getEntryDataAndImages( const { data } = await getEntryDataAndImages(
entry, entry,
collectionConfig, collectionConfig,
shouldEmitFile, shouldEmitFile,
experimentalSvgEnabled,
pluginContext, pluginContext,
); );
return data; return data;

View file

@ -245,6 +245,7 @@ async function getContentEntryModule(
{ id, collection, _internal, unvalidatedData }, { id, collection, _internal, unvalidatedData },
collectionConfig, collectionConfig,
params.shouldEmitFile, params.shouldEmitFile,
!!params.config.experimental.svg,
pluginContext, pluginContext,
) )
: unvalidatedData; : unvalidatedData;
@ -280,6 +281,7 @@ async function getDataEntryModule(
{ id, collection, _internal, unvalidatedData }, { id, collection, _internal, unvalidatedData },
collectionConfig, collectionConfig,
params.shouldEmitFile, params.shouldEmitFile,
!!params.config.experimental.svg,
pluginContext, pluginContext,
) )
: unvalidatedData; : unvalidatedData;

View file

@ -312,6 +312,7 @@ async function emitOptimizedImages(
const src = await emitESMImage( const src = await emitESMImage(
resolved.id, resolved.id,
ctx.pluginContext.meta.watchMode, ctx.pluginContext.meta.watchMode,
!!ctx.astroConfig.experimental.svg,
ctx.pluginContext.emitFile, ctx.pluginContext.emitFile,
); );