prunelle 0.0.0-stage → 0.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/llms.txt +245 -0
- package/package.json +62 -4
- package/src/a11y/declaration.ts +373 -0
- package/src/a11y/index.ts +477 -0
- package/src/a11y/rgaa-map.ts +230 -0
- package/src/ambient.d.ts +6 -0
- package/src/attrs.ts +43 -0
- package/src/build/bundle.ts +661 -0
- package/src/build/byte-size.ts +11 -0
- package/src/build/carry.ts +57 -0
- package/src/build/client-entries.ts +107 -0
- package/src/build/client-plugin.ts +175 -0
- package/src/build/client-prune.ts +168 -0
- package/src/build/compile.ts +130 -0
- package/src/build/ecoindex.ts +238 -0
- package/src/build/files.ts +171 -0
- package/src/build/first-load.ts +235 -0
- package/src/build/graph.ts +237 -0
- package/src/build/index.ts +678 -0
- package/src/build/is-inside.ts +12 -0
- package/src/build/island-promote.ts +311 -0
- package/src/build/islands.ts +452 -0
- package/src/build/manifest.ts +204 -0
- package/src/build/output.ts +81 -0
- package/src/build/plugin-host.ts +294 -0
- package/src/build/precompress.ts +139 -0
- package/src/build/prerender.ts +288 -0
- package/src/build/reach.ts +200 -0
- package/src/build/render-rules.ts +205 -0
- package/src/build/report.ts +290 -0
- package/src/build/resolve.ts +52 -0
- package/src/build/routes.ts +342 -0
- package/src/build/server-entry.ts +38 -0
- package/src/build/static-headers.ts +112 -0
- package/src/build/static.ts +209 -0
- package/src/build/status-pages.ts +127 -0
- package/src/build/style-bundles.ts +526 -0
- package/src/cli/check-forms.ts +636 -0
- package/src/cli/check-imports.ts +31 -0
- package/src/cli/check-links.ts +632 -0
- package/src/cli/check-security.ts +64 -0
- package/src/cli/check.ts +409 -0
- package/src/cli/commands.ts +372 -0
- package/src/cli/docs.ts +403 -0
- package/src/cli/index.ts +70 -0
- package/src/cli/inspect.ts +406 -0
- package/src/cli/mcp-tools.ts +492 -0
- package/src/cli/mcp.ts +205 -0
- package/src/cli/new.ts +52 -0
- package/src/cli/render.ts +416 -0
- package/src/cli/upgrade-names.ts +209 -0
- package/src/cli/upgrade.ts +367 -0
- package/src/compiler/analyze.ts +651 -0
- package/src/compiler/ast.ts +82 -0
- package/src/compiler/browser-globals.ts +250 -0
- package/src/compiler/cacheability-private.ts +566 -0
- package/src/compiler/cacheability.ts +335 -0
- package/src/compiler/compliance-eco.ts +71 -0
- package/src/compiler/compliance-rgaa.ts +614 -0
- package/src/compiler/compliance-urls.ts +148 -0
- package/src/compiler/compliance.ts +653 -0
- package/src/compiler/data-keys.ts +302 -0
- package/src/compiler/dev-source.ts +87 -0
- package/src/compiler/dom-attrs.ts +452 -0
- package/src/compiler/dom.ts +641 -0
- package/src/compiler/emit.ts +131 -0
- package/src/compiler/expr-context.ts +587 -0
- package/src/compiler/foreign-imports.ts +88 -0
- package/src/compiler/index.ts +8 -0
- package/src/compiler/interactivity.ts +366 -0
- package/src/compiler/mistakes.ts +520 -0
- package/src/compiler/node-env.ts +50 -0
- package/src/compiler/patch.ts +97 -0
- package/src/compiler/props-body.ts +60 -0
- package/src/compiler/props.ts +511 -0
- package/src/compiler/scope.ts +635 -0
- package/src/compiler/server-only.ts +365 -0
- package/src/compiler/shared.ts +397 -0
- package/src/compiler/signal-reads.ts +246 -0
- package/src/compiler/slots.ts +227 -0
- package/src/compiler/spread-handlers.ts +295 -0
- package/src/compiler/ssr-helpers.ts +555 -0
- package/src/compiler/ssr-island.ts +90 -0
- package/src/compiler/ssr-prunelle-tags.ts +69 -0
- package/src/compiler/ssr.ts +525 -0
- package/src/context.ts +199 -0
- package/src/dev/child.ts +30 -0
- package/src/dev/error-log.ts +177 -0
- package/src/dev/host-check.ts +32 -0
- package/src/dev/index.ts +268 -0
- package/src/dev/modules.ts +68 -0
- package/src/dev/open-editor.ts +228 -0
- package/src/dev/protocol.ts +24 -0
- package/src/dev/queue.ts +79 -0
- package/src/dev/reload.ts +159 -0
- package/src/dev/supervisor.ts +212 -0
- package/src/dev/watchers.ts +276 -0
- package/src/dom/action.ts +187 -0
- package/src/dom/attrs.ts +84 -0
- package/src/dom/claim.ts +120 -0
- package/src/dom/extras.ts +579 -0
- package/src/dom/index.ts +329 -0
- package/src/dom/island.ts +318 -0
- package/src/dom/loop.ts +484 -0
- package/src/dom/revive.ts +36 -0
- package/src/eco/declaration.ts +328 -0
- package/src/eco/index.ts +69 -0
- package/src/index.ts +22 -0
- package/src/jsx-runtime.ts +566 -0
- package/src/plugins/config.ts +92 -0
- package/src/plugins/context.ts +109 -0
- package/src/plugins/index.ts +14 -0
- package/src/plugins/load.ts +90 -0
- package/src/plugins/order.ts +148 -0
- package/src/plugins/public.ts +15 -0
- package/src/plugins/requires.ts +47 -0
- package/src/plugins/services.ts +119 -0
- package/src/plugins/types.ts +278 -0
- package/src/reactivity/graph.ts +259 -0
- package/src/reactivity/index.ts +541 -0
- package/src/reactivity/selector.ts +78 -0
- package/src/reactivity/store.ts +252 -0
- package/src/server/action-flash.ts +158 -0
- package/src/server/action-protect.ts +199 -0
- package/src/server/action-registry.ts +78 -0
- package/src/server/action.ts +511 -0
- package/src/server/app-entry.ts +23 -0
- package/src/server/app-secret.ts +63 -0
- package/src/server/assets.ts +168 -0
- package/src/server/compress.ts +646 -0
- package/src/server/consent.ts +290 -0
- package/src/server/cookies.ts +153 -0
- package/src/server/document.ts +286 -0
- package/src/server/env.ts +47 -0
- package/src/server/errors.ts +396 -0
- package/src/server/form-schema.ts +337 -0
- package/src/server/health.ts +275 -0
- package/src/server/http.ts +588 -0
- package/src/server/index.ts +615 -0
- package/src/server/lifecycle.ts +75 -0
- package/src/server/loader.ts +131 -0
- package/src/server/page.ts +122 -0
- package/src/server/param-matchers.ts +112 -0
- package/src/server/plugin-host.ts +267 -0
- package/src/server/prerendered.ts +71 -0
- package/src/server/public.ts +20 -0
- package/src/server/rate-limit.ts +199 -0
- package/src/server/region.ts +96 -0
- package/src/server/request-body.ts +27 -0
- package/src/server/request-path.ts +73 -0
- package/src/server/request-scope.ts +22 -0
- package/src/server/route-cache.ts +541 -0
- package/src/server/route-table.ts +424 -0
- package/src/server/router.ts +261 -0
- package/src/server/routing.ts +388 -0
- package/src/server/sanitize-html.ts +309 -0
- package/src/server/security.ts +550 -0
- package/src/server/serve-handlers.ts +93 -0
- package/src/server/served-file.ts +128 -0
- package/src/server/standard-schema.ts +104 -0
- package/src/server/start.ts +143 -0
- package/src/server/static-dir.ts +246 -0
- package/src/server/stream.ts +233 -0
- package/src/ssr/consent-banner.ts +58 -0
- package/src/ssr/context.ts +399 -0
- package/src/ssr/index.ts +624 -0
- package/src/ssr/navigation.ts +96 -0
- package/src/ssr/region.ts +192 -0
- package/src/style.ts +96 -0
- package/src/test/browser.ts +201 -0
- package/src/test/index.ts +262 -0
- package/src/ts-plugin/diagnostics.ts +505 -0
- package/src/ts-plugin/fixes.ts +364 -0
- package/src/ts-plugin/index.ts +194 -0
- package/src/unrenderable.ts +35 -0
- package/templates/_shared/AGENTS.md +10 -0
- package/templates/_shared/CLAUDE.md +1 -0
- package/templates/_shared/gitignore +8 -0
- package/templates/_shared/mcp.json +11 -0
- package/templates/_shared/public/robots.txt +2 -0
- package/templates/_shared/src/routes/legal.tsx +22 -0
- package/templates/_shared/tsconfig.json +25 -0
- package/templates/app/.env.example +8 -0
- package/templates/app/package.json +22 -0
- package/templates/app/prunelle.config.ts +13 -0
- package/templates/app/src/auth.ts +12 -0
- package/templates/app/src/mail.ts +4 -0
- package/templates/app/src/routes/_layout.tsx +40 -0
- package/templates/app/src/routes/dashboard/_middleware.ts +3 -0
- package/templates/app/src/routes/dashboard/index.tsx +47 -0
- package/templates/app/src/routes/index.tsx +15 -0
- package/templates/app/src/routes/login.tsx +53 -0
- package/templates/app/src/routes/privacy.tsx +25 -0
- package/templates/app/src/styles/app.css +14 -0
- package/templates/app/tests/app.test.ts +73 -0
- package/templates/blog/content/posts/welcome.md +9 -0
- package/templates/blog/package.json +22 -0
- package/templates/blog/prunelle.config.ts +23 -0
- package/templates/blog/src/posts.ts +26 -0
- package/templates/blog/src/routes/_layout.tsx +25 -0
- package/templates/blog/src/routes/blog/[slug].tsx +31 -0
- package/templates/blog/src/routes/index.tsx +33 -0
- package/templates/blog/src/styles/app.css +15 -0
- package/templates/fullstack/package.json +19 -0
- package/templates/fullstack/prunelle.config.ts +6 -0
- package/templates/fullstack/src/actions/greet.ts +19 -0
- package/templates/fullstack/src/components/Counter.tsx +10 -0
- package/templates/fullstack/src/components/GreetForm.tsx +28 -0
- package/templates/fullstack/src/routes/_layout.tsx +18 -0
- package/templates/fullstack/src/routes/api/health.ts +5 -0
- package/templates/fullstack/src/routes/index.tsx +13 -0
- package/templates/fullstack/src/styles/app.css +39 -0
- package/templates/fullstack/tests/app.test.ts +37 -0
- package/templates/minimal/package.json +19 -0
- package/templates/minimal/prunelle.config.ts +6 -0
- package/templates/minimal/src/components/Counter.tsx +10 -0
- package/templates/minimal/src/routes/_layout.tsx +18 -0
- package/templates/minimal/src/routes/index.tsx +11 -0
- package/templates/minimal/src/styles/app.css +32 -0
- package/ts-plugin.cjs +4277 -0
- package/README.md +0 -3
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
// `prunelle build --static`: gathers the prerendered pages, client assets and public files into one folder and swaps it in as the output directory.
|
|
2
|
+
|
|
3
|
+
import type { PrunellePlugin } from '../plugins/index'
|
|
4
|
+
import type { ServerEntry } from '../server/app-entry'
|
|
5
|
+
import type { PrunelleApp } from '../server/index'
|
|
6
|
+
import type { BuildManifest, StatusPageManifestEntry } from './manifest'
|
|
7
|
+
import { copyFileSync, mkdirSync, writeFileSync } from 'node:fs'
|
|
8
|
+
import { dirname, join, relative } from 'node:path'
|
|
9
|
+
import { resolvePlugins } from '../plugins/index'
|
|
10
|
+
import { createDocumentHooks, createHtmlHeaders } from '../server/document'
|
|
11
|
+
import { createFallbackPages } from '../server/errors'
|
|
12
|
+
import { resolveParamMatchers } from '../server/param-matchers'
|
|
13
|
+
import { prerenderOrigin } from '../server/prerendered'
|
|
14
|
+
import { inferRoutesServerPrefix } from '../server/routing'
|
|
15
|
+
import { injectMetaCsp } from '../server/security'
|
|
16
|
+
import { createNavigation } from '../ssr/navigation'
|
|
17
|
+
import { carryPreviousAssets } from './carry'
|
|
18
|
+
import { walkAllFiles } from './files'
|
|
19
|
+
import { swapStage } from './output'
|
|
20
|
+
import { precompressFiles } from './precompress'
|
|
21
|
+
import { pageFailure } from './prerender'
|
|
22
|
+
import { writeStaticHostFiles } from './static-headers'
|
|
23
|
+
|
|
24
|
+
const LEADING_SLASH_RE = /^\//
|
|
25
|
+
|
|
26
|
+
export interface StaticExportOptions {
|
|
27
|
+
/** The app's folder, whose `.prunelle-cache/` keeps the brotli of what was compressed before. */
|
|
28
|
+
root: string
|
|
29
|
+
/** The folder the site is assembled in; it becomes `finalOutDir`. */
|
|
30
|
+
stageDir: string
|
|
31
|
+
finalOutDir: string
|
|
32
|
+
/** The regular build output the assets and public files are copied from. */
|
|
33
|
+
outDir: string
|
|
34
|
+
clientDir: string
|
|
35
|
+
assetPrefix: string
|
|
36
|
+
site?: string | undefined
|
|
37
|
+
isDev?: boolean | undefined
|
|
38
|
+
manifest: BuildManifest
|
|
39
|
+
entry: ServerEntry | undefined
|
|
40
|
+
pluginOverride?: PrunellePlugin[] | undefined
|
|
41
|
+
assets: string[]
|
|
42
|
+
publicFiles: { files: string[], register: (relPath: string) => string }
|
|
43
|
+
/** The server the pages were prerendered with, holding plugin services, rewrites and middlewares. */
|
|
44
|
+
app: PrunelleApp | undefined
|
|
45
|
+
errorFor?: ((url: string) => unknown) | undefined
|
|
46
|
+
hasNotFoundFile: boolean
|
|
47
|
+
notFoundEmitted?: boolean | undefined
|
|
48
|
+
stageSuffix: string
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function copyInto(dstRoot: string, rel: string, src: string): void {
|
|
52
|
+
const dst = join(dstRoot, rel)
|
|
53
|
+
mkdirSync(dirname(dst), { recursive: true })
|
|
54
|
+
copyFileSync(src, dst)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
interface StaticNotFoundTarget {
|
|
58
|
+
outRel: string
|
|
59
|
+
urlPath: string
|
|
60
|
+
routeFile?: string | undefined
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function staticNotFoundTargets(manifest: BuildManifest, hasNotFoundFile: boolean): StaticNotFoundTarget[] {
|
|
64
|
+
const targets: StaticNotFoundTarget[] = []
|
|
65
|
+
const seen = new Set<string>()
|
|
66
|
+
const statusPages: StatusPageManifestEntry[] = manifest.statusPages ?? []
|
|
67
|
+
for (const entry of statusPages) {
|
|
68
|
+
if (entry.kind !== '404' || entry.inGroup === true)
|
|
69
|
+
continue
|
|
70
|
+
if (entry.dirPattern.includes(':') || entry.dirPattern.includes('*'))
|
|
71
|
+
continue
|
|
72
|
+
const cleanDir = entry.dirPattern === '/' ? '' : entry.dirPattern.replace(LEADING_SLASH_RE, '')
|
|
73
|
+
const outRel = cleanDir === '' ? '404.html' : `${cleanDir}/404.html`
|
|
74
|
+
if (seen.has(outRel))
|
|
75
|
+
continue
|
|
76
|
+
seen.add(outRel)
|
|
77
|
+
targets.push({
|
|
78
|
+
outRel,
|
|
79
|
+
urlPath: cleanDir === '' ? '/404' : `/${cleanDir}/404`,
|
|
80
|
+
routeFile: entry.file,
|
|
81
|
+
})
|
|
82
|
+
}
|
|
83
|
+
if (hasNotFoundFile && !seen.has('404.html'))
|
|
84
|
+
targets.push({ outRel: '404.html', urlPath: '/404' })
|
|
85
|
+
return targets
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export interface StaticNotFoundOptions {
|
|
89
|
+
stageDir: string
|
|
90
|
+
site?: string | undefined
|
|
91
|
+
isDev?: boolean | undefined
|
|
92
|
+
manifest: BuildManifest
|
|
93
|
+
entry: ServerEntry | undefined
|
|
94
|
+
pluginOverride?: PrunellePlugin[] | undefined
|
|
95
|
+
app: PrunelleApp | undefined
|
|
96
|
+
errorFor?: ((url: string) => unknown) | undefined
|
|
97
|
+
hasNotFoundFile: boolean
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function hasCatchAllRoute(manifest: BuildManifest): boolean {
|
|
101
|
+
return manifest.routes.some(r => r.path.includes('*'))
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function writeStaticNotFound(stageDir: string, outRel: string, html: string, csp: string | null): void {
|
|
105
|
+
const outAbs = join(stageDir, outRel)
|
|
106
|
+
mkdirSync(dirname(outAbs), { recursive: true })
|
|
107
|
+
writeFileSync(outAbs, injectMetaCsp(html, csp))
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export async function emitStaticNotFoundPages(options: StaticNotFoundOptions): Promise<void> {
|
|
111
|
+
const targets = staticNotFoundTargets(options.manifest, options.hasNotFoundFile)
|
|
112
|
+
if (targets.length === 0 || (options.entry === undefined && options.app === undefined))
|
|
113
|
+
return
|
|
114
|
+
const origin = prerenderOrigin(options.site)
|
|
115
|
+
const catchAllPresent = hasCatchAllRoute(options.manifest)
|
|
116
|
+
const fallbackTargets: StaticNotFoundTarget[] = []
|
|
117
|
+
|
|
118
|
+
if (options.app !== undefined) {
|
|
119
|
+
for (const target of targets) {
|
|
120
|
+
// A root catch-all route returning notFound() would select the root _404 instead of a nested _404.
|
|
121
|
+
if (catchAllPresent && target.outRel !== '404.html') {
|
|
122
|
+
fallbackTargets.push(target)
|
|
123
|
+
continue
|
|
124
|
+
}
|
|
125
|
+
const probePath = target.urlPath.replace(/\/404$/, '/__pru_404__')
|
|
126
|
+
const req = new Request(`${origin}${probePath}`)
|
|
127
|
+
const res = await options.app.handle(req)
|
|
128
|
+
const renderError = options.errorFor?.(req.url)
|
|
129
|
+
if (renderError !== undefined || res.status >= 500)
|
|
130
|
+
throw pageFailure(`prunelle build: the _404 page "${target.outRel}" of the static export answered HTTP ${res.status}`, renderError)
|
|
131
|
+
if (res.status === 404) {
|
|
132
|
+
writeStaticNotFound(options.stageDir, target.outRel, await res.text(), res.headers.get('content-security-policy'))
|
|
133
|
+
continue
|
|
134
|
+
}
|
|
135
|
+
if (options.entry !== undefined)
|
|
136
|
+
fallbackTargets.push(target)
|
|
137
|
+
else
|
|
138
|
+
writeStaticNotFound(options.stageDir, target.outRel, await res.text(), res.headers.get('content-security-policy'))
|
|
139
|
+
}
|
|
140
|
+
if (fallbackTargets.length === 0)
|
|
141
|
+
return
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
fallbackTargets.push(...targets)
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (options.entry === undefined)
|
|
148
|
+
return
|
|
149
|
+
|
|
150
|
+
const plugins = resolvePlugins(options.pluginOverride ?? options.entry.prunelleConfig.plugins)
|
|
151
|
+
const lang = options.entry.prunelleConfig.lang ?? 'en'
|
|
152
|
+
const trustProxy = options.entry.prunelleConfig.trustProxy ?? false
|
|
153
|
+
let renderError: unknown
|
|
154
|
+
const notifyError = (err: unknown): void => {
|
|
155
|
+
renderError ??= err
|
|
156
|
+
}
|
|
157
|
+
const document = createDocumentHooks({ plugins, lang, trustProxy, notifyError })
|
|
158
|
+
const navigation = createNavigation(options.entry.prunelleConfig.navigation)
|
|
159
|
+
const htmlHeaders = createHtmlHeaders(options.entry.prunelleConfig.security, {}, navigation, plugins)
|
|
160
|
+
const routesPrefix = inferRoutesServerPrefix(options.manifest)
|
|
161
|
+
const paramMatchers = resolveParamMatchers(options.manifest.paramMatchers, options.entry.modules)
|
|
162
|
+
const { buildNotFoundResponse } = createFallbackPages({
|
|
163
|
+
manifest: options.manifest,
|
|
164
|
+
modules: options.entry.modules,
|
|
165
|
+
routesPrefix,
|
|
166
|
+
paramMatchers,
|
|
167
|
+
lang,
|
|
168
|
+
alwaysLoadRuntime: plugins.some(p => Boolean(p.clientEntry)),
|
|
169
|
+
navigation: navigation.html,
|
|
170
|
+
dev: options.isDev === true,
|
|
171
|
+
document,
|
|
172
|
+
htmlHeaders,
|
|
173
|
+
notifyError,
|
|
174
|
+
finish: res => res,
|
|
175
|
+
})
|
|
176
|
+
|
|
177
|
+
for (const target of fallbackTargets) {
|
|
178
|
+
renderError = undefined
|
|
179
|
+
const url = new URL(target.urlPath, origin)
|
|
180
|
+
const res = await buildNotFoundResponse(url, new Request(url), undefined, target.routeFile)
|
|
181
|
+
if (renderError !== undefined || res.status >= 500)
|
|
182
|
+
throw pageFailure(`prunelle build: the _404 page "${target.outRel}" of the static export answered HTTP ${res.status}`, renderError)
|
|
183
|
+
writeStaticNotFound(options.stageDir, target.outRel, await res.text(), res.headers.get('content-security-policy'))
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export async function exportStatic(options: StaticExportOptions): Promise<void> {
|
|
188
|
+
const { stageDir, finalOutDir, outDir, clientDir, publicFiles } = options
|
|
189
|
+
if (options.notFoundEmitted !== true)
|
|
190
|
+
await emitStaticNotFoundPages(options)
|
|
191
|
+
|
|
192
|
+
// Client assets go to <stage>/_pru/..., public files to <stage>/...
|
|
193
|
+
const assetDir = join(stageDir, options.assetPrefix.replace(LEADING_SLASH_RE, ''))
|
|
194
|
+
for (const rel of options.assets)
|
|
195
|
+
copyInto(assetDir, rel, join(clientDir, rel))
|
|
196
|
+
// Also pick up files written directly into the public folder without registering them.
|
|
197
|
+
const outPublicDir = join(outDir, 'public')
|
|
198
|
+
for (const abs of walkAllFiles(outPublicDir))
|
|
199
|
+
publicFiles.register(relative(outPublicDir, abs))
|
|
200
|
+
for (const rel of publicFiles.files)
|
|
201
|
+
copyInto(stageDir, rel, join(outPublicDir, rel))
|
|
202
|
+
writeStaticHostFiles(stageDir, options.entry?.prunelleConfig.security)
|
|
203
|
+
await precompressFiles(walkAllFiles(stageDir), 128, options.root)
|
|
204
|
+
if (!options.isDev)
|
|
205
|
+
carryPreviousAssets(finalOutDir, stageDir, relative(stageDir, assetDir), options.assets)
|
|
206
|
+
|
|
207
|
+
// Swap the stage in only after every page and asset succeeded.
|
|
208
|
+
swapStage(stageDir, finalOutDir, options.stageSuffix)
|
|
209
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
// Status and error page discovery, validation, and manifest entry construction (`_401`, `_403`, `_404`, `_429`, `_503`, `_error`).
|
|
2
|
+
|
|
3
|
+
import { basename, dirname, relative } from 'node:path'
|
|
4
|
+
import { relPosix } from './files'
|
|
5
|
+
import { ancestorFiles, dirToRoutePatternFromRel, hasRouteGroupInRel } from './routes'
|
|
6
|
+
|
|
7
|
+
export type StatusPageKind = '401' | '403' | '404' | '429' | '503' | 'error'
|
|
8
|
+
|
|
9
|
+
export const SUPPORTED_STATUS_CODES = new Set(['401', '403', '404', '429', '503'])
|
|
10
|
+
|
|
11
|
+
export const STATUS_PAGE_FILES = new Set([
|
|
12
|
+
'_401.tsx',
|
|
13
|
+
'_403.tsx',
|
|
14
|
+
'_404.tsx',
|
|
15
|
+
'_429.tsx',
|
|
16
|
+
'_503.tsx',
|
|
17
|
+
'_error.tsx',
|
|
18
|
+
])
|
|
19
|
+
|
|
20
|
+
const NUMERIC_STATUS_FILE_RE = /^_(\d{3})\.tsx$/
|
|
21
|
+
|
|
22
|
+
/** Maps a supported status/error page filename to its kind, or undefined for other files. */
|
|
23
|
+
export function statusPageKindOf(file: string): StatusPageKind | undefined {
|
|
24
|
+
const name = basename(file)
|
|
25
|
+
if (name === '_error.tsx')
|
|
26
|
+
return 'error'
|
|
27
|
+
const match = NUMERIC_STATUS_FILE_RE.exec(name)
|
|
28
|
+
if (match !== null && SUPPORTED_STATUS_CODES.has(match[1]!))
|
|
29
|
+
return match[1] as StatusPageKind
|
|
30
|
+
return undefined
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Reports an unsupported `_NNN.tsx` file (e.g. `_418.tsx`, `_500.tsx`) in `routesDir`. */
|
|
34
|
+
export function unknownStatusPageProblem(fileRel: string): string | undefined {
|
|
35
|
+
const match = NUMERIC_STATUS_FILE_RE.exec(basename(fileRel))
|
|
36
|
+
if (match === null || SUPPORTED_STATUS_CODES.has(match[1]!))
|
|
37
|
+
return undefined
|
|
38
|
+
return `unsupported status page ${fileRel}: supported status pages are _401.tsx, _403.tsx, _404.tsx, _429.tsx, _503.tsx, and _error.tsx.`
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface StatusPageManifestEntry {
|
|
42
|
+
kind: StatusPageKind
|
|
43
|
+
/** Route pattern of the folder holding this status page (`'/'`, `'/admin'`, `'/blog/:slug'`). */
|
|
44
|
+
dirPattern: string
|
|
45
|
+
/** Compiled server directory prefix holding this status page (`'server/'`, `'server/admin/'`, `'server/(admin)/'`). */
|
|
46
|
+
dirPrefix: string
|
|
47
|
+
/** True when the status page lives inside a `(group)/` folder. */
|
|
48
|
+
inGroup?: boolean | undefined
|
|
49
|
+
file: string
|
|
50
|
+
layouts: string[]
|
|
51
|
+
styles: string[]
|
|
52
|
+
islandChunks: string[]
|
|
53
|
+
eagerIslandChunks?: string[] | undefined
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface FileReachLike {
|
|
57
|
+
chunks: ReadonlySet<string>
|
|
58
|
+
eager: ReadonlySet<string>
|
|
59
|
+
styles: ReadonlySet<string>
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function compiledDirPrefix(outRel: string): string {
|
|
63
|
+
const slash = outRel.lastIndexOf('/')
|
|
64
|
+
return slash === -1 ? '' : outRel.slice(0, slash + 1)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Builds manifest entries for every status/error page (`_404`, `_error`, `_401`, `_403`, `_429`, `_503`) across `routesDir`. */
|
|
68
|
+
export function buildStatusPageEntries(options: {
|
|
69
|
+
routesDir: string
|
|
70
|
+
specialFiles: readonly string[]
|
|
71
|
+
globalPluginStyles: readonly string[]
|
|
72
|
+
reachOf: (file: string) => FileReachLike
|
|
73
|
+
toOutRel: (srcFile: string) => string
|
|
74
|
+
}): StatusPageManifestEntry[] {
|
|
75
|
+
const { routesDir, specialFiles, globalPluginStyles, reachOf, toOutRel } = options
|
|
76
|
+
const entries: StatusPageManifestEntry[] = []
|
|
77
|
+
|
|
78
|
+
for (const file of specialFiles) {
|
|
79
|
+
const kind = statusPageKindOf(file)
|
|
80
|
+
if (kind === undefined)
|
|
81
|
+
continue
|
|
82
|
+
const relDir = relative(routesDir, dirname(file)).replace(/\\/g, '/')
|
|
83
|
+
const dirPattern = dirToRoutePatternFromRel(relDir)
|
|
84
|
+
const inGroup = hasRouteGroupInRel(relDir)
|
|
85
|
+
const layoutSrcs = ancestorFiles(routesDir, file, ['_layout.tsx'])
|
|
86
|
+
const chunks = new Set<string>()
|
|
87
|
+
const eagerChunks = new Set<string>()
|
|
88
|
+
const styles = new Set<string>(globalPluginStyles)
|
|
89
|
+
|
|
90
|
+
for (const src of [...layoutSrcs, file]) {
|
|
91
|
+
const reach = reachOf(src)
|
|
92
|
+
for (const chunk of reach.chunks)
|
|
93
|
+
chunks.add(chunk)
|
|
94
|
+
for (const chunk of reach.eager)
|
|
95
|
+
eagerChunks.add(chunk)
|
|
96
|
+
for (const style of reach.styles)
|
|
97
|
+
styles.add(style)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const outRel = toOutRel(file)
|
|
101
|
+
entries.push({
|
|
102
|
+
kind,
|
|
103
|
+
dirPattern,
|
|
104
|
+
dirPrefix: compiledDirPrefix(outRel),
|
|
105
|
+
...(inGroup ? { inGroup: true } : {}),
|
|
106
|
+
file: outRel,
|
|
107
|
+
layouts: layoutSrcs.map(toOutRel),
|
|
108
|
+
styles: [...styles],
|
|
109
|
+
islandChunks: [...chunks],
|
|
110
|
+
eagerIslandChunks: [...eagerChunks],
|
|
111
|
+
})
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return entries
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Finds every unsupported `_NNN.tsx` file under `routesDir` and returns its error messages. */
|
|
118
|
+
export function findUnknownStatusPages(root: string, routeFiles: readonly string[]): { file: string, message: string }[] {
|
|
119
|
+
const problems: { file: string, message: string }[] = []
|
|
120
|
+
for (const file of routeFiles) {
|
|
121
|
+
const rel = relPosix(root, file)
|
|
122
|
+
const message = unknownStatusPageProblem(rel)
|
|
123
|
+
if (message !== undefined)
|
|
124
|
+
problems.push({ file, message })
|
|
125
|
+
}
|
|
126
|
+
return problems
|
|
127
|
+
}
|