0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

refactor: use only manifest to compute information (#7571)

* refactor: use only manifest to compute information

* use entry modules

* revert entry modules
This commit is contained in:
Emanuele Stoppa 2023-07-05 12:36:19 +01:00 committed by GitHub
parent f820bb3f0b
commit cfd5b2b785
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 44 deletions

View file

@ -37,7 +37,7 @@ export type SSRManifest = {
adapterName: string;
routes: RouteInfo[];
site?: string;
base?: string;
base: string;
compressHTML?: boolean;
assetsPrefix?: string;
markdown: MarkdownRenderingOptions;

View file

@ -155,37 +155,36 @@ export async function generatePages(opts: StaticBuildOptions, internals: BuildIn
const manifest: SSRManifest | undefined = ssrEntryPage.manifest;
const ssrEntry = manifest?.pageModule;
if (ssrEntry) {
await generatePage(opts, internals, pageData, ssrEntry, builtPaths);
await generatePage(opts, internals, pageData, ssrEntry, builtPaths, manifest);
} else {
throw new Error(
`Unable to find the manifest for the module ${ssrEntryURLPage.toString()}. This is unexpected and likely a bug in Astro, please report.`
);
}
} else {
await generatePage(
opts,
internals,
pageData,
ssrEntryPage as SinglePageBuiltModule,
builtPaths
);
const ssrEntry = ssrEntryPage as SinglePageBuiltModule;
const manifest = generateRuntimeManifest(opts.settings, internals, ssrEntry.renderers);
await generatePage(opts, internals, pageData, ssrEntry, builtPaths, manifest);
}
}
}
for (const pageData of eachRedirectPageData(internals)) {
const entry = await getEntryForRedirectRoute(pageData.route, internals, outFolder);
await generatePage(opts, internals, pageData, entry, builtPaths);
const manifest = generateRuntimeManifest(opts.settings, internals, entry.renderers);
await generatePage(opts, internals, pageData, entry, builtPaths, manifest);
}
} else {
for (const [pageData, filePath] of eachPageDataFromEntryPoint(internals)) {
const ssrEntryURLPage = createEntryURL(filePath, outFolder);
const ssrEntryPage: SinglePageBuiltModule = await import(ssrEntryURLPage.toString());
const entry: SinglePageBuiltModule = await import(ssrEntryURLPage.toString());
const manifest = generateRuntimeManifest(opts.settings, internals, entry.renderers);
await generatePage(opts, internals, pageData, ssrEntryPage, builtPaths);
await generatePage(opts, internals, pageData, entry, builtPaths, manifest);
}
for (const pageData of eachRedirectPageData(internals)) {
const entry = await getEntryForRedirectRoute(pageData.route, internals, outFolder);
await generatePage(opts, internals, pageData, entry, builtPaths);
const manifest = generateRuntimeManifest(opts.settings, internals, entry.renderers);
await generatePage(opts, internals, pageData, entry, builtPaths, manifest);
}
}
@ -228,14 +227,14 @@ async function generatePage(
internals: BuildInternals,
pageData: PageBuildData,
ssrEntry: SinglePageBuiltModule,
builtPaths: Set<string>
builtPaths: Set<string>,
manifest: SSRManifest
) {
if (routeIsRedirect(pageData.route) && !opts.settings.config.experimental.redirects) {
throw new Error(`To use redirects first set experimental.redirects to \`true\``);
}
let timeStart = performance.now();
const renderers = ssrEntry.renderers;
const pageInfo = getPageDataByComponent(internals, pageData.route.component);
@ -268,7 +267,6 @@ async function generatePage(
scripts,
styles,
mod: pageModule,
renderers,
};
const icon = pageData.route.type === 'page' ? green('▶') : magenta('λ');
@ -283,7 +281,7 @@ async function generatePage(
for (let i = 0; i < paths.length; i++) {
const path = paths[i];
await generatePath(path, opts, generationOptions, onRequest);
await generatePath(path, opts, generationOptions, manifest, onRequest);
const timeEnd = performance.now();
const timeChange = getTimeStat(timeStart, timeEnd);
const timeIncrease = `(+${timeChange})`;
@ -398,7 +396,6 @@ interface GeneratePathOptions {
scripts: { type: 'inline' | 'external'; value: string } | null;
styles: StylesheetAsset[];
mod: ComponentInstance;
renderers: SSRLoadedRenderer[];
}
function shouldAppendForwardSlash(
@ -462,10 +459,11 @@ async function generatePath(
pathname: string,
opts: StaticBuildOptions,
gopts: GeneratePathOptions,
manifest: SSRManifest,
onRequest?: MiddlewareHandler<unknown>
) {
const { settings, logging, origin, routeCache } = opts;
const { mod, internals, scripts: hoistedScripts, styles: _styles, pageData, renderers } = gopts;
const { mod, internals, scripts: hoistedScripts, styles: _styles, pageData } = gopts;
// This adds the page name to the array so it can be shown as part of stats.
if (pageData.route.type === 'page') {
@ -478,25 +476,17 @@ async function generatePath(
const links = new Set<never>();
const scripts = createModuleScriptsSet(
hoistedScripts ? [hoistedScripts] : [],
settings.config.base,
settings.config.build.assetsPrefix
);
const styles = createStylesheetElementSet(
_styles,
settings.config.base,
settings.config.build.assetsPrefix
manifest.base,
manifest.assetsPrefix
);
const styles = createStylesheetElementSet(_styles, manifest.base, manifest.assetsPrefix);
if (settings.scripts.some((script) => script.stage === 'page')) {
const hashedFilePath = internals.entrySpecifierToBundleMap.get(PAGE_SCRIPT_ID);
if (typeof hashedFilePath !== 'string') {
throw new Error(`Cannot find the built path for ${PAGE_SCRIPT_ID}`);
}
const src = createAssetLink(
hashedFilePath,
settings.config.base,
settings.config.build.assetsPrefix
);
const src = createAssetLink(hashedFilePath, manifest.base, manifest.assetsPrefix);
scripts.add({
props: { type: 'module', src },
children: '',
@ -521,14 +511,16 @@ async function generatePath(
opts.settings.config.build.format,
pageData.route.type
);
const env = createEnvironment({
adapterName: undefined,
adapterName: manifest.adapterName,
logging,
markdown: settings.config.markdown,
markdown: manifest.markdown,
mode: opts.mode,
renderers,
clientDirectives: settings.clientDirectives,
renderers: manifest.renderers,
clientDirectives: manifest.clientDirectives,
async resolve(specifier: string) {
// NOTE: next PR, borrow logic from build manifest maybe?
const hashedFilePath = internals.entrySpecifierToBundleMap.get(specifier);
if (typeof hashedFilePath !== 'string') {
// If no "astro:scripts/before-hydration.js" script exists in the build,
@ -538,16 +530,10 @@ async function generatePath(
}
throw new Error(`Cannot find the built path for ${specifier}`);
}
return createAssetLink(
hashedFilePath,
settings.config.base,
settings.config.build.assetsPrefix
);
return createAssetLink(hashedFilePath, manifest.base, manifest.assetsPrefix);
},
routeCache,
site: settings.config.site
? new URL(settings.config.base, settings.config.site).toString()
: settings.config.site,
site: manifest.site,
ssr,
streaming: true,
});
@ -556,7 +542,7 @@ async function generatePath(
origin,
pathname,
request: createRequest({ url, headers: new Headers(), logging, ssr }),
componentMetadata: internals.componentMetadata,
componentMetadata: manifest.componentMetadata,
scripts,
styles,
links,
@ -656,3 +642,32 @@ async function generatePath(
await fs.promises.mkdir(outFolder, { recursive: true });
await fs.promises.writeFile(outFile, body, encoding ?? 'utf-8');
}
/**
* It creates a `SSRManifest` from the `AstroSettings`.
*
* Renderers needs to be pulled out from the page module emitted during the build.
* @param settings
* @param renderers
*/
export function generateRuntimeManifest(
settings: AstroSettings,
internals: BuildInternals,
renderers: SSRLoadedRenderer[]
): SSRManifest {
return {
assets: new Set(),
entryModules: {},
routes: [],
adapterName: '',
markdown: settings.config.markdown,
clientDirectives: settings.clientDirectives,
renderers,
base: settings.config.base,
assetsPrefix: settings.config.build.assetsPrefix,
site: settings.config.site
? new URL(settings.config.base, settings.config.site).toString()
: settings.config.site,
componentMetadata: internals.componentMetadata,
};
}