0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-04-07 23:41:43 -05:00

utils: add makePageDataKey

This commit is contained in:
Goulven Clec'h 2024-04-22 20:02:42 +02:00
parent 57137fd5a8
commit 73ba1518c6
3 changed files with 20 additions and 9 deletions
packages/astro/src/core/build

View file

@ -4,6 +4,7 @@ import type { AllPagesData } from './types.js';
import * as colors from 'kleur/colors';
import { debug } from '../logger/core.js';
import { makePageDataKey } from './plugins/util.js';
export interface CollectPagesDataOptions {
settings: AstroSettings;
@ -36,7 +37,7 @@ export async function collectPagesData(
// with parallelized builds without guaranteeing that this is called first.
for (const route of manifest.routes) {
// Generate a unique key to identify each page in the build process.
const key = `${route.route}&${route.component}`;
const key = makePageDataKey(route.route, route.component);
// static route:
if (route.pathname) {
const routeCollectionLogTimeout = setInterval(() => {

View file

@ -19,6 +19,7 @@ import { getOutFile, getOutFolder } from '../common.js';
import { type BuildInternals, cssOrder, mergeInlineCss } from '../internal.js';
import type { AstroBuildPlugin } from '../plugin.js';
import type { StaticBuildOptions } from '../types.js';
import { makePageDataKey } from './util.js';
const manifestReplace = '@@ASTRO_MANIFEST_REPLACE@@';
const replaceExp = new RegExp(`['"]${manifestReplace}['"]`, 'g');
@ -189,10 +190,7 @@ function buildManifest(
}
for (const route of opts.manifest.routes) {
let pageData;
for (const page of internals.pagesByKeys.values()) {
if (page.route.route == route.route) pageData = page;
}
const pageData = internals.pagesByKeys.get(makePageDataKey(route.route, route.component));
if (route.prerender || !pageData) continue;
const scripts: SerializedRouteInfo['scripts'] = [];
if (pageData.hoistedScript) {

View file

@ -40,8 +40,20 @@ export function extendManualChunks(outputOptions: OutputOptions, hooks: ExtendMa
};
}
// This is an arbitrary string that we are going to replace the dot of the extension
// This is an arbitrary string that we use to replace the dot of the extension.
export const ASTRO_PAGE_EXTENSION_POST_PATTERN = '@_@';
// This is an arbitrary string that we use to make a pageData key
// Has to be a invalid character for a route, to avoid conflicts.
export const ASTRO_PAGE_KEY_SEPARATOR = '&';
/**
* Generate a unique key to identify each page in the build process.
* @param route Usually pageData.route.route
* @param componentPath Usually pageData.component
*/
export function makePageDataKey(route: string, componentPath: string) {
return route + ASTRO_PAGE_KEY_SEPARATOR + componentPath;
}
/**
* Prevents Rollup from triggering other plugins in the process by masking the extension (hence the virtual file).
@ -53,7 +65,7 @@ export const ASTRO_PAGE_EXTENSION_POST_PATTERN = '@_@';
export function getVirtualModulePageName(virtualModulePrefix: string, path: string, route: string) {
const extension = extname(path);
return (
virtualModulePrefix + route + '&' +
virtualModulePrefix + route + ASTRO_PAGE_KEY_SEPARATOR +
(extension.startsWith('.')
? path.slice(0, -extension.length) + extension.replace('.', ASTRO_PAGE_EXTENSION_POST_PATTERN)
: path)
@ -68,10 +80,10 @@ export function getVirtualModulePageName(virtualModulePrefix: string, path: stri
export function getPageKeyFromVirtualModulePageName(virtualModulePrefix: string, id: string) {
const [route, path] = id
.slice(virtualModulePrefix.length)
.split("&");
.split(ASTRO_PAGE_KEY_SEPARATOR);
const componentPath = path.replace(ASTRO_PAGE_EXTENSION_POST_PATTERN, '.');
return `${route}&${componentPath}`;
return makePageDataKey(route, componentPath);
}
// TODO: Should this be removed? Or refactored in generate.ts ?