prunelle 0.1.1 → 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.
- package/LICENSE +21 -0
- package/llms.txt +137 -40
- package/package.json +11 -2
- package/src/a11y/declaration.ts +369 -0
- package/src/a11y/index.ts +466 -0
- package/src/a11y/rgaa-map.ts +230 -0
- package/src/attrs.ts +11 -3
- package/src/build/bundle.ts +88 -40
- package/src/build/byte-size.ts +11 -0
- package/src/build/client-entries.ts +3 -2
- package/src/build/client-plugin.ts +11 -5
- package/src/build/client-prune.ts +56 -6
- package/src/build/compile.ts +13 -10
- package/src/build/ecoindex.ts +230 -0
- 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 +218 -113
- package/src/build/island-promote.ts +229 -0
- package/src/build/islands.ts +11 -203
- package/src/build/manifest.ts +56 -116
- package/src/build/output.ts +2 -2
- package/src/build/plugin-host.ts +113 -41
- package/src/build/precompress.ts +16 -3
- package/src/build/prerender.ts +173 -38
- package/src/build/reach.ts +136 -0
- package/src/build/render-rules.ts +2 -2
- package/src/build/report.ts +42 -5
- package/src/build/resolve.ts +8 -6
- package/src/build/routes.ts +175 -10
- package/src/build/static-headers.ts +93 -0
- package/src/build/static.ts +139 -39
- package/src/build/status-pages.ts +127 -0
- package/src/build/style-bundles.ts +526 -0
- package/src/cli/check-forms.ts +610 -0
- package/src/cli/check-imports.ts +25 -0
- package/src/cli/check-links.ts +632 -0
- package/src/cli/check-security.ts +64 -0
- package/src/cli/check.ts +107 -26
- package/src/cli/commands.ts +120 -11
- package/src/cli/docs.ts +403 -0
- package/src/cli/inspect.ts +105 -15
- package/src/cli/mcp-tools.ts +492 -0
- package/src/cli/mcp.ts +205 -0
- package/src/cli/new.ts +16 -5
- package/src/cli/render.ts +416 -0
- package/src/cli/upgrade-names.ts +73 -13
- package/src/cli/upgrade.ts +156 -18
- package/src/compiler/analyze.ts +68 -14
- package/src/compiler/browser-globals.ts +140 -17
- package/src/compiler/cacheability.ts +636 -0
- package/src/compiler/compliance-eco.ts +71 -0
- package/src/compiler/compliance-rgaa.ts +614 -0
- package/src/compiler/compliance-urls.ts +145 -0
- package/src/compiler/compliance.ts +355 -351
- package/src/compiler/data-keys.ts +199 -192
- package/src/compiler/dev-source.ts +87 -0
- package/src/compiler/dom-attrs.ts +138 -65
- package/src/compiler/dom.ts +517 -442
- package/src/compiler/expr-context.ts +167 -57
- package/src/compiler/foreign-imports.ts +88 -0
- package/src/compiler/mistakes.ts +23 -2
- package/src/compiler/props-body.ts +52 -0
- package/src/compiler/props.ts +87 -36
- package/src/compiler/scope.ts +400 -333
- package/src/compiler/server-only.ts +190 -151
- package/src/compiler/shared.ts +42 -14
- package/src/compiler/signal-reads.ts +6 -2
- package/src/compiler/ssr-prunelle-tags.ts +27 -14
- package/src/compiler/ssr.ts +220 -89
- package/src/context.ts +34 -0
- package/src/dev/error-log.ts +177 -0
- package/src/dev/host-check.ts +26 -0
- package/src/dev/index.ts +91 -34
- package/src/dev/open-editor.ts +208 -0
- package/src/dev/protocol.ts +3 -0
- package/src/dev/reload.ts +5 -3
- package/src/dev/supervisor.ts +50 -25
- package/src/dev/watchers.ts +74 -13
- package/src/dom/action.ts +47 -21
- package/src/dom/attrs.ts +15 -3
- package/src/dom/extras.ts +29 -14
- package/src/dom/index.ts +39 -10
- package/src/dom/island.ts +54 -35
- package/src/dom/loop.ts +33 -23
- 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 +8 -7
- package/src/plugins/context.ts +16 -1
- package/src/plugins/index.ts +2 -2
- package/src/plugins/load.ts +30 -10
- package/src/plugins/order.ts +38 -1
- package/src/plugins/public.ts +6 -3
- package/src/plugins/services.ts +84 -0
- package/src/plugins/types.ts +51 -2
- package/src/reactivity/graph.ts +10 -0
- package/src/reactivity/index.ts +24 -16
- package/src/reactivity/selector.ts +31 -4
- package/src/reactivity/store.ts +45 -13
- package/src/server/action-flash.ts +158 -0
- package/src/server/action-protect.ts +199 -0
- package/src/server/action-registry.ts +1 -1
- package/src/server/action.ts +56 -117
- package/src/server/app-secret.ts +63 -0
- package/src/server/assets.ts +34 -2
- package/src/server/compress.ts +96 -10
- package/src/server/consent.ts +59 -22
- package/src/server/cookies.ts +16 -2
- package/src/server/document.ts +25 -10
- package/src/server/env.ts +14 -0
- package/src/server/errors.ts +305 -65
- package/src/server/form-schema.ts +343 -0
- package/src/server/health.ts +275 -0
- package/src/server/http.ts +177 -38
- package/src/server/index.ts +264 -272
- package/src/server/loader.ts +11 -2
- package/src/server/param-matchers.ts +112 -0
- package/src/server/plugin-host.ts +125 -88
- package/src/server/public.ts +6 -5
- package/src/server/rate-limit.ts +102 -36
- package/src/server/request-body.ts +1 -1
- package/src/server/route-cache.ts +445 -0
- package/src/server/route-table.ts +344 -9
- package/src/server/router.ts +125 -40
- package/src/server/routing.ts +112 -63
- package/src/server/sanitize-html.ts +309 -0
- package/src/server/security.ts +277 -50
- package/src/server/serve-handlers.ts +31 -7
- package/src/server/start.ts +60 -16
- package/src/server/static-dir.ts +87 -11
- package/src/server/stream.ts +68 -27
- package/src/ssr/consent-banner.ts +58 -0
- package/src/ssr/context.ts +105 -7
- package/src/ssr/index.ts +148 -3
- package/src/ssr/navigation.ts +2 -2
- package/src/ssr/region.ts +31 -19
- package/src/style.ts +32 -1
- package/src/test/browser.ts +28 -13
- package/src/test/index.ts +97 -12
- package/src/ts-plugin/diagnostics.ts +505 -0
- package/src/ts-plugin/fixes.ts +364 -0
- package/src/ts-plugin/index.ts +194 -0
- package/templates/_shared/AGENTS.md +2 -2
- package/templates/_shared/gitignore +5 -0
- package/templates/_shared/mcp.json +11 -0
- package/templates/_shared/src/routes/legal.tsx +15 -1
- package/templates/_shared/tsconfig.json +5 -0
- 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/prunelle.config.ts +1 -1
- package/templates/blog/src/routes/_layout.tsx +6 -1
- package/templates/blog/src/routes/index.tsx +5 -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/ts-plugin.cjs +4277 -0
|
@@ -0,0 +1,229 @@
|
|
|
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 { IslandIssue } from './islands'
|
|
5
|
+
import type { ModuleGraph } from './graph'
|
|
6
|
+
import { withStateReads } from '../compiler/interactivity'
|
|
7
|
+
import { isInNodeModules, isTsFile } from './files'
|
|
8
|
+
|
|
9
|
+
export type LoadModule = (file: string) => ModuleAnalysis | undefined
|
|
10
|
+
|
|
11
|
+
export const MAX_REEXPORT_DEPTH = 8
|
|
12
|
+
|
|
13
|
+
/** Follows `export { x } from './y'` and `export * from './y'` chains using `load` so parse errors are never swallowed. */
|
|
14
|
+
export function resolveExportTarget(
|
|
15
|
+
graph: ModuleGraph,
|
|
16
|
+
load: LoadModule,
|
|
17
|
+
file: string,
|
|
18
|
+
exportName: string,
|
|
19
|
+
depth = 0,
|
|
20
|
+
seen = new Set<string>(),
|
|
21
|
+
): { file: string, exportName: string } | undefined {
|
|
22
|
+
if (depth > MAX_REEXPORT_DEPTH)
|
|
23
|
+
return { file, exportName }
|
|
24
|
+
const key = `${file}\0${exportName}`
|
|
25
|
+
if (seen.has(key))
|
|
26
|
+
return { file, exportName }
|
|
27
|
+
seen.add(key)
|
|
28
|
+
|
|
29
|
+
const analysis = load(file)
|
|
30
|
+
if (analysis === undefined)
|
|
31
|
+
return undefined
|
|
32
|
+
const direct = analysis.reexports.find(r => r.exported === exportName)
|
|
33
|
+
if (direct !== undefined) {
|
|
34
|
+
const next = graph.resolve(file, direct.source)
|
|
35
|
+
if (next !== undefined && isTsFile(next))
|
|
36
|
+
return resolveExportTarget(graph, load, next, direct.local, depth + 1, seen)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (analysis.exportedNames.includes(exportName))
|
|
40
|
+
return { file, exportName }
|
|
41
|
+
|
|
42
|
+
// Follow `export * from './y'` barrel re-exports (named exports only, per ESM spec `export *` never re-exports `default`).
|
|
43
|
+
if (exportName !== 'default') {
|
|
44
|
+
for (const re of analysis.reexports) {
|
|
45
|
+
if (re.exported !== '*')
|
|
46
|
+
continue
|
|
47
|
+
const next = graph.resolve(file, re.source)
|
|
48
|
+
if (next === undefined || !isTsFile(next))
|
|
49
|
+
continue
|
|
50
|
+
const candidate = resolveExportTarget(graph, load, next, exportName, depth + 1, seen)
|
|
51
|
+
if (candidate !== undefined && load(candidate.file)?.exportedNames.includes(candidate.exportName))
|
|
52
|
+
return candidate
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return { file, exportName }
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function moduleHasInteractiveExport(graph: ModuleGraph, load: LoadModule, file: string, seen = new Set<string>()): boolean {
|
|
60
|
+
if (seen.has(file))
|
|
61
|
+
return false
|
|
62
|
+
seen.add(file)
|
|
63
|
+
const analysis = graph.analyses.get(file) ?? load(file)
|
|
64
|
+
if (analysis === undefined)
|
|
65
|
+
return false
|
|
66
|
+
if (analysis.stateExports.length > 0 || [...analysis.exports.values()].some(e => e.interactive))
|
|
67
|
+
return true
|
|
68
|
+
for (const re of analysis.reexports) {
|
|
69
|
+
const next = graph.resolve(file, re.source)
|
|
70
|
+
if (next === undefined || !isTsFile(next))
|
|
71
|
+
continue
|
|
72
|
+
if (re.exported === '*') {
|
|
73
|
+
if (moduleHasInteractiveExport(graph, load, next, seen))
|
|
74
|
+
return true
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
const target = resolveExportTarget(graph, load, next, re.local)
|
|
78
|
+
const targetAnalysis = target === undefined ? undefined : (graph.analyses.get(target.file) ?? load(target.file))
|
|
79
|
+
if (target !== undefined && (targetAnalysis?.stateExports.includes(target.exportName) === true || targetAnalysis?.exports.get(target.exportName)?.interactive === true))
|
|
80
|
+
return true
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return false
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function collectExportedSignals(graph: ModuleGraph, load: LoadModule, file: string, out: Set<string>, seen = new Set<string>()): void {
|
|
87
|
+
if (seen.has(file))
|
|
88
|
+
return
|
|
89
|
+
seen.add(file)
|
|
90
|
+
const analysis = graph.analyses.get(file) ?? load(file)
|
|
91
|
+
if (analysis === undefined)
|
|
92
|
+
return
|
|
93
|
+
for (const name of analysis.signalExports)
|
|
94
|
+
out.add(name)
|
|
95
|
+
for (const re of analysis.reexports) {
|
|
96
|
+
const next = graph.resolve(file, re.source)
|
|
97
|
+
if (next === undefined || !isTsFile(next))
|
|
98
|
+
continue
|
|
99
|
+
if (re.exported === '*') {
|
|
100
|
+
collectExportedSignals(graph, load, next, out, seen)
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
const target = resolveExportTarget(graph, load, next, re.local)
|
|
104
|
+
if (target !== undefined && (graph.analyses.get(target.file) ?? load(target.file))?.signalExports.includes(target.exportName) === true)
|
|
105
|
+
out.add(re.exported)
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function interactivityChanged(prev: ModuleAnalysis, next: { isInteractive: boolean, exports: Map<string, { interactive: boolean }>, localIslands: Map<string, unknown> }): boolean {
|
|
111
|
+
if (prev.isInteractive !== next.isInteractive)
|
|
112
|
+
return true
|
|
113
|
+
for (const [name, fact] of next.exports) {
|
|
114
|
+
if (prev.exports.get(name)?.interactive !== fact.interactive)
|
|
115
|
+
return true
|
|
116
|
+
}
|
|
117
|
+
return next.localIslands.size !== prev.localIslands.size || [...next.localIslands.keys()].some(name => !prev.localIslands.has(name))
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** An imported island a module reads as a value (`const D = Counter`, `{ C: Counter }`, `c={Counter}`), which makes what holds it an island. */
|
|
121
|
+
export interface IslandValueUse { file: string, offset: number, name: string }
|
|
122
|
+
|
|
123
|
+
const COMPONENT_NAME_RE = /^[A-Z]/
|
|
124
|
+
|
|
125
|
+
// The first place `analysis` reads `local` as a value, its bindings being in source order.
|
|
126
|
+
const firstValueRead = (analysis: ModuleAnalysis, local: string): number | undefined => analysis.bindings.map(b => b.valueRefs.get(local)).find(offset => offset !== undefined)
|
|
127
|
+
|
|
128
|
+
export function createPromoter(graph: ModuleGraph, load: LoadModule, issues: IslandIssue[], valueUses: IslandValueUse[] = []): { promoteAll: () => void, loadPromoted: LoadModule } {
|
|
129
|
+
const initialAsyncInteractive = new Set<string>()
|
|
130
|
+
const initialServerOnly = new Set<string>()
|
|
131
|
+
const reportedAsync = new Set<string>()
|
|
132
|
+
const reportedServerOnly = new Set<string>()
|
|
133
|
+
|
|
134
|
+
const recordInitial = (file: string, a: ModuleAnalysis): void => {
|
|
135
|
+
if (a.hasAsyncDefault && a.exports.get('default')?.interactive === true)
|
|
136
|
+
initialAsyncInteractive.add(file)
|
|
137
|
+
if (a.serverOnly !== undefined)
|
|
138
|
+
initialServerOnly.add(file)
|
|
139
|
+
}
|
|
140
|
+
for (const [file, a] of graph.analyses)
|
|
141
|
+
recordInitial(file, a)
|
|
142
|
+
|
|
143
|
+
// What an import names: reactive state, an interactive export (a component or a hook), or neither.
|
|
144
|
+
const interactiveKind = (file: string, source: string, imported: string): 'state' | 'export' | undefined => {
|
|
145
|
+
const resolved = graph.resolve(file, source)
|
|
146
|
+
if (resolved === undefined || !isTsFile(resolved))
|
|
147
|
+
return undefined
|
|
148
|
+
if (imported === '*')
|
|
149
|
+
return moduleHasInteractiveExport(graph, load, resolved) ? 'export' : undefined
|
|
150
|
+
const target = resolveExportTarget(graph, load, resolved, imported)
|
|
151
|
+
if (target === undefined)
|
|
152
|
+
return undefined
|
|
153
|
+
const targetAnalysis = graph.analyses.get(target.file) ?? load(target.file)
|
|
154
|
+
if (targetAnalysis?.stateExports.includes(target.exportName) === true)
|
|
155
|
+
return 'state'
|
|
156
|
+
return targetAnalysis?.exports.get(target.exportName)?.interactive === true ? 'export' : undefined
|
|
157
|
+
}
|
|
158
|
+
const isInteractiveTarget = (file: string, source: string, imported: string): boolean => interactiveKind(file, source, imported) !== undefined
|
|
159
|
+
|
|
160
|
+
const reportedValueUses = new Set<string>()
|
|
161
|
+
const recordValueUses = (file: string, analysis: ModuleAnalysis): void => {
|
|
162
|
+
for (const imp of analysis.imports) {
|
|
163
|
+
for (const spec of imp.specifiers) {
|
|
164
|
+
const offset = spec.imported === '*' || !COMPONENT_NAME_RE.test(spec.local) ? undefined : firstValueRead(analysis, spec.local)
|
|
165
|
+
if (offset === undefined || reportedValueUses.has(`${file}#${spec.local}`) || interactiveKind(file, imp.source, spec.imported) !== 'export')
|
|
166
|
+
continue
|
|
167
|
+
reportedValueUses.add(`${file}#${spec.local}`)
|
|
168
|
+
valueUses.push({ file, offset, name: spec.local })
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const promoteOne = (file: string): boolean => {
|
|
174
|
+
if (isInNodeModules(file) || !isTsFile(file))
|
|
175
|
+
return false
|
|
176
|
+
const analysis = graph.analyses.get(file)
|
|
177
|
+
if (analysis === undefined)
|
|
178
|
+
return false
|
|
179
|
+
const promoted = withStateReads(analysis, (imp, imported) => isInteractiveTarget(file, imp.source, imported))
|
|
180
|
+
if (promoted === undefined)
|
|
181
|
+
return false
|
|
182
|
+
const page = promoted.exports.get('default')
|
|
183
|
+
if (analysis.hasAsyncDefault && page?.interactive === true) {
|
|
184
|
+
if (!initialAsyncInteractive.has(file) && !reportedAsync.has(file)) {
|
|
185
|
+
reportedAsync.add(file)
|
|
186
|
+
issues.push({ kind: 'async-interactive', file, offset: page.offset ?? 0, reason: page.reason })
|
|
187
|
+
}
|
|
188
|
+
return false
|
|
189
|
+
}
|
|
190
|
+
if (promoted.serverOnly !== undefined) {
|
|
191
|
+
if (!initialServerOnly.has(file) && !reportedServerOnly.has(file)) {
|
|
192
|
+
reportedServerOnly.add(file)
|
|
193
|
+
issues.push({ kind: 'server-only-tag', file, serverOnly: promoted.serverOnly })
|
|
194
|
+
}
|
|
195
|
+
return false
|
|
196
|
+
}
|
|
197
|
+
if (!interactivityChanged(analysis, promoted))
|
|
198
|
+
return false
|
|
199
|
+
recordValueUses(file, analysis)
|
|
200
|
+
graph.analyses.set(file, { ...analysis, ...promoted })
|
|
201
|
+
return true
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const promoteAll = (): void => {
|
|
205
|
+
for (let pass = 0; pass < MAX_REEXPORT_DEPTH; pass++) {
|
|
206
|
+
let changed = false
|
|
207
|
+
for (const file of [...graph.analyses.keys()]) {
|
|
208
|
+
if (promoteOne(file))
|
|
209
|
+
changed = true
|
|
210
|
+
}
|
|
211
|
+
if (!changed)
|
|
212
|
+
break
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const loadPromoted: LoadModule = (file: string): ModuleAnalysis | undefined => {
|
|
217
|
+
const existed = graph.analyses.has(file)
|
|
218
|
+
const analysis = graph.analyses.get(file) ?? load(file)
|
|
219
|
+
if (analysis === undefined)
|
|
220
|
+
return undefined
|
|
221
|
+
if (!existed) {
|
|
222
|
+
recordInitial(file, analysis)
|
|
223
|
+
promoteAll()
|
|
224
|
+
}
|
|
225
|
+
return graph.analyses.get(file) ?? analysis
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
return { promoteAll, loadPromoted }
|
|
229
|
+
}
|
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 } 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,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
|
-
|
|
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))
|
|
@@ -526,8 +333,8 @@ export function analyzeIslands(options: AnalyzeIslandsOptions): IslandPlan {
|
|
|
526
333
|
const load: LoadModule = options.load ?? (file => graph.load(file))
|
|
527
334
|
const issues: IslandIssue[] = []
|
|
528
335
|
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'])
|
|
336
|
+
// Reserve 'runtime' and 'regions' so an island named Runtime.tsx or Regions.tsx never overwrites entriesDir/runtime.ts or entriesDir/regions.ts.
|
|
337
|
+
const usedIds = new Set<string>(['runtime', 'regions'])
|
|
531
338
|
const pageSet = new Set(pageFiles)
|
|
532
339
|
|
|
533
340
|
function registerIsland(file: string, exportName: string, local?: string): IslandTarget {
|
|
@@ -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
|
|
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>> {
|