mirror of
https://github.com/withastro/astro.git
synced 2024-12-23 21:53:55 -05:00
[ci] format
This commit is contained in:
parent
23fceb93ac
commit
d32f6723a0
3 changed files with 60 additions and 35 deletions
|
@ -712,8 +712,8 @@ export type InjectedScriptStage = 'before-hydration' | 'head-inline' | 'page' |
|
|||
*/
|
||||
|
||||
export interface InjectedRoute {
|
||||
pattern: string,
|
||||
entryPoint: string
|
||||
pattern: string;
|
||||
entryPoint: string;
|
||||
}
|
||||
export interface AstroConfig extends z.output<typeof AstroConfigSchema> {
|
||||
// Public:
|
||||
|
@ -726,7 +726,7 @@ export interface AstroConfig extends z.output<typeof AstroConfigSchema> {
|
|||
// that is different from the user-exposed configuration.
|
||||
// TODO: Create an AstroConfig class to manage this, long-term.
|
||||
_ctx: {
|
||||
injectedRoutes: InjectedRoute[],
|
||||
injectedRoutes: InjectedRoute[];
|
||||
adapter: AstroAdapter | undefined;
|
||||
renderers: AstroRenderer[];
|
||||
scripts: { stage: InjectedScriptStage; content: string }[];
|
||||
|
@ -973,7 +973,7 @@ export interface RoutePart {
|
|||
}
|
||||
|
||||
export interface RouteData {
|
||||
route: string,
|
||||
route: string;
|
||||
component: string;
|
||||
generate: (data?: any) => string;
|
||||
params: string[];
|
||||
|
|
|
@ -55,7 +55,7 @@ class AstroBuilder {
|
|||
this.origin = config.site
|
||||
? new URL(config.site).origin
|
||||
: `http://localhost:${config.server.port}`;
|
||||
this.manifest = {routes: []};
|
||||
this.manifest = { routes: [] };
|
||||
this.timer = {};
|
||||
}
|
||||
|
||||
|
|
|
@ -2,9 +2,9 @@ import type { AstroConfig, ManifestData, RouteData, RoutePart } from '../../../@
|
|||
import type { LogOptions } from '../../logger/core';
|
||||
|
||||
import fs from 'fs';
|
||||
import { createRequire } from 'module';
|
||||
import path from 'path';
|
||||
import slash from 'slash';
|
||||
import { createRequire } from 'module';
|
||||
import { fileURLToPath } from 'url';
|
||||
import { warn } from '../../logger/core.js';
|
||||
import { resolvePages } from '../../util.js';
|
||||
|
@ -96,15 +96,17 @@ function isSpread(str: string) {
|
|||
}
|
||||
|
||||
function validateSegment(segment: string, file = '') {
|
||||
if(!file) file = segment;
|
||||
if (!file) file = segment;
|
||||
|
||||
if (/^\$/.test(segment)) {
|
||||
throw new Error(`Invalid route ${file} \u2014 Astro's Collections API has been replaced by dynamic route params.`);
|
||||
throw new Error(
|
||||
`Invalid route ${file} \u2014 Astro's Collections API has been replaced by dynamic route params.`
|
||||
);
|
||||
}
|
||||
if (/\]\[/.test(segment)) {
|
||||
throw new Error(`Invalid route ${file} \u2014 parameters must be separated`);
|
||||
}
|
||||
if (countOccurrences("[", segment) !== countOccurrences("]", segment)) {
|
||||
if (countOccurrences('[', segment) !== countOccurrences(']', segment)) {
|
||||
throw new Error(`Invalid route ${file} \u2014 brackets are unbalanced`);
|
||||
}
|
||||
if (/.+\[\.\.\.[^\]]+\]/.test(segment) || /\[\.\.\.[^\]]+\].+/.test(segment)) {
|
||||
|
@ -253,7 +255,9 @@ export function createRouteManifest(
|
|||
const pathname = segments.every((segment) => segment.length === 1 && !segment[0].dynamic)
|
||||
? `/${segments.map((segment) => segment[0].content).join('/')}`
|
||||
: null;
|
||||
const route = `/${segments.map(([{dynamic, content}]) => dynamic ? `[${content}]` : content).join('/')}`.toLowerCase();
|
||||
const route = `/${segments
|
||||
.map(([{ dynamic, content }]) => (dynamic ? `[${content}]` : content))
|
||||
.join('/')}`.toLowerCase();
|
||||
|
||||
routes.push({
|
||||
route,
|
||||
|
@ -279,43 +283,64 @@ export function createRouteManifest(
|
|||
warn(logging, 'astro', `Missing pages directory: ${pagesDirRootRelative}`);
|
||||
}
|
||||
|
||||
config?._ctx?.injectedRoutes?.forEach(({pattern: name, entryPoint}) => {
|
||||
config?._ctx?.injectedRoutes?.forEach(({ pattern: name, entryPoint }) => {
|
||||
const resolved = require.resolve(entryPoint, { paths: [cwd || fileURLToPath(config.root)] });
|
||||
const component = slash(path.relative(cwd || fileURLToPath(config.root), resolved));
|
||||
|
||||
const isDynamic = (str: string) => str?.[0] === '[';
|
||||
const normalize = (str: string) => str?.substring(1, str?.length - 1);
|
||||
|
||||
const segments = name.split(path.sep)
|
||||
const segments = name
|
||||
.split(path.sep)
|
||||
.filter(Boolean)
|
||||
.map((s: string) => {
|
||||
validateSegment(s);
|
||||
|
||||
const dynamic = isDynamic(s);
|
||||
const content = dynamic ? normalize(s) : s;
|
||||
return [{
|
||||
return [
|
||||
{
|
||||
content,
|
||||
dynamic,
|
||||
spread: isSpread(s)
|
||||
}]
|
||||
spread: isSpread(s),
|
||||
},
|
||||
];
|
||||
});
|
||||
|
||||
const type = resolved.endsWith('.astro') ? 'page' : 'endpoint';
|
||||
const isPage = type === 'page';
|
||||
const trailingSlash = isPage ? config.trailingSlash : "never";
|
||||
const trailingSlash = isPage ? config.trailingSlash : 'never';
|
||||
|
||||
const pattern = getPattern(segments, trailingSlash);
|
||||
const generate = getRouteGenerator(segments, trailingSlash);
|
||||
const pathname = segments.every((segment) => segment.length === 1 && !segment[0].dynamic) ? `/${segments.map((segment) => segment[0].content).join("/")}` : null;
|
||||
const params = segments.flat().filter((p) => p.dynamic).map((p) => p.content);
|
||||
const route = `/${segments.map(([{dynamic, content}]) => dynamic ? `[${content}]` : content).join('/')}`.toLowerCase();
|
||||
const pathname = segments.every((segment) => segment.length === 1 && !segment[0].dynamic)
|
||||
? `/${segments.map((segment) => segment[0].content).join('/')}`
|
||||
: null;
|
||||
const params = segments
|
||||
.flat()
|
||||
.filter((p) => p.dynamic)
|
||||
.map((p) => p.content);
|
||||
const route = `/${segments
|
||||
.map(([{ dynamic, content }]) => (dynamic ? `[${content}]` : content))
|
||||
.join('/')}`.toLowerCase();
|
||||
|
||||
const collision = routes.find(({route: r}) => r === route);
|
||||
if(collision) {
|
||||
throw new Error(`An integration attempted to inject a route that is already used in your project: "${route}" at "${component}". \nThis route collides with: "${collision.component}".`);
|
||||
const collision = routes.find(({ route: r }) => r === route);
|
||||
if (collision) {
|
||||
throw new Error(
|
||||
`An integration attempted to inject a route that is already used in your project: "${route}" at "${component}". \nThis route collides with: "${collision.component}".`
|
||||
);
|
||||
}
|
||||
|
||||
routes.push({type, route, pattern, segments, params, component, generate, pathname: pathname || void 0})
|
||||
routes.push({
|
||||
type,
|
||||
route,
|
||||
pattern,
|
||||
segments,
|
||||
params,
|
||||
component,
|
||||
generate,
|
||||
pathname: pathname || void 0,
|
||||
});
|
||||
});
|
||||
|
||||
return {
|
||||
|
|
Loading…
Reference in a new issue