From 495f46bca78665732e51c629d93a68fa392b88a4 Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Tue, 10 Dec 2024 09:53:58 +0000 Subject: [PATCH] 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> --- .changeset/fast-adults-lick.md | 6 ++++++ packages/astro/src/assets/utils/node/emitAsset.ts | 4 +++- packages/astro/src/assets/vite-plugin-assets.ts | 7 ++++++- packages/astro/src/content/content-layer.ts | 1 + packages/astro/src/content/runtime-assets.ts | 2 ++ packages/astro/src/content/utils.ts | 10 +++++++++- .../astro/src/content/vite-plugin-content-imports.ts | 2 ++ .../integrations/markdoc/src/content-entry-type.ts | 1 + 8 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 .changeset/fast-adults-lick.md diff --git a/.changeset/fast-adults-lick.md b/.changeset/fast-adults-lick.md new file mode 100644 index 0000000000..22b2617bed --- /dev/null +++ b/.changeset/fast-adults-lick.md @@ -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 diff --git a/packages/astro/src/assets/utils/node/emitAsset.ts b/packages/astro/src/assets/utils/node/emitAsset.ts index 79a5287f64..1337ac8800 100644 --- a/packages/astro/src/assets/utils/node/emitAsset.ts +++ b/packages/astro/src/assets/utils/node/emitAsset.ts @@ -15,6 +15,7 @@ export async function emitESMImage( _watchMode: boolean, // 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. + experimentalSvgEnabled: boolean, fileEmitter?: FileEmitter, ): Promise { if (!id) { @@ -44,7 +45,8 @@ export async function emitESMImage( }); // 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; } diff --git a/packages/astro/src/assets/vite-plugin-assets.ts b/packages/astro/src/assets/vite-plugin-assets.ts index e09fc15975..abce0c9b7e 100644 --- a/packages/astro/src/assets/vite-plugin-assets.ts +++ b/packages/astro/src/assets/vite-plugin-assets.ts @@ -205,7 +205,12 @@ export default function assets({ settings }: { settings: AstroSettings }): vite. } 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) { throw new AstroError({ diff --git a/packages/astro/src/content/content-layer.ts b/packages/astro/src/content/content-layer.ts index 235bbb1fd1..71a58f6d17 100644 --- a/packages/astro/src/content/content-layer.ts +++ b/packages/astro/src/content/content-layer.ts @@ -206,6 +206,7 @@ export class ContentLayer { }, collectionWithResolvedSchema, false, + !!this.#settings.config.experimental.svg ); return parsedData; diff --git a/packages/astro/src/content/runtime-assets.ts b/packages/astro/src/content/runtime-assets.ts index 95b5092fe3..74204e127d 100644 --- a/packages/astro/src/content/runtime-assets.ts +++ b/packages/astro/src/content/runtime-assets.ts @@ -7,6 +7,7 @@ export function createImage( pluginContext: PluginContext, shouldEmitFile: boolean, entryFilePath: string, + experimentalSvgEnabled: boolean, ) { return () => { return z.string().transform(async (imagePath, ctx) => { @@ -14,6 +15,7 @@ export function createImage( const metadata = (await emitESMImage( resolvedFilePath, pluginContext.meta.watchMode, + experimentalSvgEnabled, shouldEmitFile ? pluginContext.emitFile : undefined, )) as OmitBrand; diff --git a/packages/astro/src/content/utils.ts b/packages/astro/src/content/utils.ts index 16d703fc78..3b9588c0b6 100644 --- a/packages/astro/src/content/utils.ts +++ b/packages/astro/src/content/utils.ts @@ -164,6 +164,7 @@ export async function getEntryDataAndImages< }, collectionConfig: CollectionConfig, shouldEmitFile: boolean, + experimentalSvgEnabled: boolean, pluginContext?: PluginContext, ): Promise<{ data: TOutputData; imageImports: Array }> { let data: TOutputData; @@ -182,7 +183,12 @@ export async function getEntryDataAndImages< if (typeof schema === 'function') { if (pluginContext) { schema = schema({ - image: createImage(pluginContext, shouldEmitFile, entry._internal.filePath), + image: createImage( + pluginContext, + shouldEmitFile, + entry._internal.filePath, + experimentalSvgEnabled, + ), }); } else if (collectionConfig.type === CONTENT_LAYER_TYPE) { schema = schema({ @@ -257,12 +263,14 @@ export async function getEntryData( }, collectionConfig: CollectionConfig, shouldEmitFile: boolean, + experimentalSvgEnabled: boolean, pluginContext?: PluginContext, ) { const { data } = await getEntryDataAndImages( entry, collectionConfig, shouldEmitFile, + experimentalSvgEnabled, pluginContext, ); return data; diff --git a/packages/astro/src/content/vite-plugin-content-imports.ts b/packages/astro/src/content/vite-plugin-content-imports.ts index 4950d2d9b2..68f9cf706f 100644 --- a/packages/astro/src/content/vite-plugin-content-imports.ts +++ b/packages/astro/src/content/vite-plugin-content-imports.ts @@ -245,6 +245,7 @@ async function getContentEntryModule( { id, collection, _internal, unvalidatedData }, collectionConfig, params.shouldEmitFile, + !!params.config.experimental.svg, pluginContext, ) : unvalidatedData; @@ -280,6 +281,7 @@ async function getDataEntryModule( { id, collection, _internal, unvalidatedData }, collectionConfig, params.shouldEmitFile, + !!params.config.experimental.svg, pluginContext, ) : unvalidatedData; diff --git a/packages/integrations/markdoc/src/content-entry-type.ts b/packages/integrations/markdoc/src/content-entry-type.ts index 67a8be5317..998d8fbb55 100644 --- a/packages/integrations/markdoc/src/content-entry-type.ts +++ b/packages/integrations/markdoc/src/content-entry-type.ts @@ -312,6 +312,7 @@ async function emitOptimizedImages( const src = await emitESMImage( resolved.id, ctx.pluginContext.meta.watchMode, + !!ctx.astroConfig.experimental.svg, ctx.pluginContext.emitFile, );