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

feat: Rework image generation to improve performance #8821

Merged
merged 14 commits into from
Oct 25, 2023
Merged
5 changes: 5 additions & 0 deletions .changeset/good-mirrors-bake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': minor
---

Add concurrency to asset generation
Princesseuh marked this conversation as resolved.
Show resolved Hide resolved
37 changes: 22 additions & 15 deletions packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ import type {
StylesheetAsset,
} from './types.js';
import { getTimeStat } from './util.js';
import OS from 'node:os';
import pLimit from 'p-limit';

function createEntryURL(filePath: string, outFolder: URL) {
return new URL('./' + filePath + `?time=${Date.now()}`, outFolder);
Expand Down Expand Up @@ -196,21 +198,26 @@ export async function generatePages(opts: StaticBuildOptions, internals: BuildIn
}
}

const cpuCount = OS.cpus().length;
const staticImageList = getStaticImageList();
const limit = pLimit(cpuCount);

if (staticImageList.size)
logger.info(null, `\n${bgGreen(black(` generating optimized images `))}`);
let count = 0;
for (const imageData of staticImageList.entries()) {
count++;
await generateImage(
pipeline,
imageData[1].options,
imageData[1].path,
count,
staticImageList.size
);
}

await Promise.all(
Array.from(staticImageList.entries()).map((imageData, count) =>
limit(() =>
generateImage(
pipeline,
imageData[1].options,
imageData[1].path,
count,
staticImageList.size
)
)
)
);

delete globalThis?.astroAsset?.addStaticImage;

Expand Down Expand Up @@ -414,10 +421,10 @@ function getInvalidRouteSegmentError(
...AstroErrorData.InvalidDynamicRoute,
message: invalidParam
? AstroErrorData.InvalidDynamicRoute.message(
route.route,
JSON.stringify(invalidParam),
JSON.stringify(received)
)
route.route,
JSON.stringify(invalidParam),
JSON.stringify(received)
)
: `Generated path for ${route.route} is invalid.`,
hint,
});
Expand Down
Loading