@kudzujs/core 0.6.5 → 0.6.6
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/GOAL_B.md +153 -0
- package/README.md +15 -0
- package/framework/README.md +2 -0
- package/framework/build.mjs +203 -8
- package/package.json +2 -1
package/GOAL_B.md
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
# Goal B: Static Realtime Dashboards
|
|
2
|
+
|
|
3
|
+
Goal B makes Kudzu sufficient for a ThingsBoard-shaped realtime device dashboard without turning Kudzu into a stream runtime, widget framework, or server platform. It preserves complete static documents, zero-JavaScript routes that use no browser capabilities, direct DOM ownership, native navigation fallback, and the absence of React, a VDOM, hydration, or a retained browser component tree.
|
|
4
|
+
|
|
5
|
+
`MIGRATION_ROADMAP.md` remains the source of truth for compiler invariants and fixture-first development. This document is the implementation contract for realtime dashboard work.
|
|
6
|
+
|
|
7
|
+
## Product Target
|
|
8
|
+
|
|
9
|
+
A user can statically deploy this flow:
|
|
10
|
+
|
|
11
|
+
```text
|
|
12
|
+
dashboard -> devices -> device detail -> alarms -> settings
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
The first vertical slice is deliberately smaller:
|
|
16
|
+
|
|
17
|
+
```text
|
|
18
|
+
plain route <-> realtime device dashboard
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
The dashboard receives a logical 1,000 telemetry samples per second, keeps a bounded history in a module Worker, downsamples it, and updates one imperative chart without routing samples through `useState()`.
|
|
22
|
+
|
|
23
|
+
## Runtime Model
|
|
24
|
+
|
|
25
|
+
```text
|
|
26
|
+
Complete static dashboard shell
|
|
27
|
+
-> route-specific effect ESM
|
|
28
|
+
-> route-owned module Worker
|
|
29
|
+
-> bounded telemetry buffer and downsampling
|
|
30
|
+
-> batched imperative chart updates
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Kudzu state is for low-frequency UI state such as selected device, time range, filters, tabs, connection status, alarm status, and widget configuration. High-frequency samples belong in a Worker or imperative browser module.
|
|
34
|
+
|
|
35
|
+
The Worker is a capability, not a framework runtime. Routes that do not create one must not load its graph. Static routes must remain JavaScript-free.
|
|
36
|
+
|
|
37
|
+
## Milestone 1: Relative TypeScript Workers
|
|
38
|
+
|
|
39
|
+
Status: implemented and verified. The compiler recognizes only the exact inline-effect form below, emits its validated graph separately, rewrites the constructor to the base-aware same-origin asset, and leaves routes without this capability on their existing output paths.
|
|
40
|
+
|
|
41
|
+
Support this exact shape inside a compiled inline `useEffect` callback:
|
|
42
|
+
|
|
43
|
+
```tsx
|
|
44
|
+
useEffect(() => {
|
|
45
|
+
const worker = new Worker(
|
|
46
|
+
new URL("../telemetry.worker.ts", import.meta.url),
|
|
47
|
+
{ type: "module" },
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
const onMessage = (event: MessageEvent<ChartFrame>) => chart.render(event.data)
|
|
51
|
+
worker.addEventListener("message", onMessage)
|
|
52
|
+
|
|
53
|
+
return () => {
|
|
54
|
+
worker.removeEventListener("message", onMessage)
|
|
55
|
+
worker.terminate()
|
|
56
|
+
chart.dispose()
|
|
57
|
+
}
|
|
58
|
+
}, [])
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Compiler requirements:
|
|
62
|
+
|
|
63
|
+
- accept only an unshadowed `Worker` with `new URL(relativeLiteral, import.meta.url)` and literal `{ type: "module" }`;
|
|
64
|
+
- resolve one `.worker.ts` entry under project source and bundle its relative TypeScript graph separately from window code;
|
|
65
|
+
- emit deterministic content-hashed ESM under `assets/workers` and rewrite the constructor to the base-aware emitted URL;
|
|
66
|
+
- reject package imports, JSX, TypeScript import-equals declarations, dynamic imports, `require()`, paths outside source, malformed options, and unsupported Worker forms with source locations;
|
|
67
|
+
- reject ordinary runtime imports or re-exports of `.worker.ts`; type-only imports may erase normally;
|
|
68
|
+
- do not mark Worker files as navigation capability scripts or import them into the window;
|
|
69
|
+
- create the Worker only when the owning effect mounts;
|
|
70
|
+
- preserve byte-for-byte generated shared/list/effect paths for builds without relative TypeScript Workers where practical;
|
|
71
|
+
- reject generated/public asset collisions instead of silently overwriting output.
|
|
72
|
+
|
|
73
|
+
Worker construction inside imported helpers or imported keyed-row effects, `SharedWorker`, classic workers, inline Blob workers, and arbitrary dynamic Worker URLs are outside Milestone 1. Imported keyed rows must move Worker ownership to a directly compiled page or local component effect so lexical global analysis remains tied to the original source tree.
|
|
74
|
+
|
|
75
|
+
## Ownership
|
|
76
|
+
|
|
77
|
+
| Owner | Lifetime | Dashboard responsibility |
|
|
78
|
+
|---|---|---|
|
|
79
|
+
| Document | Full document | authentication expiry and global diagnostics |
|
|
80
|
+
| Layout | Enhanced navigation session | tenant session or shared transport |
|
|
81
|
+
| Route | Current dashboard/device | Worker, telemetry subscription, request cancellation |
|
|
82
|
+
| DOM range | Current widget | chart, gauge, map, table, animation frame |
|
|
83
|
+
| Worker | Explicit owner cleanup | parsing, bounded buffering, aggregation, downsampling |
|
|
84
|
+
|
|
85
|
+
Leaving a route must remove message listeners, stop chart work, terminate its Worker, and invalidate stale UI writes before another route mounts. BFCache-preserved documents must retain their live ownership until a real document exit.
|
|
86
|
+
|
|
87
|
+
## Fixture Contract
|
|
88
|
+
|
|
89
|
+
The first fixture must provide:
|
|
90
|
+
|
|
91
|
+
- one complete realtime dashboard document and one complete plain document in an opt-in navigation group;
|
|
92
|
+
- one unrelated static route with zero JavaScript;
|
|
93
|
+
- a relative TypeScript Worker importing at least one relative helper;
|
|
94
|
+
- a fixed-capacity ring buffer with deterministic eviction;
|
|
95
|
+
- logical 1,000 samples/second input in batches rather than a 1 ms browser timer;
|
|
96
|
+
- bounded downsampled frames delivered at no more than display cadence;
|
|
97
|
+
- one imperative canvas or DOM chart updated without sample-level Kudzu setters;
|
|
98
|
+
- direct load, dashboard-to-plain navigation, back/forward, and repeated cached revisits;
|
|
99
|
+
- exact counters for Worker starts, terminations, messages, renders, listeners, and stale post-cleanup work;
|
|
100
|
+
- native document fallback when JavaScript or Worker creation fails.
|
|
101
|
+
|
|
102
|
+
## Acceptance Criteria
|
|
103
|
+
|
|
104
|
+
- the Worker graph is absent from static and plain route HTML and is fetched only after the dashboard effect mounts;
|
|
105
|
+
- two unchanged production builds emit identical Worker names and bytes;
|
|
106
|
+
- changing Worker source changes its content hash;
|
|
107
|
+
- base-prefixed deployment produces a valid same-origin Worker URL;
|
|
108
|
+
- 30 dashboard/plain cycles create and terminate exactly 30 route Workers with no growing listener or chart ownership;
|
|
109
|
+
- messages arriving after cleanup cannot update removed route DOM;
|
|
110
|
+
- the ring buffer remains at its configured capacity under sustained input;
|
|
111
|
+
- chart rendering is batched and sample ingestion does not call `useState()`;
|
|
112
|
+
- routes without Worker capabilities remain byte-for-byte unaffected;
|
|
113
|
+
- output raw/gzip cost, clean build time, sample throughput, render cadence, and lifecycle counters are recorded;
|
|
114
|
+
- `npm run check`, `npm test`, package dry-run, and browser checks pass.
|
|
115
|
+
|
|
116
|
+
Verified measurements for the focused `/dash` fixture: the minified Worker graph is `assets/workers/telemetry.worker-BVG2SA55.js`, 907 B raw and 477 B gzip. The dashboard window graph is 11,388 B raw and 5,148 B gzip across its shared runtime, effect runtime, navigation, route effect entry, and handler module; the Worker is not part of that graph. Seven clean minified builds measured 455.1, 459.9, 460.7, 463.5, 467.6, 472.6, and 475.2 ms, with a 463.5 ms median.
|
|
117
|
+
|
|
118
|
+
The real-Worker browser check uses real wall time and requires sustained generation beyond 1,130 samples at 700-1,300 logical samples/second, an exact 128-sample ring bound, batches of 10, exactly 24 displayed points, multiple renders, and a render ceiling below 25 Hz. Delayed Worker ticks catch up in batches to the logical 1,000 samples/second clock; frames emit no more often than every 50 ms. The imperative chart performs a minimal canvas path draw. The navigation ownership check completed 30 dashboard/plain cycles with exactly 30 starts and 30 terminations, exactly 60 listener additions and removals across message and error listeners, zero retained listeners after every cleanup, disposed every old chart canvas, fresh ownership on back/forward and cached revisits, and no render from a removed message listener invoked after cleanup. Dashboard, plain, and static HTML contain no Worker asset URL; plain does not load the route effect graph, and static contains no script, capability marker, or state payload. A no-Worker equivalent emitted byte-identical shared runtime, effect runtime, navigation, and route effect entry files with no `assets/workers` directory. An unreachable imported-row effect referencing `unused.worker.ts` emitted no Worker asset. Two unchanged builds emitted identical Worker names and bytes, and a controlled downsample-source change changed the emitted hash.
|
|
119
|
+
|
|
120
|
+
## Delivery Order
|
|
121
|
+
|
|
122
|
+
1. **Worker compiler capability**: exact syntax, graph bundling, hashing, base rewriting, diagnostics, and zero-cost exclusion.
|
|
123
|
+
2. **Realtime vertical slice**: mock telemetry Worker, bounded buffer, downsampling, imperative chart, and route cleanup.
|
|
124
|
+
3. **Shared transport**: add a layout-owned mock connection only if multiple routes prove that one Worker per route is wasteful.
|
|
125
|
+
4. **Device workflows**: filters, commands, timeout/error handling, and stale response suppression.
|
|
126
|
+
5. **Alarm workflows**: active/history views and optimistic acknowledgement with rollback.
|
|
127
|
+
6. **Widget expansion**: add one gauge, table, map, or real chart engine at a time only when a fixture requires it.
|
|
128
|
+
|
|
129
|
+
Each phase starts with one failing fixture and ends with correctness, lifecycle, browser, size, and build measurements.
|
|
130
|
+
|
|
131
|
+
## Performance Gates
|
|
132
|
+
|
|
133
|
+
- sustained 1,000 samples/second does not create one main-thread task or Kudzu state commit per sample;
|
|
134
|
+
- Worker memory is bounded by declared buffer capacity;
|
|
135
|
+
- chart updates are batched to at most one per display frame;
|
|
136
|
+
- dashboard departure stops observable messages and renders before the next route mounts;
|
|
137
|
+
- repeated navigation leaves no growing Worker, timer, listener, chart, state, DOM, or heap ownership;
|
|
138
|
+
- Worker support adds no bytes to routes and builds that do not use it;
|
|
139
|
+
- material losses are profiled and fixed or documented as explicit tradeoffs using matched initial content and behavior.
|
|
140
|
+
|
|
141
|
+
## Non-Goals
|
|
142
|
+
|
|
143
|
+
- implementing ThingsBoard's server, protocol, rule engine, database, or complete UI;
|
|
144
|
+
- storing telemetry samples in Kudzu component state;
|
|
145
|
+
- adding a general observable, scheduler, stream, state, or widget runtime;
|
|
146
|
+
- retaining a browser component tree;
|
|
147
|
+
- request-time SSR, Server Actions, or a hidden application server;
|
|
148
|
+
- a plugin marketplace or arbitrary third-party React widgets;
|
|
149
|
+
- claiming Worker isolation as a security sandbox.
|
|
150
|
+
|
|
151
|
+
## Completion Definition
|
|
152
|
+
|
|
153
|
+
Goal B Milestone 1 is complete when the focused realtime fixture proves deterministic relative TypeScript Worker emission, bounded high-frequency processing, imperative chart updates, exact route ownership and cleanup across repeated navigation, native/static fallback, zero-cost exclusion, source diagnostics, and recorded production measurements.
|
package/README.md
CHANGED
|
@@ -380,6 +380,20 @@ Dependency values are limited to JSON-safe strings, finite numbers, booleans, an
|
|
|
380
380
|
|
|
381
381
|
Effect callbacks must be inline and block-bodied. Named or dynamically obtained cleanup functions, cleanup parameters or generators, other return values, callback parameters, and non-serializable captures are rejected. Async effects cannot return cleanup functions; the cleanup itself may be async. Pages without effects receive no effect entry. Empty-dependency effects retain their smaller output, and dependency-only capability code is isolated to the routes that use `kudzu-deps.js` unless another capability already requires the shared runtime.
|
|
382
382
|
|
|
383
|
+
An inline effect may own an exact relative TypeScript module Worker:
|
|
384
|
+
|
|
385
|
+
```tsx
|
|
386
|
+
useEffect(() => {
|
|
387
|
+
const worker = new Worker(
|
|
388
|
+
new URL("../telemetry.worker.ts", import.meta.url),
|
|
389
|
+
{ type: "module" },
|
|
390
|
+
)
|
|
391
|
+
return () => worker.terminate()
|
|
392
|
+
}, [])
|
|
393
|
+
```
|
|
394
|
+
|
|
395
|
+
Kudzu resolves the path from the callback source, bundles the Worker and its relative TypeScript imports separately as content-hashed ESM under `assets/workers`, and rewrites the constructor to the base-prefixed same-origin asset URL. The Worker is fetched only when the effect mounts; it is not a capability script, preload, or window import. Unrendered effect handlers do not cause their Worker root to be emitted. This slice requires unshadowed global `Worker` and `URL`, exact `import.meta.url`, a relative `.worker.ts` string literal, and exactly `{ type: "module" }`. Worker graphs reject JSX, package runtime imports, TypeScript import-equals declarations, dynamic imports, `require()`, missing files, and paths outside `src`. Worker source cannot be imported or re-exported as an ordinary runtime module. Construction in event handlers, imported helpers, or imported keyed-row effects is rejected; move keyed-row Worker ownership to a directly compiled page or local component effect. Public or absolute JavaScript Workers remain ordinary browser code and are not transformed.
|
|
396
|
+
|
|
383
397
|
A matched mount-fetch benchmark renders a title and two keyed rows from local JSON. With one warm-up and seven rotating clean builds, Kudzu shipped initial HTML, 3.4 KB initial JS gzip, 8.1 KB total output, and built in 374 ms. React CSR shipped no initial content, 59.3 KB initial JS gzip, 189.2 KB total output, and built in 992 ms. Hand-written ESM shipped 534 B initial JS gzip, 1.2 KB total output, and built in 210 ms. Fresh-profile Chrome medians to loaded data were 157.9 ms, 166.5 ms, and 153.4 ms respectively.
|
|
384
398
|
|
|
385
399
|
A matched resize-listener cleanup fixture, measured with the same warm-up and seven rotating clean builds, shipped 1.2 KB JavaScript gzip and built in 402 ms with Kudzu. Svelte shipped 10.1 KB and built in 861 ms, Vue shipped 23.6 KB and built in 768 ms, and React shipped 59.1 KB and built in 1,058 ms. Kudzu and the 127 B hand-written Astro baseline emitted initial HTML; the CSR fixtures did not.
|
|
@@ -495,6 +509,7 @@ Supported:
|
|
|
495
509
|
- Base-path deployments, multiple CSS files, and `afterBuild`
|
|
496
510
|
- Primitive `useState` bindings
|
|
497
511
|
- Mount-only `useEffect(fn, [])` compiled to route-specific ESM
|
|
512
|
+
- Relative TypeScript module Workers owned by inline effects
|
|
498
513
|
- Conditional and keyed-row effect ownership with cleanup on DOM removal
|
|
499
514
|
- Synchronous and async event handlers
|
|
500
515
|
- Relative imported helpers in native handlers
|
package/framework/README.md
CHANGED
|
@@ -17,6 +17,8 @@
|
|
|
17
17
|
|
|
18
18
|
Static routes receive no browser runtime. Command routes receive `runtime.js`; dependency effects use route-specific `kudzu-deps.js` unless that route already requires shared commit hooks; runtime bracket pages using `useParams()` add one route-specific pathname matcher; reactive attributes and conditions add `binding-runtime.js`; keyed lists add `list-runtime.js`; native handlers add `native-runtime.js`; effects add `effect-runtime.js` and one route-specific entry. Generated module scripts live in the document head, so cold downloads overlap HTML transfer while standard module deferral preserves execution after parsing. A single effect with one dependency compiles to a direct runner; generic maps, sets, and ordering are reserved for larger effect graphs. Dependency commits coalesce in a microtask; affected cleanups are awaited in declaration order before replacement setups run. Document cleanup integrates with shared unmount hooks when present and otherwise disposes directly on non-persisted `pagehide`. List builds remove unused text-range, attribute, event, expression, condition, seed, and mount branches. Effect builds omit capture deserialization entirely when every effect scope is empty. Capability runtimes share state and lifecycle hooks through `shared-runtime.js`. Generated evaluators and their bundled relative TypeScript helpers live under `dist/assets/handlers/`; shared helper chunks are emitted only when multiple handler entries need them. Runtime fallback rewrites are ordered by specificity in `.kudzu/kudzu-plan.json` and passed to `afterBuild()`; exact static files take precedence in development. The dev server derives stable state identities from route-unique state variable names in each route plan; every state sharing a duplicate name is omitted. It then injects its SSE reload, short-lived full-URL-scoped logical-state snapshot, and build-error client into responses only, never into `dist/`. Snapshots are consumed even when the next page is static or broken. Reload restoration covers compatible framework state, not uncontrolled DOM state, focus, selection, or imperative mutations.
|
|
19
19
|
|
|
20
|
+
Exact relative `.worker.ts` constructors in inline effects are validated and bundled in a separate content-hashed ESM graph under `dist/assets/workers/`. Those files are referenced only by rendered effect handlers and never become document capability scripts, preloads, or window imports; unreachable source effects do not emit their Worker roots. Worker graphs allow relative TypeScript ESM runtime imports only and reject JSX, package runtime imports, import-equals declarations, dynamic imports, `require()`, and paths outside `src`. Ordinary runtime imports or re-exports of `.worker.ts` and Worker construction in imported keyed-row effects are rejected.
|
|
21
|
+
|
|
20
22
|
Page `metadata` can emit description, canonical, favicon, manifest, Open Graph, and Twitter Card tags without a client runtime. Source CSS and global `kudzu.config` styles are emitted in document heads before `afterBuild()` runs; static stylesheet links in component JSX fail compilation instead of loading from the body.
|
|
21
23
|
|
|
22
24
|
Direct JSON-safe primitive keyed-item dependencies subscribe each row record to its owning list commit and compare selected fields after `list-runtime.js` synchronously refreshes the row marker. Only changed rows rerun with the complete latest item; reorder compares equal, unrelated fields do nothing, and key changes remain remove plus mount. Builds without item dependencies emit no item reader or list-state subscription code.
|
package/framework/build.mjs
CHANGED
|
@@ -45,8 +45,10 @@ export async function build({ quiet = false, minify = true } = {}) {
|
|
|
45
45
|
const sourceIndex = new Map(await Promise.all(sourceFiles.map(async file => [file, await readFile(file, "utf8")])))
|
|
46
46
|
|
|
47
47
|
const handlerModules = []
|
|
48
|
+
const workerReferences = []
|
|
48
49
|
for (const file of sourceFiles) {
|
|
49
|
-
|
|
50
|
+
if (file.endsWith(".worker.ts")) continue
|
|
51
|
+
const handlerModule = await compile(file, sourceFileSet, sourceIndex, base, workerReferences)
|
|
50
52
|
if (handlerModule) handlerModules.push(handlerModule)
|
|
51
53
|
}
|
|
52
54
|
|
|
@@ -161,6 +163,18 @@ export async function build({ quiet = false, minify = true } = {}) {
|
|
|
161
163
|
|
|
162
164
|
const assetsDirectory = join(outputDirectory, "assets")
|
|
163
165
|
await mkdir(assetsDirectory, { recursive: true })
|
|
166
|
+
const renderedEffects = new Set(plans.flatMap(plan => plan.effects.map(effect => `${effect.module}:${effect.handler}`)))
|
|
167
|
+
const renderedWorkerReferences = workerReferences.filter(reference => renderedEffects.has(`${reference.module}:${reference.handler}`))
|
|
168
|
+
if (renderedWorkerReferences.length && await exists(join(root, "public", "assets", "workers"))) throw new Error("public/assets/workers collides with Kudzu's generated Worker asset namespace")
|
|
169
|
+
const workerAssets = await emitWorkers(renderedWorkerReferences, sourceFileSet, assetsDirectory, base, minify)
|
|
170
|
+
for (const module of handlerModules) {
|
|
171
|
+
for (const reference of workerReferences) {
|
|
172
|
+
if (reference.module !== assetPath(base, `assets/${module.path}`)) continue
|
|
173
|
+
const url = workerAssets.get(reference.placeholder) ?? "about:blank"
|
|
174
|
+
module.code = module.code.replaceAll(JSON.stringify(reference.placeholder), JSON.stringify(url))
|
|
175
|
+
}
|
|
176
|
+
if (module.code.includes("/__kudzu_worker_")) throw new Error(`Worker URL placeholder survived in ${module.path}`)
|
|
177
|
+
}
|
|
164
178
|
const commandEvents = [...new Set(plans.flatMap(plan => plan.events.filter(event => event.commands).map(event => event.event)))].sort()
|
|
165
179
|
const nativeEvents = [...new Set(plans.flatMap(plan => plan.events.filter(event => event.native).map(event => event.event)))].sort()
|
|
166
180
|
const hasTextBindings = plans.some(plan => plan.bindings.some(binding => binding.target === "text"))
|
|
@@ -1528,7 +1542,7 @@ function escapeAttribute(value) {
|
|
|
1528
1542
|
return escapeHtml(value).replaceAll('"', """).replaceAll("'", "'")
|
|
1529
1543
|
}
|
|
1530
1544
|
|
|
1531
|
-
async function compile(file, sourceFiles, sourceIndex, base) {
|
|
1545
|
+
async function compile(file, sourceFiles, sourceIndex, base, workerReferences) {
|
|
1532
1546
|
const source = sourceIndex.get(file)
|
|
1533
1547
|
const nativeHandlers = []
|
|
1534
1548
|
const effectHandlers = []
|
|
@@ -1544,7 +1558,7 @@ async function compile(file, sourceFiles, sourceIndex, base) {
|
|
|
1544
1558
|
jsx: ts.JsxEmit.ReactJSX,
|
|
1545
1559
|
jsxImportSource: "@kudzujs/core"
|
|
1546
1560
|
},
|
|
1547
|
-
transformers: { before: [createKudzuTransformer(nativeHandlers, effectHandlers, reactiveBindings, listExpressions, assetPath(base, `assets/${handlerPath}`), file, sourceFiles, sourceIndex, clientImports)] },
|
|
1561
|
+
transformers: { before: [createKudzuTransformer(nativeHandlers, effectHandlers, reactiveBindings, listExpressions, assetPath(base, `assets/${handlerPath}`), file, sourceFiles, sourceIndex, clientImports, workerReferences)] },
|
|
1548
1562
|
reportDiagnostics: true
|
|
1549
1563
|
})
|
|
1550
1564
|
|
|
@@ -1574,12 +1588,13 @@ async function compile(file, sourceFiles, sourceIndex, base) {
|
|
|
1574
1588
|
return { path: handlerPath, code: moduleResult.outputText, hasNativeHandlers: nativeHandlers.length > 0, hasEffects: effectHandlers.length > 0, clientImports: [...clientImports] }
|
|
1575
1589
|
}
|
|
1576
1590
|
|
|
1577
|
-
function createKudzuTransformer(nativeHandlers, effectHandlers, reactiveBindings, listExpressions, handlerUrl, file, sourceFiles, sourceIndex, clientImports) {
|
|
1591
|
+
function createKudzuTransformer(nativeHandlers, effectHandlers, reactiveBindings, listExpressions, handlerUrl, file, sourceFiles, sourceIndex, clientImports, workerReferences) {
|
|
1578
1592
|
return context => sourceFile => {
|
|
1579
1593
|
const factory = context.factory
|
|
1580
1594
|
const hasLinkElements = /<link/i.test(sourceFile.text)
|
|
1581
1595
|
sourceFile = normalizeRenderControlFlow(sourceFile, factory, context)
|
|
1582
1596
|
ts.setParentRecursive(sourceFile, false)
|
|
1597
|
+
rejectOrdinaryWorkerImports(sourceFile, file, sourceFiles)
|
|
1583
1598
|
const importBindings = clientImportBindings(sourceFile, file, sourceFiles)
|
|
1584
1599
|
const hasUseEffectImport = sourceFile.statements.some(statement => ts.isImportDeclaration(statement) && statement.moduleSpecifier.text === "@kudzujs/core" && statement.importClause?.namedBindings && ts.isNamedImports(statement.importClause.namedBindings) && statement.importClause.namedBindings.elements.some(entry => !entry.propertyName && entry.name.text === "useEffect"))
|
|
1585
1600
|
const importedSources = new Map()
|
|
@@ -1855,7 +1870,19 @@ function createKudzuTransformer(nativeHandlers, effectHandlers, reactiveBindings
|
|
|
1855
1870
|
if (invalidCleanup) effectFail(invalidCleanup, "useEffect() cleanup functions cannot declare parameters or be generators")
|
|
1856
1871
|
if (returns.cleanup && callback.modifiers?.some(modifier => modifier.kind === ts.SyntaxKind.AsyncKeyword)) effectFail(callback, "useEffect() async callbacks cannot return cleanup functions")
|
|
1857
1872
|
const setters = settersForNode(node, settersByFunction)
|
|
1858
|
-
const
|
|
1873
|
+
const callbackSource = listEffect?.sourceFile ?? sourceFile
|
|
1874
|
+
const callbackFile = callbackSource.fileName
|
|
1875
|
+
const workerStart = workerReferences.length
|
|
1876
|
+
let compiledCallback
|
|
1877
|
+
if (listEffect && callbackFile !== file) {
|
|
1878
|
+
const originalCallback = listEffect.source.arguments[0]
|
|
1879
|
+
rejectWorkerConstructions(originalCallback, callbackSource, "Relative TypeScript Worker construction in imported keyed-row effects is not supported; construct the Worker in a directly compiled page or local component effect")
|
|
1880
|
+
compiledCallback = callback
|
|
1881
|
+
} else {
|
|
1882
|
+
compiledCallback = rewriteEffectWorkers(callback, callbackFile, callbackSource, sourceFiles, workerReferences, factory, context)
|
|
1883
|
+
}
|
|
1884
|
+
const descriptor = compileNativeCallback(compiledCallback, setters, factory, effectHandlers, listEffect?.imports ?? importBindings, clientImports, "effect", dependencyItem, true, returns.cleanup)
|
|
1885
|
+
for (const reference of workerReferences.slice(workerStart)) Object.assign(reference, { module: handlerUrl, handler: descriptor.exportName })
|
|
1859
1886
|
usesListItem ||= Boolean(itemDependencies.length && !listEffect)
|
|
1860
1887
|
usesBehavior = true
|
|
1861
1888
|
return factory.updateCallExpression(node, node.expression, node.typeArguments, [
|
|
@@ -2547,6 +2574,7 @@ function compileEvent(expression, setters, functions, factory, nativeHandlers, h
|
|
|
2547
2574
|
const optimized = compileOptimizedEvent(expression, setters, factory)
|
|
2548
2575
|
if (optimized) return optimized
|
|
2549
2576
|
|
|
2577
|
+
rejectWorkerConstructions(expression, expression.getSourceFile(), "Relative TypeScript Worker construction is only supported directly inside an inline useEffect() callback")
|
|
2550
2578
|
const descriptor = compileNativeCallback(expression, setters, factory, nativeHandlers, importBindings, clientImports, "handler", listItem)
|
|
2551
2579
|
return factory.createCallExpression(factory.createIdentifier("__kNativeBehavior"), undefined, [
|
|
2552
2580
|
factory.createStringLiteral(handlerUrl),
|
|
@@ -2609,9 +2637,81 @@ function compileOptimizedEvent(expression, setters, factory) {
|
|
|
2609
2637
|
}
|
|
2610
2638
|
|
|
2611
2639
|
const nativeGlobals = new Set([
|
|
2612
|
-
"Array", "ArrayBuffer", "BigInt", "Boolean", "Date", "Error", "Event", "FormData", "Infinity", "Intl", "JSON", "Map", "Math", "NaN", "Number", "Object", "Promise", "Proxy", "RangeError", "ReferenceError", "Reflect", "RegExp", "Set", "String", "Symbol", "TypeError", "URL", "URLSearchParams", "WeakMap", "WeakSet", "WebSocket", "atob", "btoa", "clearInterval", "clearTimeout", "console", "crypto", "document", "fetch", "globalThis", "history", "isFinite", "isNaN", "location", "navigator", "parseFloat", "parseInt", "queueMicrotask", "requestAnimationFrame", "setInterval", "setTimeout", "structuredClone", "undefined", "window"
|
|
2640
|
+
"Array", "ArrayBuffer", "BigInt", "Boolean", "Date", "Error", "Event", "FormData", "Infinity", "Intl", "JSON", "Map", "Math", "NaN", "Number", "Object", "Promise", "Proxy", "RangeError", "ReferenceError", "Reflect", "RegExp", "Set", "String", "Symbol", "TypeError", "URL", "URLSearchParams", "WeakMap", "WeakSet", "WebSocket", "Worker", "atob", "btoa", "clearInterval", "clearTimeout", "console", "crypto", "document", "fetch", "globalThis", "history", "isFinite", "isNaN", "location", "navigator", "parseFloat", "parseInt", "queueMicrotask", "requestAnimationFrame", "setInterval", "setTimeout", "structuredClone", "undefined", "window"
|
|
2613
2641
|
])
|
|
2614
2642
|
|
|
2643
|
+
function rewriteEffectWorkers(callback, file, sourceFile, sourceFiles, workerReferences, factory, context) {
|
|
2644
|
+
const visit = node => {
|
|
2645
|
+
const candidate = relativeWorkerCandidate(node, sourceFile)
|
|
2646
|
+
if (candidate) {
|
|
2647
|
+
if (nearestFunction(node) !== callback) throw sourceNodeError(node, sourceFile, "Relative TypeScript Worker construction must be directly inside the inline useEffect() callback, not a nested function")
|
|
2648
|
+
const { worker, url, specifier, options } = validateWorkerCandidate(candidate, sourceFile)
|
|
2649
|
+
const target = resolve(dirname(file), specifier)
|
|
2650
|
+
const sourceRelative = relative(sourceDirectory, target)
|
|
2651
|
+
if (sourceRelative.startsWith(`..${sep}`) || sourceRelative === ".." || resolve(sourceDirectory, sourceRelative) !== target) throw sourceNodeError(url.arguments[0], sourceFile, "Relative TypeScript Worker source must remain under src/")
|
|
2652
|
+
if (!sourceFiles.has(target)) throw sourceNodeError(url.arguments[0], sourceFile, `Relative TypeScript Worker ${JSON.stringify(specifier)} must resolve to an existing .worker.ts file under src/`)
|
|
2653
|
+
const identity = `${sourceRelative.replaceAll(sep, "/")}:${ts.getOriginalNode(node).getStart(sourceFile)}`
|
|
2654
|
+
const placeholder = `/__kudzu_worker_${createHash("sha256").update(identity).digest("hex").slice(0, 16)}__.js`
|
|
2655
|
+
workerReferences.push({ root: target, placeholder })
|
|
2656
|
+
return factory.updateNewExpression(worker, worker.expression, worker.typeArguments, [factory.createStringLiteral(placeholder), options])
|
|
2657
|
+
}
|
|
2658
|
+
return ts.visitEachChild(node, visit, context)
|
|
2659
|
+
}
|
|
2660
|
+
return ts.visitEachChild(callback, visit, context)
|
|
2661
|
+
}
|
|
2662
|
+
|
|
2663
|
+
function rejectWorkerConstructions(expression, sourceFile, message) {
|
|
2664
|
+
const visit = node => {
|
|
2665
|
+
if (relativeWorkerCandidate(node, sourceFile)) throw sourceNodeError(node, sourceFile, message)
|
|
2666
|
+
ts.forEachChild(node, visit)
|
|
2667
|
+
}
|
|
2668
|
+
visit(expression.body ?? expression)
|
|
2669
|
+
}
|
|
2670
|
+
|
|
2671
|
+
function relativeWorkerCandidate(node, sourceFile) {
|
|
2672
|
+
if (!ts.isNewExpression(node) || !ts.isIdentifier(node.expression) || node.expression.text !== "Worker") return undefined
|
|
2673
|
+
const first = node.arguments?.[0]
|
|
2674
|
+
if (!first || !ts.isNewExpression(first) || !ts.isIdentifier(first.expression) || first.expression.text !== "URL") return undefined
|
|
2675
|
+
const specifier = first.arguments?.[0]
|
|
2676
|
+
const base = first.arguments?.[1]
|
|
2677
|
+
const relativeLiteral = ts.isStringLiteral(specifier) && (specifier.text.startsWith("./") || specifier.text.startsWith("../"))
|
|
2678
|
+
if (!relativeLiteral && !(specifier && !ts.isStringLiteral(specifier) && base && isImportMetaUrl(base))) return undefined
|
|
2679
|
+
return { worker: node, url: first, sourceFile }
|
|
2680
|
+
}
|
|
2681
|
+
|
|
2682
|
+
function validateWorkerCandidate(candidate, sourceFile) {
|
|
2683
|
+
const { worker, url } = candidate
|
|
2684
|
+
if (!isUnshadowedGlobal(worker.expression, sourceFile)) throw sourceNodeError(worker.expression, sourceFile, "Relative TypeScript Workers require the unshadowed global Worker constructor")
|
|
2685
|
+
if (!isUnshadowedGlobal(url.expression, sourceFile)) throw sourceNodeError(url.expression, sourceFile, "Relative TypeScript Workers require the unshadowed global URL constructor")
|
|
2686
|
+
if (url.arguments?.length !== 2 || !isImportMetaUrl(url.arguments[1])) throw sourceNodeError(url, sourceFile, "Relative TypeScript Workers require new URL(relativeLiteral, import.meta.url)")
|
|
2687
|
+
const specifierNode = url.arguments[0]
|
|
2688
|
+
if (!ts.isStringLiteral(specifierNode) || !(specifierNode.text.startsWith("./") || specifierNode.text.startsWith("../"))) throw sourceNodeError(specifierNode, sourceFile, "Relative TypeScript Worker paths must be relative string literals")
|
|
2689
|
+
if (/[\\?#]/.test(specifierNode.text) || !specifierNode.text.endsWith(".worker.ts")) throw sourceNodeError(specifierNode, sourceFile, "Relative TypeScript Worker paths must end in .worker.ts")
|
|
2690
|
+
if (worker.arguments?.length !== 2) throw sourceNodeError(worker, sourceFile, 'Relative TypeScript Workers require exactly { type: "module" } as the second argument')
|
|
2691
|
+
const options = worker.arguments[1]
|
|
2692
|
+
if (!ts.isObjectLiteralExpression(options) || options.properties.length !== 1) throw sourceNodeError(options, sourceFile, 'Relative TypeScript Workers require exactly { type: "module" } as the second argument')
|
|
2693
|
+
const property = options.properties[0]
|
|
2694
|
+
const name = ts.isPropertyAssignment(property) && !ts.isComputedPropertyName(property.name) && (ts.isIdentifier(property.name) || ts.isStringLiteral(property.name)) ? property.name.text : undefined
|
|
2695
|
+
if (name !== "type" || !ts.isStringLiteral(property.initializer) || property.initializer.text !== "module") throw sourceNodeError(property, sourceFile, 'Relative TypeScript Workers require exactly { type: "module" } as the second argument')
|
|
2696
|
+
return { worker, url, specifier: specifierNode.text, options }
|
|
2697
|
+
}
|
|
2698
|
+
|
|
2699
|
+
function isImportMetaUrl(node) {
|
|
2700
|
+
return ts.isPropertyAccessExpression(node) && node.name.text === "url" && ts.isMetaProperty(node.expression) && node.expression.keywordToken === ts.SyntaxKind.ImportKeyword && node.expression.name.text === "meta"
|
|
2701
|
+
}
|
|
2702
|
+
|
|
2703
|
+
function isUnshadowedGlobal(identifier, sourceFile) {
|
|
2704
|
+
if (isShadowedIdentifier(identifier, sourceFile)) return false
|
|
2705
|
+
return !sourceFile.statements.some(statement => {
|
|
2706
|
+
if (statementDeclaresName(statement, identifier.text)) return true
|
|
2707
|
+
if (!ts.isImportDeclaration(statement) || !statement.importClause) return false
|
|
2708
|
+
const clause = statement.importClause
|
|
2709
|
+
if (clause.name?.text === identifier.text) return true
|
|
2710
|
+
if (clause.namedBindings && ts.isNamespaceImport(clause.namedBindings)) return clause.namedBindings.name.text === identifier.text
|
|
2711
|
+
return clause.namedBindings && ts.isNamedImports(clause.namedBindings) ? clause.namedBindings.elements.some(entry => entry.name.text === identifier.text) : false
|
|
2712
|
+
})
|
|
2713
|
+
}
|
|
2714
|
+
|
|
2615
2715
|
function nativeCaptureNames(expression, setters) {
|
|
2616
2716
|
return captureNames(expression, expression.body, setters)
|
|
2617
2717
|
}
|
|
@@ -2704,7 +2804,31 @@ function isShadowedIdentifier(node, scopeRoot) {
|
|
|
2704
2804
|
|
|
2705
2805
|
function statementDeclaresName(statement, name) {
|
|
2706
2806
|
if (ts.isVariableStatement(statement)) return statement.declarationList.declarations.some(declaration => bindingNames(declaration.name).includes(name))
|
|
2707
|
-
|
|
2807
|
+
if (ts.isFunctionDeclaration(statement) || ts.isClassDeclaration(statement) || ts.isImportEqualsDeclaration(statement)) return statement.name?.text === name
|
|
2808
|
+
if ((ts.isEnumDeclaration(statement) || ts.isModuleDeclaration(statement)) && !statement.modifiers?.some(modifier => modifier.kind === ts.SyntaxKind.DeclareKeyword)) return ts.isIdentifier(statement.name) && statement.name.text === name
|
|
2809
|
+
return false
|
|
2810
|
+
}
|
|
2811
|
+
|
|
2812
|
+
function rejectOrdinaryWorkerImports(sourceFile, file, sourceFiles) {
|
|
2813
|
+
for (const node of sourceFile.statements) {
|
|
2814
|
+
let specifier
|
|
2815
|
+
let runtime = false
|
|
2816
|
+
if ((ts.isImportDeclaration(node) || ts.isExportDeclaration(node)) && node.moduleSpecifier && ts.isStringLiteral(node.moduleSpecifier)) {
|
|
2817
|
+
specifier = node.moduleSpecifier
|
|
2818
|
+
runtime = runtimeModuleReference(node)
|
|
2819
|
+
} else if (ts.isImportEqualsDeclaration(node) && ts.isExternalModuleReference(node.moduleReference) && ts.isStringLiteral(node.moduleReference.expression)) {
|
|
2820
|
+
specifier = node.moduleReference.expression
|
|
2821
|
+
runtime = !node.isTypeOnly
|
|
2822
|
+
}
|
|
2823
|
+
if (!runtime || !specifier?.text.startsWith(".")) continue
|
|
2824
|
+
let target
|
|
2825
|
+
try {
|
|
2826
|
+
target = resolveSourceImport(file, specifier.text, sourceFiles)
|
|
2827
|
+
} catch {
|
|
2828
|
+
continue
|
|
2829
|
+
}
|
|
2830
|
+
if (target.endsWith(".worker.ts")) throw sourceNodeError(specifier, sourceFile, "Worker source modules cannot be imported or re-exported as ordinary runtime modules; use new Worker(new URL(relative.worker.ts, import.meta.url), { type: \"module\" }) inside an inline useEffect() callback")
|
|
2831
|
+
}
|
|
2708
2832
|
}
|
|
2709
2833
|
|
|
2710
2834
|
function loopDeclaresName(loop, name) {
|
|
@@ -2736,7 +2860,12 @@ function clientImportBindings(sourceFile, file, sourceFiles) {
|
|
|
2736
2860
|
const bindings = new Map()
|
|
2737
2861
|
for (const node of sourceFile.statements) {
|
|
2738
2862
|
if (!ts.isImportDeclaration(node) || !node.importClause || node.importClause.isTypeOnly || !ts.isStringLiteral(node.moduleSpecifier) || !node.moduleSpecifier.text.startsWith(".")) continue
|
|
2739
|
-
|
|
2863
|
+
let target
|
|
2864
|
+
try {
|
|
2865
|
+
target = resolveSourceImport(file, node.moduleSpecifier.text, sourceFiles)
|
|
2866
|
+
} catch (error) {
|
|
2867
|
+
throw sourceNodeError(node.moduleSpecifier, sourceFile, error.message)
|
|
2868
|
+
}
|
|
2740
2869
|
if (node.importClause.name) bindings.set(node.importClause.name.text, { kind: "default", local: node.importClause.name.text, target })
|
|
2741
2870
|
const named = node.importClause.namedBindings
|
|
2742
2871
|
if (named && ts.isNamespaceImport(named)) bindings.set(named.name.text, { kind: "namespace", local: named.name.text, target })
|
|
@@ -2845,6 +2974,71 @@ function printClientImports(entries, handlerPath) {
|
|
|
2845
2974
|
return imports.join("\n")
|
|
2846
2975
|
}
|
|
2847
2976
|
|
|
2977
|
+
async function emitWorkers(references, sourceFiles, assetsDirectory, base, minify) {
|
|
2978
|
+
const roots = [...new Set(references.map(reference => reference.root))].sort()
|
|
2979
|
+
if (!roots.length) return new Map()
|
|
2980
|
+
await validateWorkerGraphs(roots, sourceFiles)
|
|
2981
|
+
const workerDirectory = join(assetsDirectory, "workers")
|
|
2982
|
+
await mkdir(workerDirectory, { recursive: true })
|
|
2983
|
+
const result = await bundle({
|
|
2984
|
+
absWorkingDir: root,
|
|
2985
|
+
entryPoints: roots,
|
|
2986
|
+
outbase: sourceDirectory,
|
|
2987
|
+
outdir: workerDirectory,
|
|
2988
|
+
entryNames: "[dir]/[name]-[hash]",
|
|
2989
|
+
chunkNames: "chunks/[name]-[hash]",
|
|
2990
|
+
bundle: true,
|
|
2991
|
+
splitting: true,
|
|
2992
|
+
format: "esm",
|
|
2993
|
+
platform: "browser",
|
|
2994
|
+
target: "es2022",
|
|
2995
|
+
minify,
|
|
2996
|
+
legalComments: "none",
|
|
2997
|
+
metafile: true,
|
|
2998
|
+
logLevel: "silent"
|
|
2999
|
+
})
|
|
3000
|
+
const emitted = new Map()
|
|
3001
|
+
for (const [output, metadata] of Object.entries(result.metafile.outputs)) {
|
|
3002
|
+
if (!metadata.entryPoint) continue
|
|
3003
|
+
const entry = resolve(root, metadata.entryPoint)
|
|
3004
|
+
const rootReferences = references.filter(reference => reference.root === entry)
|
|
3005
|
+
const outputFile = resolve(root, output)
|
|
3006
|
+
const url = assetPath(base, relative(outputDirectory, outputFile).replaceAll(sep, "/"))
|
|
3007
|
+
for (const reference of rootReferences) emitted.set(reference.placeholder, url)
|
|
3008
|
+
}
|
|
3009
|
+
for (const reference of references) if (!emitted.has(reference.placeholder)) throw new Error(`Worker entry was not emitted: ${relative(root, reference.root)}`)
|
|
3010
|
+
return emitted
|
|
3011
|
+
}
|
|
3012
|
+
|
|
3013
|
+
async function validateWorkerGraphs(roots, sourceFiles) {
|
|
3014
|
+
const visited = new Set()
|
|
3015
|
+
const queue = [...roots]
|
|
3016
|
+
while (queue.length) {
|
|
3017
|
+
const file = queue.shift()
|
|
3018
|
+
if (visited.has(file)) continue
|
|
3019
|
+
visited.add(file)
|
|
3020
|
+
const sourceFile = parseSourceFile(file, await readFile(file, "utf8"))
|
|
3021
|
+
if (containsJsx(sourceFile)) throw sourceNodeError(sourceFile, sourceFile, "Worker modules must not contain JSX")
|
|
3022
|
+
const visit = node => {
|
|
3023
|
+
if (ts.isImportEqualsDeclaration(node)) throw sourceNodeError(node, sourceFile, "TypeScript import-equals declarations are not supported in Worker modules; use a relative ESM import")
|
|
3024
|
+
if (ts.isCallExpression(node) && node.expression.kind === ts.SyntaxKind.ImportKeyword) throw sourceNodeError(node, sourceFile, "Dynamic imports are not supported in Worker modules")
|
|
3025
|
+
if (ts.isCallExpression(node) && ts.isIdentifier(node.expression) && node.expression.text === "require") throw sourceNodeError(node, sourceFile, "require() is not supported in Worker modules")
|
|
3026
|
+
ts.forEachChild(node, visit)
|
|
3027
|
+
}
|
|
3028
|
+
visit(sourceFile)
|
|
3029
|
+
for (const node of sourceFile.statements) {
|
|
3030
|
+
if ((!ts.isImportDeclaration(node) && !ts.isExportDeclaration(node)) || !node.moduleSpecifier || !ts.isStringLiteral(node.moduleSpecifier) || !runtimeModuleReference(node)) continue
|
|
3031
|
+
if (!node.moduleSpecifier.text.startsWith(".")) throw sourceNodeError(node.moduleSpecifier, sourceFile, "Worker modules may only use relative runtime imports")
|
|
3032
|
+
try {
|
|
3033
|
+
queue.push(resolveSourceImport(file, node.moduleSpecifier.text, sourceFiles))
|
|
3034
|
+
} catch (error) {
|
|
3035
|
+
const message = error.message.slice(error.message.indexOf("Relative import"))
|
|
3036
|
+
throw sourceNodeError(node.moduleSpecifier, sourceFile, message)
|
|
3037
|
+
}
|
|
3038
|
+
}
|
|
3039
|
+
}
|
|
3040
|
+
}
|
|
3041
|
+
|
|
2848
3042
|
async function collectClientModules(entries, sourceFiles) {
|
|
2849
3043
|
const modules = new Set()
|
|
2850
3044
|
const queue = [...new Set(entries)]
|
|
@@ -2853,6 +3047,7 @@ async function collectClientModules(entries, sourceFiles) {
|
|
|
2853
3047
|
if (modules.has(file)) continue
|
|
2854
3048
|
const source = await readFile(file, "utf8")
|
|
2855
3049
|
const sourceFile = parseSourceFile(file, source)
|
|
3050
|
+
rejectWorkerConstructions(sourceFile, sourceFile, "Relative TypeScript Worker construction is only supported directly inside an inline useEffect() callback, not imported client helpers")
|
|
2856
3051
|
if (containsJsx(sourceFile)) throw new Error(`${relative(root, file)} Imported client helpers must not contain JSX`)
|
|
2857
3052
|
rejectUnsupportedClientImports(sourceFile, file)
|
|
2858
3053
|
modules.add(file)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kudzujs/core",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.6",
|
|
4
4
|
"description": "HTML-first TSX framework with synchronous state semantics and no virtual DOM",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
"bin/",
|
|
26
26
|
"framework/",
|
|
27
27
|
"GOAL_A.md",
|
|
28
|
+
"GOAL_B.md",
|
|
28
29
|
"README.md",
|
|
29
30
|
"LICENSE"
|
|
30
31
|
],
|