prunelle 0.1.1 → 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.
Files changed (177) hide show
  1. package/LICENSE +21 -0
  2. package/llms.txt +137 -40
  3. package/package.json +11 -2
  4. package/src/a11y/declaration.ts +373 -0
  5. package/src/a11y/index.ts +477 -0
  6. package/src/a11y/rgaa-map.ts +230 -0
  7. package/src/attrs.ts +11 -3
  8. package/src/build/bundle.ts +89 -41
  9. package/src/build/byte-size.ts +11 -0
  10. package/src/build/client-entries.ts +3 -2
  11. package/src/build/client-plugin.ts +11 -5
  12. package/src/build/client-prune.ts +56 -6
  13. package/src/build/compile.ts +13 -10
  14. package/src/build/ecoindex.ts +238 -0
  15. package/src/build/files.ts +15 -0
  16. package/src/build/first-load.ts +85 -14
  17. package/src/build/graph.ts +29 -39
  18. package/src/build/index.ts +225 -122
  19. package/src/build/island-promote.ts +311 -0
  20. package/src/build/islands.ts +14 -203
  21. package/src/build/manifest.ts +56 -116
  22. package/src/build/output.ts +3 -6
  23. package/src/build/plugin-host.ts +115 -41
  24. package/src/build/precompress.ts +16 -3
  25. package/src/build/prerender.ts +177 -39
  26. package/src/build/reach.ts +200 -0
  27. package/src/build/render-rules.ts +2 -2
  28. package/src/build/report.ts +48 -5
  29. package/src/build/resolve.ts +8 -6
  30. package/src/build/routes.ts +175 -10
  31. package/src/build/static-headers.ts +112 -0
  32. package/src/build/static.ts +139 -39
  33. package/src/build/status-pages.ts +127 -0
  34. package/src/build/style-bundles.ts +526 -0
  35. package/src/cli/check-forms.ts +636 -0
  36. package/src/cli/check-imports.ts +31 -0
  37. package/src/cli/check-links.ts +632 -0
  38. package/src/cli/check-security.ts +64 -0
  39. package/src/cli/check.ts +101 -26
  40. package/src/cli/commands.ts +120 -11
  41. package/src/cli/docs.ts +403 -0
  42. package/src/cli/inspect.ts +105 -15
  43. package/src/cli/mcp-tools.ts +492 -0
  44. package/src/cli/mcp.ts +205 -0
  45. package/src/cli/new.ts +16 -5
  46. package/src/cli/render.ts +416 -0
  47. package/src/cli/upgrade-names.ts +73 -13
  48. package/src/cli/upgrade.ts +156 -22
  49. package/src/compiler/analyze.ts +87 -54
  50. package/src/compiler/browser-globals.ts +140 -17
  51. package/src/compiler/cacheability-private.ts +566 -0
  52. package/src/compiler/cacheability.ts +335 -0
  53. package/src/compiler/compliance-eco.ts +71 -0
  54. package/src/compiler/compliance-rgaa.ts +614 -0
  55. package/src/compiler/compliance-urls.ts +148 -0
  56. package/src/compiler/compliance.ts +407 -352
  57. package/src/compiler/data-keys.ts +199 -192
  58. package/src/compiler/dev-source.ts +87 -0
  59. package/src/compiler/dom-attrs.ts +138 -65
  60. package/src/compiler/dom.ts +517 -442
  61. package/src/compiler/expr-context.ts +260 -81
  62. package/src/compiler/foreign-imports.ts +88 -0
  63. package/src/compiler/mistakes.ts +23 -2
  64. package/src/compiler/props-body.ts +60 -0
  65. package/src/compiler/props.ts +87 -36
  66. package/src/compiler/scope.ts +400 -333
  67. package/src/compiler/server-only.ts +190 -151
  68. package/src/compiler/shared.ts +42 -14
  69. package/src/compiler/signal-reads.ts +6 -2
  70. package/src/compiler/spread-handlers.ts +295 -0
  71. package/src/compiler/ssr-prunelle-tags.ts +27 -14
  72. package/src/compiler/ssr.ts +220 -89
  73. package/src/context.ts +34 -0
  74. package/src/dev/error-log.ts +177 -0
  75. package/src/dev/host-check.ts +32 -0
  76. package/src/dev/index.ts +94 -34
  77. package/src/dev/open-editor.ts +228 -0
  78. package/src/dev/protocol.ts +3 -0
  79. package/src/dev/reload.ts +5 -3
  80. package/src/dev/supervisor.ts +50 -25
  81. package/src/dev/watchers.ts +74 -13
  82. package/src/dom/action.ts +47 -21
  83. package/src/dom/attrs.ts +15 -3
  84. package/src/dom/extras.ts +57 -40
  85. package/src/dom/index.ts +54 -14
  86. package/src/dom/island.ts +102 -68
  87. package/src/dom/loop.ts +66 -32
  88. package/src/eco/declaration.ts +328 -0
  89. package/src/eco/index.ts +69 -0
  90. package/src/index.ts +2 -2
  91. package/src/jsx-runtime.ts +2 -2
  92. package/src/plugins/config.ts +18 -12
  93. package/src/plugins/context.ts +44 -1
  94. package/src/plugins/index.ts +2 -2
  95. package/src/plugins/load.ts +30 -10
  96. package/src/plugins/order.ts +38 -1
  97. package/src/plugins/public.ts +6 -3
  98. package/src/plugins/services.ts +84 -0
  99. package/src/plugins/types.ts +51 -2
  100. package/src/reactivity/graph.ts +10 -0
  101. package/src/reactivity/index.ts +24 -16
  102. package/src/reactivity/selector.ts +31 -4
  103. package/src/reactivity/store.ts +64 -20
  104. package/src/server/action-flash.ts +158 -0
  105. package/src/server/action-protect.ts +199 -0
  106. package/src/server/action-registry.ts +1 -1
  107. package/src/server/action.ts +188 -152
  108. package/src/server/app-secret.ts +63 -0
  109. package/src/server/assets.ts +34 -2
  110. package/src/server/compress.ts +96 -10
  111. package/src/server/consent.ts +59 -22
  112. package/src/server/cookies.ts +16 -2
  113. package/src/server/document.ts +25 -10
  114. package/src/server/env.ts +14 -0
  115. package/src/server/errors.ts +308 -65
  116. package/src/server/form-schema.ts +337 -0
  117. package/src/server/health.ts +275 -0
  118. package/src/server/http.ts +186 -39
  119. package/src/server/index.ts +272 -273
  120. package/src/server/loader.ts +15 -2
  121. package/src/server/page.ts +15 -0
  122. package/src/server/param-matchers.ts +112 -0
  123. package/src/server/plugin-host.ts +125 -88
  124. package/src/server/public.ts +6 -5
  125. package/src/server/rate-limit.ts +102 -36
  126. package/src/server/request-body.ts +1 -1
  127. package/src/server/route-cache.ts +541 -0
  128. package/src/server/route-table.ts +356 -11
  129. package/src/server/router.ts +125 -40
  130. package/src/server/routing.ts +112 -63
  131. package/src/server/sanitize-html.ts +309 -0
  132. package/src/server/security.ts +279 -52
  133. package/src/server/serve-handlers.ts +31 -7
  134. package/src/server/standard-schema.ts +17 -0
  135. package/src/server/start.ts +60 -16
  136. package/src/server/static-dir.ts +91 -12
  137. package/src/server/stream.ts +68 -27
  138. package/src/ssr/consent-banner.ts +58 -0
  139. package/src/ssr/context.ts +106 -8
  140. package/src/ssr/index.ts +151 -4
  141. package/src/ssr/navigation.ts +2 -2
  142. package/src/ssr/region.ts +40 -19
  143. package/src/style.ts +64 -1
  144. package/src/test/browser.ts +28 -13
  145. package/src/test/index.ts +97 -12
  146. package/src/ts-plugin/diagnostics.ts +505 -0
  147. package/src/ts-plugin/fixes.ts +364 -0
  148. package/src/ts-plugin/index.ts +194 -0
  149. package/templates/_shared/AGENTS.md +2 -2
  150. package/templates/_shared/gitignore +5 -0
  151. package/templates/_shared/mcp.json +11 -0
  152. package/templates/_shared/src/routes/legal.tsx +15 -1
  153. package/templates/_shared/tsconfig.json +5 -0
  154. package/templates/app/.env.example +4 -0
  155. package/templates/app/package.json +2 -1
  156. package/templates/app/prunelle.config.ts +2 -0
  157. package/templates/app/src/auth.ts +4 -6
  158. package/templates/app/src/mail.ts +4 -0
  159. package/templates/app/src/routes/_layout.tsx +9 -1
  160. package/templates/app/src/routes/dashboard/index.tsx +42 -8
  161. package/templates/app/src/routes/login.tsx +49 -30
  162. package/templates/app/src/routes/privacy.tsx +25 -0
  163. package/templates/app/src/styles/app.css +8 -1
  164. package/templates/app/tests/app.test.ts +44 -3
  165. package/templates/blog/package.json +1 -1
  166. package/templates/blog/prunelle.config.ts +1 -1
  167. package/templates/blog/src/routes/_layout.tsx +6 -1
  168. package/templates/blog/src/routes/index.tsx +5 -1
  169. package/templates/blog/src/styles/app.css +6 -1
  170. package/templates/fullstack/package.json +1 -1
  171. package/templates/fullstack/src/components/GreetForm.tsx +2 -1
  172. package/templates/fullstack/src/routes/_layout.tsx +4 -0
  173. package/templates/fullstack/src/styles/app.css +11 -1
  174. package/templates/minimal/package.json +1 -1
  175. package/templates/minimal/src/routes/_layout.tsx +4 -0
  176. package/templates/minimal/src/styles/app.css +11 -1
  177. package/ts-plugin.cjs +4277 -0
