Repository: withastro/astro
File: packages/astro/src/core/build/plugins/plugin-ssr.ts (TypeScript)

Code before:
					enforce: 'after-user-plugins',
					vitePlugin: vitePlugin,
				};
			},
			'build:post': async () => {
				if (!ssr) {
					return;
				}

				if (!internals.ssrEntryChunk) {
					throw new Error(`Did not generate an entry chunk for SSR`);
				}
				// Mutate the filename
				internals.ssrEntryChunk.fileName = options.settings.config.build.serverEntry;
			},
		},
	};
}

function generateSSRCode(adapter: AstroAdapter) {
	const edgeMiddleware = adapter?.adapterFeatures?.edgeMiddleware ?? false;

	const imports = [
		`import { renderers } from '${ASTRO_RENDERERS_MODULE_ID}';`,
		`import * as serverEntrypointModule from '${ADAPTER_VIRTUAL_MODULE_ID}';`,
		`import { manifest as defaultManifest } from 'virtual:astro:serialized-manifest';`,
		`import { serverIslandMap } from '${VIRTUAL_ISLAND_MAP_ID}';`,
	];

	const contents = [
		edgeMiddleware ? `const middleware = (_, next) => next()` : '',
		`const _manifest = Object.assign(defaultManifest, {`,
		`    pageMap,`,
		`    serverIslandMap,`,
		`    middleware: ${edgeMiddleware ? 'undefined' : `() => import("${MIDDLEWARE_MODULE_ID}")`}`,
		`});`,
		`const _args = ${adapter.args ? JSON.stringify(adapter.args, null, 4) : 'undefined'};`,
		adapter.exports
			? `const _exports = serverEntrypointModule.createExports?.(_manifest, _args) || serverEntrypointModule;`
			: '',
		...(adapter.exports?.map((name) => {
			if (name === 'default') {
				return `export default _exports.default;`;
			} else {
				return `export const ${name} = _exports['${name}'];`;
			}
		}) ?? []),
		// NOTE: This is intentionally obfuscated!
		// Do NOT simplify this to something like `serverEntrypointModule.start?.(_manifest, _args)`
		// They are NOT equivalent! Some bundlers will throw if `start` is not exported, but we
		// only want to silently ignore it... hence the dynamic, obfuscated weirdness.

Review comment (suggestion):
Please let's use the const to reduce errors. We can import it from `manifest/serialized.js`

Code after:
				}

				return {
					enforce: 'after-user-plugins',
					vitePlugin: vitePlugin,
				};
			},
			'build:post': async () => {
				if (!ssr) {
					return;
				}

				if (!internals.ssrEntryChunk) {
					throw new Error(`Did not generate an entry chunk for SSR`);
				}
				// Mutate the filename
				internals.ssrEntryChunk.fileName = options.settings.config.build.serverEntry;
			},
		},
	};
}

function generateSSRCode(adapter: AstroAdapter) {
	const edgeMiddleware = adapter?.adapterFeatures?.edgeMiddleware ?? false;

	const imports = [
		`import { renderers } from '${ASTRO_RENDERERS_MODULE_ID}';`,
		`import * as serverEntrypointModule from '${ADAPTER_VIRTUAL_MODULE_ID}';`,
		`import { manifest as defaultManifest } from '${SERIALIZED_MANIFEST_ID}';`,
		`import { serverIslandMap } from '${VIRTUAL_ISLAND_MAP_ID}';`,
	];

	const contents = [
		edgeMiddleware ? `const middleware = (_, next) => next()` : '',
		`const _manifest = Object.assign(defaultManifest, {`,
		`    pageMap,`,
		`    serverIslandMap,`,
		`    middleware: ${edgeMiddleware ? 'undefined' : `() => import("${MIDDLEWARE_MODULE_ID}")`}`,
		`});`,
		`const _args = ${adapter.args ? JSON.stringify(adapter.args, null, 4) : 'undefined'};`,
		adapter.exports
			? `const _exports = serverEntrypointModule.createExports?.(_manifest, _args) || serverEntrypointModule;`
			: '',
		...(adapter.exports?.map((name) => {
			if (name === 'default') {
				return `export default _exports.default;`;
			} else {
				return `export const ${name} = _exports['${name}'];`;
			}
		}) ?? []),
		// NOTE: This is intentionally obfuscated!