prunelle 0.6.0 → 0.7.1
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 +48 -43
- package/package.json +4 -1
- package/src/a11y/declaration.ts +106 -72
- package/src/a11y/index.ts +59 -39
- package/src/a11y/rgaa-map.ts +95 -49
- package/src/build/bundle.ts +8 -4
- package/src/build/byte-size.ts +11 -0
- package/src/build/client-plugin.ts +3 -1
- package/src/build/compile.ts +2 -1
- package/src/build/ecoindex.ts +25 -16
- package/src/build/files.ts +6 -0
- package/src/build/first-load.ts +83 -11
- package/src/build/graph.ts +29 -39
- package/src/build/index.ts +61 -18
- package/src/build/island-promote.ts +250 -0
- package/src/build/islands.ts +12 -201
- package/src/build/manifest.ts +4 -125
- package/src/build/plugin-host.ts +21 -8
- package/src/build/precompress.ts +1 -1
- package/src/build/prerender.ts +97 -44
- package/src/build/reach.ts +136 -0
- package/src/build/render-rules.ts +2 -2
- package/src/build/report.ts +25 -3
- package/src/build/static-headers.ts +93 -0
- package/src/build/static.ts +2 -0
- package/src/cli/check-forms.ts +97 -48
- package/src/cli/check-imports.ts +25 -0
- package/src/cli/check-links.ts +2 -2
- package/src/cli/check-security.ts +64 -0
- package/src/cli/check.ts +37 -7
- package/src/cli/commands.ts +35 -7
- package/src/cli/docs.ts +4 -3
- package/src/cli/inspect.ts +2 -1
- package/src/cli/mcp-tools.ts +7 -5
- package/src/cli/mcp.ts +1 -1
- package/src/cli/new.ts +14 -3
- package/src/cli/render.ts +14 -19
- package/src/cli/upgrade.ts +22 -5
- package/src/compiler/analyze.ts +27 -5
- package/src/compiler/cacheability.ts +2 -2
- package/src/compiler/compliance-eco.ts +71 -0
- package/src/compiler/compliance-rgaa.ts +110 -4
- package/src/compiler/compliance-urls.ts +146 -0
- package/src/compiler/compliance.ts +168 -116
- package/src/compiler/foreign-imports.ts +88 -0
- package/src/compiler/mistakes.ts +19 -1
- package/src/compiler/props-body.ts +60 -0
- package/src/compiler/shared.ts +2 -2
- package/src/compiler/ssr.ts +2 -2
- package/src/context.ts +34 -0
- package/src/dev/error-log.ts +90 -64
- package/src/dev/host-check.ts +32 -0
- package/src/dev/index.ts +17 -7
- package/src/dev/open-editor.ts +36 -12
- package/src/dev/protocol.ts +2 -0
- package/src/dev/supervisor.ts +8 -0
- package/src/dev/watchers.ts +27 -2
- package/src/dom/action.ts +4 -1
- package/src/dom/extras.ts +1 -2
- package/src/dom/index.ts +28 -6
- package/src/dom/island.ts +22 -10
- package/src/dom/loop.ts +60 -27
- package/src/eco/declaration.ts +257 -0
- package/src/eco/index.ts +65 -0
- package/src/index.ts +2 -2
- package/src/jsx-runtime.ts +2 -2
- package/src/plugins/config.ts +1 -1
- package/src/plugins/context.ts +44 -1
- package/src/plugins/load.ts +4 -3
- package/src/plugins/public.ts +2 -2
- package/src/plugins/services.ts +3 -0
- package/src/plugins/types.ts +17 -2
- package/src/server/action-flash.ts +158 -0
- package/src/server/action-protect.ts +38 -37
- package/src/server/action.ts +11 -127
- package/src/server/app-secret.ts +63 -0
- package/src/server/compress.ts +5 -1
- package/src/server/consent.ts +59 -22
- package/src/server/cookies.ts +16 -2
- package/src/server/env.ts +14 -0
- package/src/server/errors.ts +3 -1
- package/src/server/form-schema.ts +1 -1
- package/src/server/health.ts +46 -15
- package/src/server/http.ts +56 -8
- package/src/server/index.ts +59 -30
- package/src/server/loader.ts +10 -2
- package/src/server/plugin-host.ts +3 -1
- package/src/server/public.ts +4 -3
- package/src/server/rate-limit.ts +52 -9
- package/src/server/route-cache.ts +26 -4
- package/src/server/route-table.ts +7 -3
- package/src/server/routing.ts +3 -87
- package/src/server/sanitize-html.ts +309 -0
- package/src/server/security.ts +73 -22
- package/src/server/serve-handlers.ts +14 -4
- package/src/server/start.ts +1 -1
- package/src/server/static-dir.ts +31 -5
- package/src/server/stream.ts +28 -6
- package/src/ssr/consent-banner.ts +58 -0
- package/src/ssr/context.ts +20 -4
- package/src/ssr/index.ts +18 -0
- package/src/ssr/navigation.ts +2 -2
- package/src/style.ts +64 -1
- package/templates/_shared/gitignore +5 -0
- package/templates/_shared/src/routes/legal.tsx +10 -1
- package/templates/app/.env.example +4 -0
- package/templates/app/package.json +2 -1
- package/templates/app/prunelle.config.ts +2 -0
- package/templates/app/src/auth.ts +4 -6
- package/templates/app/src/mail.ts +4 -0
- package/templates/app/src/routes/_layout.tsx +9 -1
- package/templates/app/src/routes/dashboard/index.tsx +42 -8
- package/templates/app/src/routes/login.tsx +49 -30
- package/templates/app/src/routes/privacy.tsx +25 -0
- package/templates/app/src/styles/app.css +8 -1
- package/templates/app/tests/app.test.ts +44 -3
- package/templates/blog/package.json +1 -1
- package/templates/blog/src/routes/_layout.tsx +6 -1
- package/templates/blog/src/routes/index.tsx +1 -1
- package/templates/blog/src/styles/app.css +6 -1
- package/templates/fullstack/package.json +1 -1
- package/templates/fullstack/src/components/GreetForm.tsx +2 -1
- package/templates/fullstack/src/routes/_layout.tsx +4 -0
- package/templates/fullstack/src/styles/app.css +11 -1
- package/templates/minimal/package.json +1 -1
- package/templates/minimal/src/routes/_layout.tsx +4 -0
- package/templates/minimal/src/styles/app.css +11 -1
package/src/build/islands.ts
CHANGED
|
@@ -3,14 +3,16 @@
|
|
|
3
3
|
import type { ModuleAnalysis } from '../compiler/analyze'
|
|
4
4
|
import type { ServerOnlyInIsland } from '../compiler/interactivity'
|
|
5
5
|
import type { ModuleGraph } from './graph'
|
|
6
|
+
import type { IslandValueUse, LoadModule, PromotedWarning } from './island-promote'
|
|
6
7
|
import { basename, extname } from 'node:path'
|
|
7
8
|
import { asyncInteractiveError } from '../compiler/analyze'
|
|
8
|
-
import { findCrossFileServerOnly
|
|
9
|
+
import { findCrossFileServerOnly } from '../compiler/interactivity'
|
|
9
10
|
import { collectMistakes, serverOnlyTagError } from '../compiler/mistakes'
|
|
10
11
|
import { findPrivateEnv, isServerOnlyModule } from '../compiler/server-only'
|
|
11
12
|
import { checkBareSignals } from '../compiler/signal-reads'
|
|
12
13
|
import { clientNeeds, clientView } from './client-prune'
|
|
13
14
|
import { isInNodeModules, isTsFile, relPosix } from './files'
|
|
15
|
+
import { collectExportedSignals, createPromoter, resolveExportTarget } from './island-promote'
|
|
14
16
|
|
|
15
17
|
/** A component that is not an island, as a module renders it. */
|
|
16
18
|
export interface StaticTag { async: boolean }
|
|
@@ -53,6 +55,10 @@ export interface IslandPlan {
|
|
|
53
55
|
clientKeep: Map<string, Set<string>>
|
|
54
56
|
/** Island and reachability issues found during analysis. */
|
|
55
57
|
issues: IslandIssue[]
|
|
58
|
+
/** Imported islands read as values, each making what holds it an island (`prunelle check` warns). */
|
|
59
|
+
valueUses: IslandValueUse[]
|
|
60
|
+
/** Compiler warnings triggered once a route is promoted to an island across files. */
|
|
61
|
+
promotedWarnings: PromotedWarning[]
|
|
56
62
|
}
|
|
57
63
|
|
|
58
64
|
export interface AnalyzeIslandsOptions {
|
|
@@ -68,9 +74,7 @@ export interface AnalyzeIslandsOptions {
|
|
|
68
74
|
checkLeaks?: boolean | undefined
|
|
69
75
|
}
|
|
70
76
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
const MAX_REEXPORT_DEPTH = 8
|
|
77
|
+
const EMPTY_SET = new Set<string>()
|
|
74
78
|
|
|
75
79
|
export function islandKey(target: { file: string, exportName: string }): string {
|
|
76
80
|
return `${target.file}#${target.exportName}`
|
|
@@ -85,201 +89,6 @@ export function clientLeakError(leaks: Set<string>): Error {
|
|
|
85
89
|
return new Error(`prunelle build (client): server code is reachable from an island:\n${[...leaks].map(l => ` ${l}`).join('\n')}\nMove server logic to a server action or async server component.`)
|
|
86
90
|
}
|
|
87
91
|
|
|
88
|
-
/** Follows `export { x } from './y'` and `export * from './y'` chains using `load` so parse errors are never swallowed. */
|
|
89
|
-
function resolveExportTarget(
|
|
90
|
-
graph: ModuleGraph,
|
|
91
|
-
load: LoadModule,
|
|
92
|
-
file: string,
|
|
93
|
-
exportName: string,
|
|
94
|
-
depth = 0,
|
|
95
|
-
seen = new Set<string>(),
|
|
96
|
-
): { file: string, exportName: string } | undefined {
|
|
97
|
-
if (depth > MAX_REEXPORT_DEPTH)
|
|
98
|
-
return { file, exportName }
|
|
99
|
-
const key = `${file}\0${exportName}`
|
|
100
|
-
if (seen.has(key))
|
|
101
|
-
return { file, exportName }
|
|
102
|
-
seen.add(key)
|
|
103
|
-
|
|
104
|
-
const analysis = load(file)
|
|
105
|
-
if (analysis === undefined)
|
|
106
|
-
return undefined
|
|
107
|
-
const direct = analysis.reexports.find(r => r.exported === exportName)
|
|
108
|
-
if (direct !== undefined) {
|
|
109
|
-
const next = graph.resolve(file, direct.source)
|
|
110
|
-
if (next !== undefined && isTsFile(next))
|
|
111
|
-
return resolveExportTarget(graph, load, next, direct.local, depth + 1, seen)
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
if (analysis.exportedNames.includes(exportName))
|
|
115
|
-
return { file, exportName }
|
|
116
|
-
|
|
117
|
-
// Follow `export * from './y'` barrel re-exports (named exports only, per ESM spec `export *` never re-exports `default`).
|
|
118
|
-
if (exportName !== 'default') {
|
|
119
|
-
for (const re of analysis.reexports) {
|
|
120
|
-
if (re.exported !== '*')
|
|
121
|
-
continue
|
|
122
|
-
const next = graph.resolve(file, re.source)
|
|
123
|
-
if (next === undefined || !isTsFile(next))
|
|
124
|
-
continue
|
|
125
|
-
const candidate = resolveExportTarget(graph, load, next, exportName, depth + 1, seen)
|
|
126
|
-
if (candidate !== undefined && load(candidate.file)?.exportedNames.includes(candidate.exportName))
|
|
127
|
-
return candidate
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
return { file, exportName }
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
const EMPTY_SET = new Set<string>()
|
|
135
|
-
|
|
136
|
-
function moduleHasInteractiveExport(graph: ModuleGraph, load: LoadModule, file: string, seen = new Set<string>()): boolean {
|
|
137
|
-
if (seen.has(file))
|
|
138
|
-
return false
|
|
139
|
-
seen.add(file)
|
|
140
|
-
const analysis = graph.analyses.get(file) ?? load(file)
|
|
141
|
-
if (analysis === undefined)
|
|
142
|
-
return false
|
|
143
|
-
if (analysis.stateExports.length > 0 || [...analysis.exports.values()].some(e => e.interactive))
|
|
144
|
-
return true
|
|
145
|
-
for (const re of analysis.reexports) {
|
|
146
|
-
const next = graph.resolve(file, re.source)
|
|
147
|
-
if (next === undefined || !isTsFile(next))
|
|
148
|
-
continue
|
|
149
|
-
if (re.exported === '*') {
|
|
150
|
-
if (moduleHasInteractiveExport(graph, load, next, seen))
|
|
151
|
-
return true
|
|
152
|
-
}
|
|
153
|
-
else {
|
|
154
|
-
const target = resolveExportTarget(graph, load, next, re.local)
|
|
155
|
-
const targetAnalysis = target === undefined ? undefined : (graph.analyses.get(target.file) ?? load(target.file))
|
|
156
|
-
if (target !== undefined && (targetAnalysis?.stateExports.includes(target.exportName) === true || targetAnalysis?.exports.get(target.exportName)?.interactive === true))
|
|
157
|
-
return true
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
return false
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
function collectExportedSignals(graph: ModuleGraph, load: LoadModule, file: string, out: Set<string>, seen = new Set<string>()): void {
|
|
164
|
-
if (seen.has(file))
|
|
165
|
-
return
|
|
166
|
-
seen.add(file)
|
|
167
|
-
const analysis = graph.analyses.get(file) ?? load(file)
|
|
168
|
-
if (analysis === undefined)
|
|
169
|
-
return
|
|
170
|
-
for (const name of analysis.signalExports)
|
|
171
|
-
out.add(name)
|
|
172
|
-
for (const re of analysis.reexports) {
|
|
173
|
-
const next = graph.resolve(file, re.source)
|
|
174
|
-
if (next === undefined || !isTsFile(next))
|
|
175
|
-
continue
|
|
176
|
-
if (re.exported === '*') {
|
|
177
|
-
collectExportedSignals(graph, load, next, out, seen)
|
|
178
|
-
}
|
|
179
|
-
else {
|
|
180
|
-
const target = resolveExportTarget(graph, load, next, re.local)
|
|
181
|
-
if (target !== undefined && (graph.analyses.get(target.file) ?? load(target.file))?.signalExports.includes(target.exportName) === true)
|
|
182
|
-
out.add(re.exported)
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
function interactivityChanged(prev: ModuleAnalysis, next: { isInteractive: boolean, exports: Map<string, { interactive: boolean }>, localIslands: Map<string, unknown> }): boolean {
|
|
188
|
-
if (prev.isInteractive !== next.isInteractive)
|
|
189
|
-
return true
|
|
190
|
-
for (const [name, fact] of next.exports) {
|
|
191
|
-
if (prev.exports.get(name)?.interactive !== fact.interactive)
|
|
192
|
-
return true
|
|
193
|
-
}
|
|
194
|
-
return next.localIslands.size !== prev.localIslands.size || [...next.localIslands.keys()].some(name => !prev.localIslands.has(name))
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
function createPromoter(graph: ModuleGraph, load: LoadModule, issues: IslandIssue[]): { promoteAll: () => void, loadPromoted: LoadModule } {
|
|
198
|
-
const initialAsyncInteractive = new Set<string>()
|
|
199
|
-
const initialServerOnly = new Set<string>()
|
|
200
|
-
const reportedAsync = new Set<string>()
|
|
201
|
-
const reportedServerOnly = new Set<string>()
|
|
202
|
-
|
|
203
|
-
const recordInitial = (file: string, a: ModuleAnalysis): void => {
|
|
204
|
-
if (a.hasAsyncDefault && a.exports.get('default')?.interactive === true)
|
|
205
|
-
initialAsyncInteractive.add(file)
|
|
206
|
-
if (a.serverOnly !== undefined)
|
|
207
|
-
initialServerOnly.add(file)
|
|
208
|
-
}
|
|
209
|
-
for (const [file, a] of graph.analyses)
|
|
210
|
-
recordInitial(file, a)
|
|
211
|
-
|
|
212
|
-
const isInteractiveTarget = (file: string, source: string, imported: string): boolean => {
|
|
213
|
-
const resolved = graph.resolve(file, source)
|
|
214
|
-
if (resolved === undefined || !isTsFile(resolved))
|
|
215
|
-
return false
|
|
216
|
-
if (imported === '*')
|
|
217
|
-
return moduleHasInteractiveExport(graph, load, resolved)
|
|
218
|
-
const target = resolveExportTarget(graph, load, resolved, imported)
|
|
219
|
-
if (target === undefined)
|
|
220
|
-
return false
|
|
221
|
-
const targetAnalysis = graph.analyses.get(target.file) ?? load(target.file)
|
|
222
|
-
return targetAnalysis?.stateExports.includes(target.exportName) === true
|
|
223
|
-
|| targetAnalysis?.exports.get(target.exportName)?.interactive === true
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
const promoteOne = (file: string): boolean => {
|
|
227
|
-
if (isInNodeModules(file) || !isTsFile(file))
|
|
228
|
-
return false
|
|
229
|
-
const analysis = graph.analyses.get(file)
|
|
230
|
-
if (analysis === undefined)
|
|
231
|
-
return false
|
|
232
|
-
const promoted = withStateReads(analysis, (imp, imported) => isInteractiveTarget(file, imp.source, imported))
|
|
233
|
-
if (promoted === undefined)
|
|
234
|
-
return false
|
|
235
|
-
const page = promoted.exports.get('default')
|
|
236
|
-
if (analysis.hasAsyncDefault && page?.interactive === true) {
|
|
237
|
-
if (!initialAsyncInteractive.has(file) && !reportedAsync.has(file)) {
|
|
238
|
-
reportedAsync.add(file)
|
|
239
|
-
issues.push({ kind: 'async-interactive', file, offset: page.offset ?? 0, reason: page.reason })
|
|
240
|
-
}
|
|
241
|
-
return false
|
|
242
|
-
}
|
|
243
|
-
if (promoted.serverOnly !== undefined) {
|
|
244
|
-
if (!initialServerOnly.has(file) && !reportedServerOnly.has(file)) {
|
|
245
|
-
reportedServerOnly.add(file)
|
|
246
|
-
issues.push({ kind: 'server-only-tag', file, serverOnly: promoted.serverOnly })
|
|
247
|
-
}
|
|
248
|
-
return false
|
|
249
|
-
}
|
|
250
|
-
if (!interactivityChanged(analysis, promoted))
|
|
251
|
-
return false
|
|
252
|
-
graph.analyses.set(file, { ...analysis, ...promoted })
|
|
253
|
-
return true
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
const promoteAll = (): void => {
|
|
257
|
-
for (let pass = 0; pass < MAX_REEXPORT_DEPTH; pass++) {
|
|
258
|
-
let changed = false
|
|
259
|
-
for (const file of [...graph.analyses.keys()]) {
|
|
260
|
-
if (promoteOne(file))
|
|
261
|
-
changed = true
|
|
262
|
-
}
|
|
263
|
-
if (!changed)
|
|
264
|
-
break
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
const loadPromoted: LoadModule = (file: string): ModuleAnalysis | undefined => {
|
|
269
|
-
const existed = graph.analyses.has(file)
|
|
270
|
-
const analysis = graph.analyses.get(file) ?? load(file)
|
|
271
|
-
if (analysis === undefined)
|
|
272
|
-
return undefined
|
|
273
|
-
if (!existed) {
|
|
274
|
-
recordInitial(file, analysis)
|
|
275
|
-
promoteAll()
|
|
276
|
-
}
|
|
277
|
-
return graph.analyses.get(file) ?? analysis
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
return { promoteAll, loadPromoted }
|
|
281
|
-
}
|
|
282
|
-
|
|
283
92
|
function checkImportedBareSignals(graph: ModuleGraph, load: LoadModule, root: string, issues: IslandIssue[]): void {
|
|
284
93
|
for (const [file, analysis] of [...graph.analyses]) {
|
|
285
94
|
if (isInNodeModules(file) || !isTsFile(file))
|
|
@@ -551,7 +360,9 @@ export function analyzeIslands(options: AnalyzeIslandsOptions): IslandPlan {
|
|
|
551
360
|
}
|
|
552
361
|
|
|
553
362
|
// A component or hook reading reactive state or calling an interactive hook runs in the browser too. The promoted analysis lives in this run's map only, so the cached one still reflects the file alone.
|
|
554
|
-
const
|
|
363
|
+
const valueUses: IslandValueUse[] = []
|
|
364
|
+
const promotedWarnings: PromotedWarning[] = []
|
|
365
|
+
const { promoteAll, loadPromoted } = createPromoter(graph, load, issues, valueUses, promotedWarnings)
|
|
555
366
|
promoteAll()
|
|
556
367
|
checkImportedBareSignals(graph, load, root, issues)
|
|
557
368
|
checkCrossFileServerOnlyTags(graph, load, issues)
|
|
@@ -592,7 +403,7 @@ export function analyzeIslands(options: AnalyzeIslandsOptions): IslandPlan {
|
|
|
592
403
|
}
|
|
593
404
|
|
|
594
405
|
const { reach, clientFiles, cssFiles } = walkIslandReach(graph, load, [...rootFiles], clientKeep, options.checkLeaks === true, issues)
|
|
595
|
-
return { targets, pageBindings, staticTags, cssFiles, reach, clientFiles, clientKeep, issues }
|
|
406
|
+
return { targets, pageBindings, staticTags, cssFiles, reach, clientFiles, clientKeep, issues, valueUses, promotedWarnings }
|
|
596
407
|
}
|
|
597
408
|
|
|
598
409
|
function clientKeepOf(targets: Iterable<IslandTarget>, load: LoadModule): Map<string, Set<string>> {
|
package/src/build/manifest.ts
CHANGED
|
@@ -7,13 +7,12 @@ import type { StatusPageManifestEntry } from './status-pages'
|
|
|
7
7
|
import { join } from 'node:path'
|
|
8
8
|
import { isRouteCacheable } from '../compiler/cacheability'
|
|
9
9
|
import { isInside } from './is-inside'
|
|
10
|
-
import {
|
|
10
|
+
import { createReachWalker } from './reach'
|
|
11
11
|
import { ancestorFiles, fileToRoutePath, MIDDLEWARE_NAMES } from './routes'
|
|
12
12
|
import { buildStatusPageEntries } from './status-pages'
|
|
13
13
|
|
|
14
14
|
export type { StatusPageKind, StatusPageManifestEntry } from './status-pages'
|
|
15
15
|
|
|
16
|
-
const REGION_SOURCES = new Set(['prunelle', 'prunelle/internal/ssr'])
|
|
17
16
|
|
|
18
17
|
export interface ActionManifestEntry {
|
|
19
18
|
url: string
|
|
@@ -44,6 +43,8 @@ export interface RouteManifestEntry {
|
|
|
44
43
|
/** The route, a layout or a component it renders imports `<Region>`. */
|
|
45
44
|
usesRegion?: boolean | undefined
|
|
46
45
|
prerender?: boolean | undefined
|
|
46
|
+
/** Prerendered only because `autoPrerender` found it static, so the build may render it at request time instead. */
|
|
47
|
+
autoPrerender?: boolean | undefined
|
|
47
48
|
/** Route response cache policy from `export const cache = ...`. */
|
|
48
49
|
cache?: RouteCacheSpec | undefined
|
|
49
50
|
/** True when static analysis proves the page reads nothing from the request or clock. */
|
|
@@ -87,129 +88,6 @@ export interface BuildManifest {
|
|
|
87
88
|
pluginData?: Record<string, Record<string, unknown>> | undefined
|
|
88
89
|
}
|
|
89
90
|
|
|
90
|
-
interface FileReach {
|
|
91
|
-
/** Island chunks reachable from the file, and the subset loaded eagerly. */
|
|
92
|
-
chunks: Set<string>
|
|
93
|
-
eager: Set<string>
|
|
94
|
-
styles: Set<string>
|
|
95
|
-
/** All reachable source files (`.ts`/`.tsx`) walked from this file. */
|
|
96
|
-
files: Set<string>
|
|
97
|
-
usesRenderContext: boolean
|
|
98
|
-
usesRegion: boolean
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/** Walks what a module renders and imports to find the island chunks, stylesheets and render-context use behind it. */
|
|
102
|
-
function createReachWalker(graph: ModuleGraph, islands: IslandPlan, islandChunks: Map<string, string>, srcCssToUrl: Map<string, string>, islandCssUrl: Map<string, string>) {
|
|
103
|
-
const { analyses } = graph
|
|
104
|
-
|
|
105
|
-
function walkIslands(file: string, seen: Set<string>, reach: FileReach): void {
|
|
106
|
-
if (seen.has(file))
|
|
107
|
-
return
|
|
108
|
-
seen.add(file)
|
|
109
|
-
const analysis = analyses.get(file)
|
|
110
|
-
if (analysis === undefined)
|
|
111
|
-
return
|
|
112
|
-
if (analysis.isInteractive) {
|
|
113
|
-
const chunk = islandChunks.get(`${file}#default`)
|
|
114
|
-
if (chunk !== undefined) {
|
|
115
|
-
reach.chunks.add(chunk)
|
|
116
|
-
if (analysis.defaultHydrate === undefined || analysis.defaultHydrate === 'load')
|
|
117
|
-
reach.eager.add(chunk)
|
|
118
|
-
}
|
|
119
|
-
// Imports rendered only inside the module's islands ship within them; a static component beside an island renders its own.
|
|
120
|
-
if (analysis.staticJsxTags.size === 0)
|
|
121
|
-
return
|
|
122
|
-
}
|
|
123
|
-
for (const [localName, target] of islands.pageBindings.get(file) ?? []) {
|
|
124
|
-
const chunk = islandChunks.get(islandKey(target))
|
|
125
|
-
if (chunk === undefined)
|
|
126
|
-
continue
|
|
127
|
-
reach.chunks.add(chunk)
|
|
128
|
-
const isDeferred = analysis.deferredIslandTags.has(localName)
|
|
129
|
-
|| (target.defaultHydrate !== undefined && target.defaultHydrate !== 'load')
|
|
130
|
-
if (!isDeferred)
|
|
131
|
-
reach.eager.add(chunk)
|
|
132
|
-
}
|
|
133
|
-
for (const dep of graph.localDeps(file))
|
|
134
|
-
walkIslands(dep, seen, reach)
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
function walkStyles(file: string, seen: Set<string>, out: Set<string>): void {
|
|
138
|
-
if (seen.has(file))
|
|
139
|
-
return
|
|
140
|
-
seen.add(file)
|
|
141
|
-
const analysis = analyses.get(file)
|
|
142
|
-
if (analysis === undefined)
|
|
143
|
-
return
|
|
144
|
-
if (analysis.isInteractive) {
|
|
145
|
-
const islCss = islandCssUrl.get(`${file}#default`)
|
|
146
|
-
if (islCss !== undefined)
|
|
147
|
-
out.add(islCss)
|
|
148
|
-
}
|
|
149
|
-
for (const target of islands.pageBindings.get(file)?.values() ?? []) {
|
|
150
|
-
const islCss = islandCssUrl.get(islandKey(target))
|
|
151
|
-
if (islCss !== undefined)
|
|
152
|
-
out.add(islCss)
|
|
153
|
-
walkStyles(target.file, seen, out)
|
|
154
|
-
}
|
|
155
|
-
for (const imp of analysis.imports) {
|
|
156
|
-
const cssUrl = srcCssToUrl.get(graph.resolve(file, imp.source) ?? '')
|
|
157
|
-
if (cssUrl !== undefined)
|
|
158
|
-
out.add(cssUrl)
|
|
159
|
-
}
|
|
160
|
-
for (const dep of graph.localDeps(file))
|
|
161
|
-
walkStyles(dep, seen, out)
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
function walkRenderContext(file: string, seen: Set<string>): boolean {
|
|
165
|
-
if (seen.has(file))
|
|
166
|
-
return false
|
|
167
|
-
seen.add(file)
|
|
168
|
-
const analysis = analyses.get(file)
|
|
169
|
-
if (analysis === undefined)
|
|
170
|
-
return false
|
|
171
|
-
return analysis.usesRenderContext || graph.localDeps(file).some(dep => walkRenderContext(dep, seen))
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
// A namespace import of prunelle may render `NS.Region`, so it counts.
|
|
175
|
-
function walkRegion(file: string, seen: Set<string>): boolean {
|
|
176
|
-
if (seen.has(file))
|
|
177
|
-
return false
|
|
178
|
-
seen.add(file)
|
|
179
|
-
const analysis = analyses.get(file)
|
|
180
|
-
if (analysis === undefined)
|
|
181
|
-
return false
|
|
182
|
-
return analysis.imports.some(imp => REGION_SOURCES.has(imp.source) && imp.specifiers.some(s => s.imported === 'Region' || s.imported === '*'))
|
|
183
|
-
|| graph.localDeps(file).some(dep => walkRegion(dep, seen))
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
function walkFiles(file: string, seen: Set<string>): void {
|
|
187
|
-
if (seen.has(file))
|
|
188
|
-
return
|
|
189
|
-
seen.add(file)
|
|
190
|
-
for (const target of islands.pageBindings.get(file)?.values() ?? [])
|
|
191
|
-
walkFiles(target.file, seen)
|
|
192
|
-
for (const dep of graph.localDeps(file))
|
|
193
|
-
walkFiles(dep, seen)
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
// Layouts are shared by many routes, so each file is walked once.
|
|
197
|
-
const memo = new Map<string, FileReach>()
|
|
198
|
-
return (file: string): FileReach => {
|
|
199
|
-
let reach = memo.get(file)
|
|
200
|
-
if (reach === undefined) {
|
|
201
|
-
reach = { chunks: new Set(), eager: new Set(), styles: new Set(), files: new Set(), usesRenderContext: false, usesRegion: false }
|
|
202
|
-
walkIslands(file, new Set(), reach)
|
|
203
|
-
walkStyles(file, new Set(), reach.styles)
|
|
204
|
-
walkFiles(file, reach.files)
|
|
205
|
-
reach.usesRenderContext = walkRenderContext(file, new Set())
|
|
206
|
-
reach.usesRegion = walkRegion(file, new Set())
|
|
207
|
-
memo.set(file, reach)
|
|
208
|
-
}
|
|
209
|
-
return reach
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
|
|
213
91
|
export interface RouteEntries {
|
|
214
92
|
routes: RouteManifestEntry[]
|
|
215
93
|
statusPages: StatusPageManifestEntry[]
|
|
@@ -293,6 +171,7 @@ export function buildRouteEntries(options: {
|
|
|
293
171
|
usesRenderContext,
|
|
294
172
|
usesRegion,
|
|
295
173
|
prerender: analysis?.hasPrerender || options.isStatic || autoPre,
|
|
174
|
+
...(autoPre && analysis?.hasPrerender !== true && !options.isStatic ? { autoPrerender: true } : {}),
|
|
296
175
|
...(cache !== undefined ? { cache } : {}),
|
|
297
176
|
...(cacheable ? { cacheable: true } : {}),
|
|
298
177
|
}
|
package/src/build/plugin-host.ts
CHANGED
|
@@ -4,7 +4,8 @@ import type { AppRuntimeContext } from '../plugins/context'
|
|
|
4
4
|
import type { BuildPluginContext, FinalizeBuildContext, PrunellePlugin } from '../plugins/index'
|
|
5
5
|
import type { RouteManifestEntry, StatusPageManifestEntry } from './manifest'
|
|
6
6
|
import { existsSync, mkdirSync, writeFileSync } from 'node:fs'
|
|
7
|
-
import { dirname, join } from 'node:path'
|
|
7
|
+
import { dirname, join, resolve } from 'node:path'
|
|
8
|
+
import { isInside } from './is-inside'
|
|
8
9
|
import { precompressFiles } from './precompress'
|
|
9
10
|
|
|
10
11
|
const LEADING_SLASHES_RE = /^\/+/
|
|
@@ -14,6 +15,14 @@ function cleanRel(relPath: string): string {
|
|
|
14
15
|
return relPath.replace(LEADING_SLASHES_RE, '').replace(BACKSLASH_RE, '/')
|
|
15
16
|
}
|
|
16
17
|
|
|
18
|
+
/** The clean relative path of a file a plugin writes under `base`, or a throw naming the plugin when it climbs out of `base`. */
|
|
19
|
+
function confinedRel(plugin: string, what: string, base: string, relPath: string): string {
|
|
20
|
+
const clean = cleanRel(relPath)
|
|
21
|
+
if (!isInside(base, resolve(base, clean)))
|
|
22
|
+
throw new Error(`prunelle build: plugin "${plugin}" ${what}("${relPath}") leaves the output folder: use a relative path without "..".`)
|
|
23
|
+
return clean
|
|
24
|
+
}
|
|
25
|
+
|
|
17
26
|
export interface PublicFiles {
|
|
18
27
|
/** Public files known so far, as relative paths in registration order. */
|
|
19
28
|
files: string[]
|
|
@@ -159,8 +168,8 @@ export function createPluginHost(options: PluginHostOptions) {
|
|
|
159
168
|
const precompressed = new Set<string>()
|
|
160
169
|
const styles = createStyleMutators(styleUrls, globalStyles, routes, statusPages)
|
|
161
170
|
|
|
162
|
-
function emitAsset(relPath: string, content: string | Uint8Array): string {
|
|
163
|
-
const clean =
|
|
171
|
+
function emitAsset(plugin: string, relPath: string, content: string | Uint8Array): string {
|
|
172
|
+
const clean = confinedRel(plugin, 'emitAsset', clientDir, relPath)
|
|
164
173
|
const dst = join(clientDir, clean)
|
|
165
174
|
mkdirSync(dirname(dst), { recursive: true })
|
|
166
175
|
writeFileSync(dst, content)
|
|
@@ -171,8 +180,8 @@ export function createPluginHost(options: PluginHostOptions) {
|
|
|
171
180
|
return `${assetPrefix}/${clean}`
|
|
172
181
|
}
|
|
173
182
|
|
|
174
|
-
function emitPublic(relPath: string, content: string | Uint8Array): string {
|
|
175
|
-
const clean = options.publicFiles.register(relPath)
|
|
183
|
+
function emitPublic(plugin: string, relPath: string, content: string | Uint8Array): string {
|
|
184
|
+
const clean = options.publicFiles.register(confinedRel(plugin, 'emitPublic', outPublicDir, relPath))
|
|
176
185
|
const dst = join(outPublicDir, clean)
|
|
177
186
|
mkdirSync(dirname(dst), { recursive: true })
|
|
178
187
|
writeFileSync(dst, content)
|
|
@@ -193,12 +202,15 @@ export function createPluginHost(options: PluginHostOptions) {
|
|
|
193
202
|
isStatic: options.isStatic,
|
|
194
203
|
dev: options.isDev,
|
|
195
204
|
routes,
|
|
196
|
-
emitAsset,
|
|
197
|
-
emitPublic,
|
|
198
205
|
provide: appCtx.provide,
|
|
199
206
|
inject: appCtx.inject,
|
|
200
207
|
}
|
|
201
208
|
|
|
209
|
+
const emittersFor = (plugin: PrunellePlugin) => ({
|
|
210
|
+
emitAsset: (relPath: string, content: string | Uint8Array) => emitAsset(plugin.name, relPath, content),
|
|
211
|
+
emitPublic: (relPath: string, content: string | Uint8Array) => emitPublic(plugin.name, relPath, content),
|
|
212
|
+
})
|
|
213
|
+
|
|
202
214
|
const buildCtx = {
|
|
203
215
|
...shared,
|
|
204
216
|
...(options.routeSourceFiles !== undefined ? { routeSourceFiles: options.routeSourceFiles } : {}),
|
|
@@ -222,7 +234,7 @@ export function createPluginHost(options: PluginHostOptions) {
|
|
|
222
234
|
async runBuild(): Promise<void> {
|
|
223
235
|
for (const plugin of plugins) {
|
|
224
236
|
if (plugin.build !== undefined) {
|
|
225
|
-
const ctx: BuildPluginContext = { ...buildCtx, setData: setDataFor(plugin) }
|
|
237
|
+
const ctx: BuildPluginContext = { ...buildCtx, ...emittersFor(plugin), setData: setDataFor(plugin) }
|
|
226
238
|
await runPluginHook(plugin, 'build', () => plugin.build!(ctx))
|
|
227
239
|
}
|
|
228
240
|
}
|
|
@@ -236,6 +248,7 @@ export function createPluginHost(options: PluginHostOptions) {
|
|
|
236
248
|
ran = true
|
|
237
249
|
const ctx: FinalizeBuildContext = {
|
|
238
250
|
...shared,
|
|
251
|
+
...emittersFor(plugin),
|
|
239
252
|
...(prerenderDir !== undefined ? { prerenderDir } : {}),
|
|
240
253
|
publicFiles: options.publicFiles.files,
|
|
241
254
|
assets,
|
package/src/build/precompress.ts
CHANGED
|
@@ -10,7 +10,7 @@ import { getAppContext } from '../plugins/context'
|
|
|
10
10
|
/** Brotli quality of the sidecars: written once per build, downloaded by every visitor, so the smallest the encoder makes. */
|
|
11
11
|
export const BROTLI_QUALITY = 11
|
|
12
12
|
|
|
13
|
-
const COMPRESSIBLE_EXTS = new Set(['.html', '.js', '.mjs', '.css', '.svg', '.xml', '.json', '.txt', '.webmanifest', '.wasm'])
|
|
13
|
+
const COMPRESSIBLE_EXTS = new Set(['.html', '.js', '.mjs', '.css', '.svg', '.xml', '.json', '.txt', '.webmanifest', '.wasm', '.ttf', '.otf', '.ico'])
|
|
14
14
|
const STALE_MS = 7 * 86_400_000
|
|
15
15
|
|
|
16
16
|
const compress = promisify(brotliCompress)
|