Repository: withastro/astro
File: packages/astro/src/core/build/generate.ts (TypeScript)

Code before:
import type { BaseApp } from '../app/base.js';
import type { SSRManifest } from '../app/types.js';
import { NoPrerenderedRoutesWithDomains } from '../errors/errors-data.js';
import { AstroError, AstroErrorData } from '../errors/index.js';
import { getRedirectLocationOrThrow, routeIsRedirect } from '../redirects/index.js';
import { callGetStaticPaths } from '../render/route-cache.js';
import { createRequest } from '../request.js';
import { redirectTemplate } from '../routing/3xx.js';
import { matchRoute } from '../routing/match.js';
import { stringifyParams } from '../routing/params.js';
import { getOutputFilename } from '../util.js';
import { getOutFile, getOutFolder } from './common.js';
import { type BuildInternals, hasPrerenderedPages } from './internal.js';
import { BuildPipeline } from './pipeline.js';
import type { SinglePageBuiltModule, StaticBuildOptions } from './types.js';
import { getTimeStat, shouldAppendForwardSlash } from './util.js';

export async function generatePages(
	options: StaticBuildOptions,
	internals: BuildInternals,
	prerenderOutputDir: URL,
) {
	const generatePagesTimer = performance.now();
	const ssr = options.settings.buildOutput === 'server';
	// Import from the single prerender entrypoint
	const prerenderEntryFileName = internals.prerenderEntryFileName ?? 'prerender-entry.mjs';
	const prerenderEntryUrl = new URL(prerenderEntryFileName, prerenderOutputDir);
	const prerenderEntry = await import(prerenderEntryUrl.toString());

	// Grab the manifest and create the pipeline
	const manifest: SSRManifest = prerenderEntry.manifest;
	const pipeline = BuildPipeline.create({ internals, manifest, options });
	const { config, logger } = pipeline;

	// HACK! `astro:assets` relies on a global to know if its running in dev, prod, ssr, ssg, full moon
	// If we don't delete it here, it's technically not impossible (albeit improbable) for it to leak
	if (ssr && !hasPrerenderedPages(internals)) {
		delete globalThis?.astroAsset?.addStaticImage;
	}

	const verb = ssr ? 'prerendering' : 'generating';
	logger.info('SKIP_FORMAT', `\n${bgGreen(black(` ${verb} static routes `))}`);
	const builtPaths = new Set<string>();
	const pagesToGenerate = pipeline.retrieveRoutesToGenerate();
	const routeToHeaders: RouteToHeaders = new Map();

	const app = prerenderEntry.app as BaseApp;
	const pageMap = prerenderEntry.pageMap as Map<string, () => Promise<SinglePageBuiltModule>>;

	if (ssr) {
		for (const [routeData, _] of pagesToGenerate) {

Review comment (suggestion):
I believe we should have `prerender-entry.mjs` in a const and use that one instead. I've seen it in other places 

Code after:
import type { BaseApp } from '../app/base.js';
import type { SSRManifest } from '../app/types.js';
import { NoPrerenderedRoutesWithDomains } from '../errors/errors-data.js';
import { AstroError, AstroErrorData } from '../errors/index.js';
import { getRedirectLocationOrThrow, routeIsRedirect } from '../redirects/index.js';
import { callGetStaticPaths } from '../render/route-cache.js';
import { createRequest } from '../request.js';
import { redirectTemplate } from '../routing/3xx.js';
import { matchRoute } from '../routing/match.js';
import { stringifyParams } from '../routing/params.js';
import { getOutputFilename } from '../util.js';
import { getOutFile, getOutFolder } from './common.js';
import { type BuildInternals, hasPrerenderedPages } from './internal.js';
import { BuildPipeline } from './pipeline.js';
import type { SinglePageBuiltModule, StaticBuildOptions } from './types.js';
import { getTimeStat, shouldAppendForwardSlash } from './util.js';

export async function generatePages(
	options: StaticBuildOptions,
	internals: BuildInternals,
	prerenderOutputDir: URL,
) {
	const generatePagesTimer = performance.now();
	const ssr = options.settings.buildOutput === 'server';
	// Import from the single prerender entrypoint
	const prerenderEntryFileName = internals.prerenderEntryFileName;
	if (!prerenderEntryFileName) {
		throw new Error(
			`Prerender entry filename not found in build internals. This is likely a bug in Astro.`
		);
	}
	const prerenderEntryUrl = new URL(prerenderEntryFileName, prerenderOutputDir);
	const prerenderEntry = await import(prerenderEntryUrl.toString());

	// Grab the manifest and create the pipeline
	const manifest: SSRManifest = prerenderEntry.manifest;
	const pipeline = BuildPipeline.create({ internals, manifest, options });
	const { config, logger } = pipeline;

	// HACK! `astro:assets` relies on a global to know if its running in dev, prod, ssr, ssg, full moon
	// If we don't delete it here, it's technically not impossible (albeit improbable) for it to leak
	if (ssr && !hasPrerenderedPages(internals)) {
		delete globalThis?.astroAsset?.addStaticImage;
	}

	const verb = ssr ? 'prerendering' : 'generating';
	logger.info('SKIP_FORMAT', `\n${bgGreen(black(` ${verb} static routes `))}`);
	const builtPaths = new Set<string>();
	const pagesToGenerate = pipeline.retrieveRoutesToGenerate();
	const routeToHeaders: RouteToHeaders = new Map();