prunelle 0.6.0 → 0.7.0

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.
Files changed (128) hide show
  1. package/LICENSE +21 -0
  2. package/llms.txt +48 -43
  3. package/package.json +4 -1
  4. package/src/a11y/declaration.ts +106 -72
  5. package/src/a11y/index.ts +59 -39
  6. package/src/a11y/rgaa-map.ts +95 -49
  7. package/src/build/bundle.ts +8 -4
  8. package/src/build/byte-size.ts +11 -0
  9. package/src/build/client-plugin.ts +3 -1
  10. package/src/build/compile.ts +2 -1
  11. package/src/build/ecoindex.ts +15 -14
  12. package/src/build/files.ts +6 -0
  13. package/src/build/first-load.ts +83 -11
  14. package/src/build/graph.ts +29 -39
  15. package/src/build/index.ts +57 -18
  16. package/src/build/island-promote.ts +229 -0
  17. package/src/build/islands.ts +9 -201
  18. package/src/build/manifest.ts +4 -125
  19. package/src/build/plugin-host.ts +21 -8
  20. package/src/build/precompress.ts +1 -1
  21. package/src/build/prerender.ts +97 -44
  22. package/src/build/reach.ts +136 -0
  23. package/src/build/render-rules.ts +2 -2
  24. package/src/build/report.ts +19 -3
  25. package/src/build/static-headers.ts +93 -0
  26. package/src/build/static.ts +2 -0
  27. package/src/cli/check-forms.ts +97 -48
  28. package/src/cli/check-imports.ts +25 -0
  29. package/src/cli/check-links.ts +2 -2
  30. package/src/cli/check-security.ts +64 -0
  31. package/src/cli/check.ts +33 -7
  32. package/src/cli/commands.ts +35 -7
  33. package/src/cli/docs.ts +4 -3
  34. package/src/cli/inspect.ts +2 -1
  35. package/src/cli/mcp-tools.ts +7 -5
  36. package/src/cli/mcp.ts +1 -1
  37. package/src/cli/new.ts +14 -3
  38. package/src/cli/render.ts +14 -19
  39. package/src/cli/upgrade.ts +22 -5
  40. package/src/compiler/analyze.ts +26 -5
  41. package/src/compiler/cacheability.ts +2 -2
  42. package/src/compiler/compliance-eco.ts +71 -0
  43. package/src/compiler/compliance-rgaa.ts +110 -4
  44. package/src/compiler/compliance-urls.ts +145 -0
  45. package/src/compiler/compliance.ts +168 -116
  46. package/src/compiler/foreign-imports.ts +88 -0
  47. package/src/compiler/mistakes.ts +19 -1
  48. package/src/compiler/props-body.ts +52 -0
  49. package/src/compiler/shared.ts +2 -2
  50. package/src/compiler/ssr.ts +2 -2
  51. package/src/context.ts +34 -0
  52. package/src/dev/error-log.ts +90 -64
  53. package/src/dev/host-check.ts +26 -0
  54. package/src/dev/index.ts +12 -5
  55. package/src/dev/open-editor.ts +15 -11
  56. package/src/dev/protocol.ts +2 -0
  57. package/src/dev/supervisor.ts +8 -0
  58. package/src/dev/watchers.ts +27 -2
  59. package/src/dom/action.ts +4 -1
  60. package/src/dom/extras.ts +1 -2
  61. package/src/dom/index.ts +13 -2
  62. package/src/dom/island.ts +22 -10
  63. package/src/dom/loop.ts +25 -16
  64. package/src/eco/declaration.ts +257 -0
  65. package/src/eco/index.ts +65 -0
  66. package/src/index.ts +2 -2
  67. package/src/jsx-runtime.ts +2 -2
  68. package/src/plugins/config.ts +1 -1
  69. package/src/plugins/context.ts +16 -1
  70. package/src/plugins/load.ts +4 -3
  71. package/src/plugins/public.ts +2 -2
  72. package/src/plugins/services.ts +3 -0
  73. package/src/plugins/types.ts +17 -2
  74. package/src/server/action-flash.ts +158 -0
  75. package/src/server/action-protect.ts +38 -37
  76. package/src/server/action.ts +11 -127
  77. package/src/server/app-secret.ts +63 -0
  78. package/src/server/compress.ts +5 -1
  79. package/src/server/consent.ts +59 -22
  80. package/src/server/cookies.ts +16 -2
  81. package/src/server/env.ts +14 -0
  82. package/src/server/errors.ts +3 -1
  83. package/src/server/form-schema.ts +1 -1
  84. package/src/server/health.ts +46 -15
  85. package/src/server/http.ts +47 -7
  86. package/src/server/index.ts +52 -30
  87. package/src/server/loader.ts +6 -2
  88. package/src/server/plugin-host.ts +3 -1
  89. package/src/server/public.ts +4 -3
  90. package/src/server/rate-limit.ts +52 -9
  91. package/src/server/route-cache.ts +26 -4
  92. package/src/server/route-table.ts +7 -3
  93. package/src/server/routing.ts +3 -87
  94. package/src/server/sanitize-html.ts +309 -0
  95. package/src/server/security.ts +71 -20
  96. package/src/server/serve-handlers.ts +14 -4
  97. package/src/server/start.ts +1 -1
  98. package/src/server/static-dir.ts +27 -4
  99. package/src/server/stream.ts +28 -6
  100. package/src/ssr/consent-banner.ts +58 -0
  101. package/src/ssr/context.ts +20 -4
  102. package/src/ssr/index.ts +18 -0
  103. package/src/ssr/navigation.ts +2 -2
  104. package/src/style.ts +32 -1
  105. package/templates/_shared/gitignore +5 -0
  106. package/templates/_shared/src/routes/legal.tsx +10 -1
  107. package/templates/app/.env.example +4 -0
  108. package/templates/app/package.json +2 -1
  109. package/templates/app/prunelle.config.ts +2 -0
  110. package/templates/app/src/auth.ts +4 -6
  111. package/templates/app/src/mail.ts +4 -0
  112. package/templates/app/src/routes/_layout.tsx +9 -1
  113. package/templates/app/src/routes/dashboard/index.tsx +42 -8
  114. package/templates/app/src/routes/login.tsx +49 -30
  115. package/templates/app/src/routes/privacy.tsx +25 -0
  116. package/templates/app/src/styles/app.css +8 -1
  117. package/templates/app/tests/app.test.ts +44 -3
  118. package/templates/blog/package.json +1 -1
  119. package/templates/blog/src/routes/_layout.tsx +6 -1
  120. package/templates/blog/src/routes/index.tsx +1 -1
  121. package/templates/blog/src/styles/app.css +6 -1
  122. package/templates/fullstack/package.json +1 -1
  123. package/templates/fullstack/src/components/GreetForm.tsx +2 -1
  124. package/templates/fullstack/src/routes/_layout.tsx +4 -0
  125. package/templates/fullstack/src/styles/app.css +11 -1
  126. package/templates/minimal/package.json +1 -1
  127. package/templates/minimal/src/routes/_layout.tsx +4 -0
  128. package/templates/minimal/src/styles/app.css +11 -1
