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(svg): conditional opt-in #12694

Merged
merged 3 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions .changeset/fast-adults-lick.md
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion packages/astro/src/assets/utils/node/emitAsset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ImageMetadataWithContents | undefined> {
if (!id) {
Expand Down Expand Up @@ -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) {
ematipico marked this conversation as resolved.
Show resolved Hide resolved
emittedImage.contents = fileData;
}

Expand Down
7 changes: 6 additions & 1 deletion packages/astro/src/assets/vite-plugin-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/content/content-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ export class ContentLayer {
},
collectionWithResolvedSchema,
false,
!!this.#settings.config.experimental.svg
);

return parsedData;
Expand Down
2 changes: 2 additions & 0 deletions packages/astro/src/content/runtime-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ export function createImage(
pluginContext: PluginContext,
shouldEmitFile: boolean,
entryFilePath: string,
experimentalSvgEnabled: boolean,
) {
return () => {
return z.string().transform(async (imagePath, ctx) => {
const resolvedFilePath = (await pluginContext.resolve(imagePath, entryFilePath))?.id;
const metadata = (await emitESMImage(
resolvedFilePath,
pluginContext.meta.watchMode,
experimentalSvgEnabled,
shouldEmitFile ? pluginContext.emitFile : undefined,
)) as OmitBrand<ImageMetadata>;

Expand Down
10 changes: 9 additions & 1 deletion packages/astro/src/content/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ export async function getEntryDataAndImages<
},
collectionConfig: CollectionConfig,
shouldEmitFile: boolean,
experimentalSvgEnabled: boolean,
pluginContext?: PluginContext,
): Promise<{ data: TOutputData; imageImports: Array<string> }> {
let data: TOutputData;
Expand All @@ -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({
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions packages/astro/src/content/vite-plugin-content-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ async function getContentEntryModule(
{ id, collection, _internal, unvalidatedData },
collectionConfig,
params.shouldEmitFile,
!!params.config.experimental.svg,
pluginContext,
)
: unvalidatedData;
Expand Down Expand Up @@ -280,6 +281,7 @@ async function getDataEntryModule(
{ id, collection, _internal, unvalidatedData },
collectionConfig,
params.shouldEmitFile,
!!params.config.experimental.svg,
pluginContext,
)
: unvalidatedData;
Expand Down
1 change: 1 addition & 0 deletions packages/integrations/markdoc/src/content-entry-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ async function emitOptimizedImages(
const src = await emitESMImage(
resolved.id,
ctx.pluginContext.meta.watchMode,
!!ctx.astroConfig.experimental.svg,
ctx.pluginContext.emitFile,
);

Expand Down
Loading