Skip to content

Commit

Permalink
feat: add progress to asset generation (#8357)
Browse files Browse the repository at this point in the history
  • Loading branch information
itsmatteomanf authored Sep 1, 2023
1 parent b74dacd commit 6b1e798
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
6 changes: 6 additions & 0 deletions .changeset/polite-scissors-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'astro': patch
---

Added counter to show progress for assets image generation.
Fixed small unit of measurement error.
8 changes: 4 additions & 4 deletions packages/astro/src/assets/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,12 @@ export async function generateImage(
};
}

export function getStaticImageList(): Iterable<
[string, { path: string; options: ImageTransform }]
export function getStaticImageList(): Map<
string, { path: string; options: ImageTransform }
> {
if (!globalThis?.astroAsset?.staticImages) {
return [];
return new Map();
}

return globalThis.astroAsset.staticImages?.entries();
return globalThis.astroAsset.staticImages;
}
24 changes: 14 additions & 10 deletions packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,12 @@ export async function generatePages(opts: StaticBuildOptions, internals: BuildIn
}
}

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

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);
}

delete globalThis?.astroAsset?.addStaticImage;
Expand All @@ -211,7 +214,7 @@ export async function generatePages(opts: StaticBuildOptions, internals: BuildIn
logger.info(null, dim(`Completed in ${getTimeStat(timer, performance.now())}.\n`));
}

async function generateImage(pipeline: BuildPipeline, transform: ImageTransform, path: string) {
async function generateImage(pipeline: BuildPipeline, transform: ImageTransform, path: string, count: number, totalCount: number) {
const logger = pipeline.getLogger();
let timeStart = performance.now();
const generationData = await generateImageInternal(pipeline, transform, path);
Expand All @@ -225,8 +228,9 @@ async function generateImage(pipeline: BuildPipeline, transform: ImageTransform,
const timeIncrease = `(+${timeChange})`;
const statsText = generationData.cached
? `(reused cache entry)`
: `(before: ${generationData.weight.before}kb, after: ${generationData.weight.after}kb)`;
logger.info(null, ` ${green('▶')} ${path} ${dim(statsText)} ${dim(timeIncrease)}`);
: `(before: ${generationData.weight.before}kB, after: ${generationData.weight.after}kB)`;
const counter = `(${count}/${totalCount})`;
logger.info(null, ` ${green('▶')} ${path} ${dim(statsText)} ${dim(timeIncrease)} ${dim(counter)}}`);
}

async function generatePage(
Expand Down Expand Up @@ -390,10 +394,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

0 comments on commit 6b1e798

Please sign in to comment.