@@ -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 } from './island-promote'
6
7
  import { basename, extname } from 'node:path'
7
8
  import { asyncInteractiveError } from '../compiler/analyze'
8
- import { findCrossFileServerOnly, withStateReads } from '../compiler/interactivity'
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,8 @@ 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[]
56
60
  }
57
61
 
58
62
  export interface AnalyzeIslandsOptions {
@@ -68,9 +72,7 @@ export interface AnalyzeIslandsOptions {
68
72
  checkLeaks?: boolean | undefined
69
73
  }
70
74
 
71
- type LoadModule = (file: string) => ModuleAnalysis | undefined
72
-
73
- const MAX_REEXPORT_DEPTH = 8
75
+ const EMPTY_SET = new Set<string>()
74
76
 
75
77
  export function islandKey(target: { file: string, exportName: string }): string {
76
78
  return `${target.file}#${target.exportName}`
@@ -85,201 +87,6 @@ export function clientLeakError(leaks: Set<string>): Error {
85
87
  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
88
  }
87
89
 
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
90
  function checkImportedBareSignals(graph: ModuleGraph, load: LoadModule, root: string, issues: IslandIssue[]): void {
284
91
  for (const [file, analysis] of [...graph.analyses]) {
285
92
  if (isInNodeModules(file) || !isTsFile(file))
@@ -551,7 +358,8 @@ export function analyzeIslands(options: AnalyzeIslandsOptions): IslandPlan {
551
358
  }
552
359
 
553
360
  // 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 { promoteAll, loadPromoted } = createPromoter(graph, load, issues)
361
+ const valueUses: IslandValueUse[] = []
362
+ const { promoteAll, loadPromoted } = createPromoter(graph, load, issues, valueUses)
555
363
  promoteAll()
556
364
  checkImportedBareSignals(graph, load, root, issues)
557
365
  checkCrossFileServerOnlyTags(graph, load, issues)
@@ -592,7 +400,7 @@ export function analyzeIslands(options: AnalyzeIslandsOptions): IslandPlan {
592
400
  }
593
401
 
594
402
  const { reach, clientFiles, cssFiles } = walkIslandReach(graph, load, [...rootFiles], clientKeep, options.checkLeaks === true, issues)
595
- return { targets, pageBindings, staticTags, cssFiles, reach, clientFiles, clientKeep, issues }
403
+ return { targets, pageBindings, staticTags, cssFiles, reach, clientFiles, clientKeep, issues, valueUses }
596
404
  }
597
405
 
598
406
  function clientKeepOf(targets: Iterable<IslandTarget>, load: LoadModule): Map<string, Set<string>> {
@@ -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 { islandKey } from './islands'
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
  }
@@ -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 = cleanRel(relPath)
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,
@@ -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)