@@ -0,0 +1,311 @@
1
+ // Which modules run in the browser: following re-exports to the binding they name, and promoting what reads an interactive import.
2
+
3
+ import type { ModuleAnalysis } from '../compiler/analyze'
4
+ import type { ComplianceConfig } from '../compiler/compliance'
5
+ import type { CompilerWarning } from '../compiler/mistakes'
6
+ import type { IslandIssue } from './islands'
7
+ import type { ModuleGraph } from './graph'
8
+ import { wholeLoadShippedWarning } from '../compiler/analyze'
9
+ import { isIgnoredAt, isRuleIgnored, parsePrunelleIgnores } from '../compiler/compliance'
10
+ import { withStateReads } from '../compiler/interactivity'
11
+ import { walkScoped } from '../compiler/scope'
12
+ import { formatDiagnostic, formatLoc } from '../compiler/shared'
13
+ import { isInNodeModules, isTsFile, relPosix } from './files'
14
+
15
+ export type LoadModule = (file: string) => ModuleAnalysis | undefined
16
+
17
+ export const MAX_REEXPORT_DEPTH = 8
18
+
19
+ /** Follows `export { x } from './y'` and `export * from './y'` chains using `load` so parse errors are never swallowed. */
20
+ export function resolveExportTarget(
21
+ graph: ModuleGraph,
22
+ load: LoadModule,
23
+ file: string,
24
+ exportName: string,
25
+ depth = 0,
26
+ seen = new Set<string>(),
27
+ ): { file: string, exportName: string } | undefined {
28
+ if (depth > MAX_REEXPORT_DEPTH)
29
+ return { file, exportName }
30
+ const key = `${file}\0${exportName}`
31
+ if (seen.has(key))
32
+ return { file, exportName }
33
+ seen.add(key)
34
+
35
+ const analysis = load(file)
36
+ if (analysis === undefined)
37
+ return undefined
38
+ const direct = analysis.reexports.find(r => r.exported === exportName)
39
+ if (direct !== undefined) {
40
+ const next = graph.resolve(file, direct.source)
41
+ if (next !== undefined && isTsFile(next))
42
+ return resolveExportTarget(graph, load, next, direct.local, depth + 1, seen)
43
+ }
44
+
45
+ if (analysis.exportedNames.includes(exportName))
46
+ return { file, exportName }
47
+
48
+ // Follow `export * from './y'` barrel re-exports (named exports only, per ESM spec `export *` never re-exports `default`).
49
+ if (exportName !== 'default') {
50
+ for (const re of analysis.reexports) {
51
+ if (re.exported !== '*')
52
+ continue
53
+ const next = graph.resolve(file, re.source)
54
+ if (next === undefined || !isTsFile(next))
55
+ continue
56
+ const candidate = resolveExportTarget(graph, load, next, exportName, depth + 1, seen)
57
+ if (candidate !== undefined && load(candidate.file)?.exportedNames.includes(candidate.exportName))
58
+ return candidate
59
+ }
60
+ }
61
+
62
+ return { file, exportName }
63
+ }
64
+
65
+ export function moduleHasInteractiveExport(graph: ModuleGraph, load: LoadModule, file: string, seen = new Set<string>()): boolean {
66
+ if (seen.has(file))
67
+ return false
68
+ seen.add(file)
69
+ const analysis = graph.analyses.get(file) ?? load(file)
70
+ if (analysis === undefined)
71
+ return false
72
+ if (analysis.stateExports.length > 0 || [...analysis.exports.values()].some(e => e.interactive))
73
+ return true
74
+ for (const re of analysis.reexports) {
75
+ const next = graph.resolve(file, re.source)
76
+ if (next === undefined || !isTsFile(next))
77
+ continue
78
+ if (re.exported === '*') {
79
+ if (moduleHasInteractiveExport(graph, load, next, seen))
80
+ return true
81
+ }
82
+ else {
83
+ const target = resolveExportTarget(graph, load, next, re.local)
84
+ const targetAnalysis = target === undefined ? undefined : (graph.analyses.get(target.file) ?? load(target.file))
85
+ if (target !== undefined && (targetAnalysis?.stateExports.includes(target.exportName) === true || targetAnalysis?.exports.get(target.exportName)?.interactive === true))
86
+ return true
87
+ }
88
+ }
89
+ return false
90
+ }
91
+
92
+ export function collectExportedSignals(graph: ModuleGraph, load: LoadModule, file: string, out: Set<string>, seen = new Set<string>()): void {
93
+ if (seen.has(file))
94
+ return
95
+ seen.add(file)
96
+ const analysis = graph.analyses.get(file) ?? load(file)
97
+ if (analysis === undefined)
98
+ return
99
+ for (const name of analysis.signalExports)
100
+ out.add(name)
101
+ for (const re of analysis.reexports) {
102
+ const next = graph.resolve(file, re.source)
103
+ if (next === undefined || !isTsFile(next))
104
+ continue
105
+ if (re.exported === '*') {
106
+ collectExportedSignals(graph, load, next, out, seen)
107
+ }
108
+ else {
109
+ const target = resolveExportTarget(graph, load, next, re.local)
110
+ if (target !== undefined && (graph.analyses.get(target.file) ?? load(target.file))?.signalExports.includes(target.exportName) === true)
111
+ out.add(re.exported)
112
+ }
113
+ }
114
+ }
115
+
116
+ function interactivityChanged(prev: ModuleAnalysis, next: { isInteractive: boolean, exports: Map<string, { interactive: boolean }>, localIslands: Map<string, unknown> }): boolean {
117
+ if (prev.isInteractive !== next.isInteractive)
118
+ return true
119
+ for (const [name, fact] of next.exports) {
120
+ if (prev.exports.get(name)?.interactive !== fact.interactive)
121
+ return true
122
+ }
123
+ return next.localIslands.size !== prev.localIslands.size || [...next.localIslands.keys()].some(name => !prev.localIslands.has(name))
124
+ }
125
+
126
+ /** An imported island a module reads as a value (`const D = Counter`, `{ C: Counter }`, `c={Counter}`), which makes what holds it an island. */
127
+ export interface IslandValueUse { file: string, offset: number, offsets?: readonly number[] | undefined, name: string }
128
+
129
+ /** A compiler warning that only applies once a module is promoted to an island across files. */
130
+ export interface PromotedWarning { file: string, warning: CompilerWarning }
131
+
132
+ const COMPONENT_NAME_RE = /^[A-Z]/
133
+
134
+ // Every binding's value read of `local`, in source order.
135
+ const allValueReads = (analysis: ModuleAnalysis, local: string): number[] =>
136
+ analysis.bindings.map(b => b.valueRefs.get(local)).filter((offset): offset is number => offset !== undefined)
137
+
138
+ // All unshadowed non-JSX value reads of `local` in `analysis.program`, used only when a line-ignore hides the first recorded offset.
139
+ function allProgramValueReads(analysis: ModuleAnalysis, local: string): number[] {
140
+ const offsets: number[] = []
141
+ for (const stmt of analysis.program.body) {
142
+ if (stmt.type === 'ImportDeclaration' || stmt.type === 'ExportAllDeclaration')
143
+ continue
144
+ const target = stmt.type === 'ExportNamedDeclaration' ? stmt.declaration : stmt
145
+ if (target === null)
146
+ continue
147
+ walkScoped(target, {
148
+ onIdentifier(id) {
149
+ if (id.name === local)
150
+ offsets.push(id.start)
151
+ },
152
+ })
153
+ }
154
+ return offsets
155
+ }
156
+
157
+ /** Formats `[rgesn/island-by-value]` warnings for `prunelle check` and production `prunelle build`, honoring `compliance.ignore` and `prunelle-ignore`. */
158
+ export function formatIslandValueWarnings(
159
+ uses: readonly IslandValueUse[],
160
+ root: string,
161
+ graph: Pick<ModuleGraph, 'analyses'>,
162
+ sourceOf: (file: string) => string,
163
+ compliance: ComplianceConfig | undefined,
164
+ ): string[] {
165
+ const rule = 'rgesn/island-by-value'
166
+ if (uses.length === 0 || isRuleIgnored(rule, compliance))
167
+ return []
168
+ const ignoresByFile = new Map<string, ReturnType<typeof parsePrunelleIgnores>>()
169
+ const warnings: string[] = []
170
+ for (const use of uses) {
171
+ const code = sourceOf(use.file)
172
+ let ignores = ignoresByFile.get(use.file)
173
+ if (ignores === undefined) {
174
+ ignores = parsePrunelleIgnores(code)
175
+ ignoresByFile.set(use.file, ignores)
176
+ }
177
+ if (ignores.fileRules.has(rule))
178
+ continue
179
+ const recorded = use.offsets ?? [use.offset]
180
+ let offset = recorded.find(o => !isIgnoredAt(ignores, rule, o))
181
+ if (offset === undefined) {
182
+ const analysis = graph.analyses.get(use.file)
183
+ if (analysis !== undefined)
184
+ offset = allProgramValueReads(analysis, use.name).find(o => !isIgnoredAt(ignores, rule, o))
185
+ }
186
+ if (offset === undefined)
187
+ continue
188
+ const rel = relPosix(root, use.file)
189
+ warnings.push(formatDiagnostic(formatLoc(rel, code, offset), `[${rule}] ${use.name} is an island read as a value, not rendered as <${use.name} />, so what holds it hydrates as an island and ships its code too: render <${use.name} /> itself where it shows, or // prunelle-ignore ${rule} when the whole component should hydrate.`, 'warning'))
190
+ }
191
+ return warnings
192
+ }
193
+
194
+ export function createPromoter(
195
+ graph: ModuleGraph,
196
+ load: LoadModule,
197
+ issues: IslandIssue[],
198
+ valueUses: IslandValueUse[] = [],
199
+ promotedWarnings: PromotedWarning[] = [],
200
+ ): { promoteAll: () => void, loadPromoted: LoadModule } {
201
+ const initialAsyncInteractive = new Set<string>()
202
+ const initialServerOnly = new Set<string>()
203
+ const reportedAsync = new Set<string>()
204
+ const reportedServerOnly = new Set<string>()
205
+ const reportedWholeLoad = new Set<string>()
206
+
207
+ const recordInitial = (file: string, a: ModuleAnalysis): void => {
208
+ if (a.hasAsyncDefault && a.exports.get('default')?.interactive === true)
209
+ initialAsyncInteractive.add(file)
210
+ if (a.serverOnly !== undefined)
211
+ initialServerOnly.add(file)
212
+ }
213
+ for (const [file, a] of graph.analyses)
214
+ recordInitial(file, a)
215
+
216
+ // What an import names: reactive state, an interactive export (a component or a hook), or neither.
217
+ const interactiveKind = (file: string, source: string, imported: string): 'state' | 'export' | undefined => {
218
+ const resolved = graph.resolve(file, source)
219
+ if (resolved === undefined || !isTsFile(resolved))
220
+ return undefined
221
+ if (imported === '*')
222
+ return moduleHasInteractiveExport(graph, load, resolved) ? 'export' : undefined
223
+ const target = resolveExportTarget(graph, load, resolved, imported)
224
+ if (target === undefined)
225
+ return undefined
226
+ const targetAnalysis = graph.analyses.get(target.file) ?? load(target.file)
227
+ if (targetAnalysis?.stateExports.includes(target.exportName) === true)
228
+ return 'state'
229
+ return targetAnalysis?.exports.get(target.exportName)?.interactive === true ? 'export' : undefined
230
+ }
231
+ const isInteractiveTarget = (file: string, source: string, imported: string): boolean => interactiveKind(file, source, imported) !== undefined
232
+
233
+ const reportedValueUses = new Set<string>()
234
+ const recordValueUses = (file: string, analysis: ModuleAnalysis): void => {
235
+ for (const imp of analysis.imports) {
236
+ for (const spec of imp.specifiers) {
237
+ const offsets = spec.imported === '*' || !COMPONENT_NAME_RE.test(spec.local) ? [] : allValueReads(analysis, spec.local)
238
+ if (offsets.length === 0 || reportedValueUses.has(`${file}#${spec.local}`) || interactiveKind(file, imp.source, spec.imported) !== 'export')
239
+ continue
240
+ reportedValueUses.add(`${file}#${spec.local}`)
241
+ valueUses.push({ file, offset: offsets[0]!, offsets, name: spec.local })
242
+ }
243
+ }
244
+ }
245
+
246
+ const promoteOne = (file: string): boolean => {
247
+ if (isInNodeModules(file) || !isTsFile(file))
248
+ return false
249
+ const analysis = graph.analyses.get(file)
250
+ if (analysis === undefined)
251
+ return false
252
+ const promoted = withStateReads(analysis, (imp, imported) => isInteractiveTarget(file, imp.source, imported))
253
+ if (promoted === undefined)
254
+ return false
255
+ const page = promoted.exports.get('default')
256
+ if (analysis.hasAsyncDefault && page?.interactive === true) {
257
+ if (!initialAsyncInteractive.has(file) && !reportedAsync.has(file)) {
258
+ reportedAsync.add(file)
259
+ issues.push({ kind: 'async-interactive', file, offset: page.offset ?? 0, reason: page.reason })
260
+ }
261
+ return false
262
+ }
263
+ if (promoted.serverOnly !== undefined) {
264
+ if (!initialServerOnly.has(file) && !reportedServerOnly.has(file)) {
265
+ reportedServerOnly.add(file)
266
+ issues.push({ kind: 'server-only-tag', file, serverOnly: promoted.serverOnly })
267
+ }
268
+ return false
269
+ }
270
+ if (!interactivityChanged(analysis, promoted))
271
+ return false
272
+ recordValueUses(file, analysis)
273
+ let nextWarnings = analysis.warnings
274
+ if (analysis.exports.get('default')?.interactive !== true && page?.interactive === true && analysis.hasLoadExport && analysis.defaultDataKeys === undefined && !analysis.exportedNames.includes('clientData') && !reportedWholeLoad.has(file)) {
275
+ reportedWholeLoad.add(file)
276
+ const defaultName = analysis.exportLocals.get('default')
277
+ const start = analysis.bindings.find(b => b.name === defaultName)?.start ?? page.offset ?? 0
278
+ const warning = wholeLoadShippedWarning(file, start)
279
+ promotedWarnings.push({ file, warning })
280
+ nextWarnings = [...analysis.warnings, warning]
281
+ }
282
+ Object.assign(analysis, promoted, { warnings: nextWarnings })
283
+ return true
284
+ }
285
+
286
+ const promoteAll = (): void => {
287
+ for (let pass = 0; pass < MAX_REEXPORT_DEPTH; pass++) {
288
+ let changed = false
289
+ for (const file of [...graph.analyses.keys()]) {
290
+ if (promoteOne(file))
291
+ changed = true
292
+ }
293
+ if (!changed)
294
+ break
295
+ }
296
+ }
297
+
298
+ const loadPromoted: LoadModule = (file: string): ModuleAnalysis | undefined => {
299
+ const existed = graph.analyses.has(file)
300
+ const analysis = graph.analyses.get(file) ?? load(file)
301
+ if (analysis === undefined)
302
+ return undefined
303
+ if (!existed) {
304
+ recordInitial(file, analysis)
305
+ promoteAll()
306
+ }
307
+ return graph.analyses.get(file) ?? analysis
308
+ }
309
+
310
+ return { promoteAll, loadPromoted }
311
+ }
@@ -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, 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,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
- type LoadModule = (file: string) => ModuleAnalysis | undefined
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))
@@ -526,8 +335,8 @@ export function analyzeIslands(options: AnalyzeIslandsOptions): IslandPlan {
526
335
  const load: LoadModule = options.load ?? (file => graph.load(file))
527
336
  const issues: IslandIssue[] = []
528
337
  const targets = new Map<string, IslandTarget>()
529
- // Reserve 'runtime' so an island named Runtime.tsx never overwrites entriesDir/runtime.ts.
530
- const usedIds = new Set<string>(['runtime'])
338
+ // Reserve 'runtime' and 'regions' so an island named Runtime.tsx or Regions.tsx never overwrites entriesDir/runtime.ts or entriesDir/regions.ts.
339
+ const usedIds = new Set<string>(['runtime', 'regions'])
531
340
  const pageSet = new Set(pageFiles)
532
341
 
533
342
  function registerIsland(file: string, exportName: string, local?: string): IslandTarget {
@@ -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 { promoteAll, loadPromoted } = createPromoter(graph, load, issues)
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>> {