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 {
|
export interface InjectedRoute {
|
||||||
pattern: string,
|
pattern: string;
|
||||||
entryPoint: string
|
entryPoint: string;
|
||||||
}
|
}
|
||||||
export interface AstroConfig extends z.output<typeof AstroConfigSchema> {
|
export interface AstroConfig extends z.output<typeof AstroConfigSchema> {
|
||||||
// Public:
|
// Public:
|
||||||
|
@ -726,7 +726,7 @@ export interface AstroConfig extends z.output<typeof AstroConfigSchema> {
|
||||||
// that is different from the user-exposed configuration.
|
// that is different from the user-exposed configuration.
|
||||||
// TODO: Create an AstroConfig class to manage this, long-term.
|
// TODO: Create an AstroConfig class to manage this, long-term.
|
||||||
_ctx: {
|
_ctx: {
|
||||||
injectedRoutes: InjectedRoute[],
|
injectedRoutes: InjectedRoute[];
|
||||||
adapter: AstroAdapter | undefined;
|
adapter: AstroAdapter | undefined;
|
||||||
renderers: AstroRenderer[];
|
renderers: AstroRenderer[];
|
||||||
scripts: { stage: InjectedScriptStage; content: string }[];
|
scripts: { stage: InjectedScriptStage; content: string }[];
|
||||||
|
@ -973,7 +973,7 @@ export interface RoutePart {
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface RouteData {
|
export interface RouteData {
|
||||||
route: string,
|
route: string;
|
||||||
component: string;
|
component: string;
|
||||||
generate: (data?: any) => string;
|
generate: (data?: any) => string;
|
||||||
params: string[];
|
params: string[];
|
||||||
|
|
|
@ -55,7 +55,7 @@ class AstroBuilder {
|
||||||
this.origin = config.site
|
this.origin = config.site
|
||||||
? new URL(config.site).origin
|
? new URL(config.site).origin
|
||||||
: `http://localhost:${config.server.port}`;
|
: `http://localhost:${config.server.port}`;
|
||||||
this.manifest = {routes: []};
|
this.manifest = { routes: [] };
|
||||||
this.timer = {};
|
this.timer = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,9 +2,9 @@ import type { AstroConfig, ManifestData, RouteData, RoutePart } from '../../../@
|
||||||
import type { LogOptions } from '../../logger/core';
|
import type { LogOptions } from '../../logger/core';
|
||||||
|
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
|
import { createRequire } from 'module';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import slash from 'slash';
|
import slash from 'slash';
|
||||||
import { createRequire } from 'module';
|
|
||||||
import { fileURLToPath } from 'url';
|
import { fileURLToPath } from 'url';
|
||||||
import { warn } from '../../logger/core.js';
|
import { warn } from '../../logger/core.js';
|
||||||
import { resolvePages } from '../../util.js';
|
import { resolvePages } from '../../util.js';
|
||||||
|
@ -96,15 +96,17 @@ function isSpread(str: string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function validateSegment(segment: string, file = '') {
|
function validateSegment(segment: string, file = '') {
|
||||||
if(!file) file = segment;
|
if (!file) file = segment;
|
||||||
|
|
||||||
if (/^\$/.test(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)) {
|
if (/\]\[/.test(segment)) {
|
||||||
throw new Error(`Invalid route ${file} \u2014 parameters must be separated`);
|
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`);
|
throw new Error(`Invalid route ${file} \u2014 brackets are unbalanced`);
|
||||||
}
|
}
|
||||||
if (/.+\[\.\.\.[^\]]+\]/.test(segment) || /\[\.\.\.[^\]]+\].+/.test(segment)) {
|
if (/.+\[\.\.\.[^\]]+\]/.test(segment) || /\[\.\.\.[^\]]+\].+/.test(segment)) {
|
||||||
|
@ -253,7 +255,9 @@ export function createRouteManifest(
|
||||||
const pathname = segments.every((segment) => segment.length === 1 && !segment[0].dynamic)
|
const pathname = segments.every((segment) => segment.length === 1 && !segment[0].dynamic)
|
||||||
? `/${segments.map((segment) => segment[0].content).join('/')}`
|
? `/${segments.map((segment) => segment[0].content).join('/')}`
|
||||||
: null;
|
: 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({
|
routes.push({
|
||||||
route,
|
route,
|
||||||
|
@ -279,43 +283,64 @@ export function createRouteManifest(
|
||||||
warn(logging, 'astro', `Missing pages directory: ${pagesDirRootRelative}`);
|
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 resolved = require.resolve(entryPoint, { paths: [cwd || fileURLToPath(config.root)] });
|
||||||
const component = slash(path.relative(cwd || fileURLToPath(config.root), resolved));
|
const component = slash(path.relative(cwd || fileURLToPath(config.root), resolved));
|
||||||
|
|
||||||
const isDynamic = (str: string) => str?.[0] === '[';
|
const isDynamic = (str: string) => str?.[0] === '[';
|
||||||
const normalize = (str: string) => str?.substring(1, str?.length - 1);
|
const normalize = (str: string) => str?.substring(1, str?.length - 1);
|
||||||
|
|
||||||
const segments = name.split(path.sep)
|
const segments = name
|
||||||
|
.split(path.sep)
|
||||||
.filter(Boolean)
|
.filter(Boolean)
|
||||||
.map((s: string) => {
|
.map((s: string) => {
|
||||||
validateSegment(s);
|
validateSegment(s);
|
||||||
|
|
||||||
const dynamic = isDynamic(s);
|
const dynamic = isDynamic(s);
|
||||||
const content = dynamic ? normalize(s) : s;
|
const content = dynamic ? normalize(s) : s;
|
||||||
return [{
|
return [
|
||||||
|
{
|
||||||
content,
|
content,
|
||||||
dynamic,
|
dynamic,
|
||||||
spread: isSpread(s)
|
spread: isSpread(s),
|
||||||
}]
|
},
|
||||||
|
];
|
||||||
});
|
});
|
||||||
|
|
||||||
const type = resolved.endsWith('.astro') ? 'page' : 'endpoint';
|
const type = resolved.endsWith('.astro') ? 'page' : 'endpoint';
|
||||||
const isPage = type === 'page';
|
const isPage = type === 'page';
|
||||||
const trailingSlash = isPage ? config.trailingSlash : "never";
|
const trailingSlash = isPage ? config.trailingSlash : 'never';
|
||||||
|
|
||||||
const pattern = getPattern(segments, trailingSlash);
|
const pattern = getPattern(segments, trailingSlash);
|
||||||
const generate = getRouteGenerator(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 pathname = segments.every((segment) => segment.length === 1 && !segment[0].dynamic)
|
||||||
const params = segments.flat().filter((p) => p.dynamic).map((p) => p.content);
|
? `/${segments.map((segment) => segment[0].content).join('/')}`
|
||||||
const route = `/${segments.map(([{dynamic, content}]) => dynamic ? `[${content}]` : content).join('/')}`.toLowerCase();
|
: 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);
|
const collision = routes.find(({ route: r }) => r === route);
|
||||||
if(collision) {
|
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}".`);
|
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 {
|
return {
|
||||||
|
|
Loading…
Reference in a new issue