what-core 0.14.0 → 0.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (145) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +64 -38
  3. package/a11y.d.ts +128 -0
  4. package/data.d.ts +294 -0
  5. package/dist/a11y.js +510 -0
  6. package/dist/a11y.min.js +413 -0
  7. package/dist/actions.js +158 -0
  8. package/dist/actions.min.js +112 -0
  9. package/dist/animation.js +481 -0
  10. package/dist/animation.min.js +295 -0
  11. package/dist/async.js +180 -0
  12. package/dist/async.min.js +134 -0
  13. package/dist/components.js +577 -0
  14. package/dist/components.min.js +413 -0
  15. package/dist/data.js +1212 -0
  16. package/dist/data.min.js +918 -0
  17. package/dist/diagnostics.js +187 -0
  18. package/dist/diagnostics.min.js +134 -0
  19. package/dist/dom.js +796 -0
  20. package/dist/dom.min.js +603 -0
  21. package/dist/entries/a11y.js +43 -0
  22. package/dist/entries/a11y.min.js +43 -0
  23. package/dist/entries/data.js +29 -0
  24. package/dist/entries/data.min.js +29 -0
  25. package/dist/entries/dom.js +45 -0
  26. package/dist/entries/dom.min.js +45 -0
  27. package/dist/entries/form.js +31 -0
  28. package/dist/entries/form.min.js +31 -0
  29. package/dist/entries/internal.js +123 -0
  30. package/dist/entries/internal.min.js +123 -0
  31. package/dist/entries/motion.js +23 -0
  32. package/dist/entries/motion.min.js +23 -0
  33. package/dist/entries/ui.js +25 -0
  34. package/dist/entries/ui.min.js +25 -0
  35. package/dist/env.js +5 -0
  36. package/dist/env.min.js +5 -0
  37. package/dist/errors.js +1518 -0
  38. package/dist/errors.min.js +548 -0
  39. package/dist/form.js +936 -0
  40. package/dist/form.min.js +703 -0
  41. package/dist/guardrails.js +84 -0
  42. package/dist/guardrails.min.js +55 -0
  43. package/dist/h.js +232 -0
  44. package/dist/h.min.js +171 -0
  45. package/dist/head.js +241 -0
  46. package/dist/head.min.js +207 -0
  47. package/dist/helpers.js +126 -0
  48. package/dist/helpers.min.js +98 -0
  49. package/dist/hooks.js +255 -0
  50. package/dist/hooks.min.js +153 -0
  51. package/dist/hot.js +215 -0
  52. package/dist/hot.min.js +24 -0
  53. package/dist/hydration-data.js +495 -0
  54. package/dist/hydration-data.min.js +371 -0
  55. package/dist/import-changes.js +273 -0
  56. package/dist/import-changes.min.js +272 -0
  57. package/dist/index.js +95 -0
  58. package/dist/index.min.js +95 -227
  59. package/dist/jsx-dev-runtime.js +10 -0
  60. package/dist/jsx-dev-runtime.min.js +10 -1
  61. package/dist/jsx-runtime.js +27 -0
  62. package/dist/jsx-runtime.min.js +15 -1
  63. package/dist/live.js +54 -0
  64. package/dist/live.min.js +49 -0
  65. package/dist/markup-rules.js +250 -0
  66. package/dist/markup-rules.min.js +213 -0
  67. package/dist/reactive.js +3334 -0
  68. package/dist/reactive.min.js +2033 -0
  69. package/dist/render.js +4746 -0
  70. package/dist/render.min.js +2797 -1
  71. package/dist/scheduler.js +225 -0
  72. package/dist/scheduler.min.js +162 -0
  73. package/dist/selector.js +11 -0
  74. package/dist/selector.min.js +10 -0
  75. package/dist/server-context.js +148 -0
  76. package/dist/server-context.min.js +121 -0
  77. package/dist/skeleton.js +342 -0
  78. package/dist/skeleton.min.js +310 -0
  79. package/dist/store.js +72 -0
  80. package/dist/store.min.js +50 -0
  81. package/dist/testing.js +720 -0
  82. package/dist/testing.min.js +458 -1
  83. package/dist/vnode-adoption.js +19 -0
  84. package/dist/vnode-adoption.min.js +19 -0
  85. package/dist/vnode-renderer.js +61 -0
  86. package/dist/vnode-renderer.min.js +47 -0
  87. package/dom.d.ts +44 -0
  88. package/form.d.ts +218 -0
  89. package/hot.d.ts +35 -0
  90. package/index.d.ts +421 -736
  91. package/internal.d.ts +284 -0
  92. package/jsx-runtime.d.ts +160 -137
  93. package/markup-rules.d.ts +83 -0
  94. package/motion.d.ts +156 -0
  95. package/package.json +117 -3
  96. package/render.d.ts +278 -29
  97. package/src/a11y.js +64 -59
  98. package/src/actions.js +226 -0
  99. package/src/animation.js +105 -57
  100. package/src/async.js +259 -0
  101. package/src/components.js +495 -144
  102. package/src/data.js +1410 -1159
  103. package/src/diagnostics.js +263 -0
  104. package/src/dom.js +1159 -872
  105. package/src/entries/a11y.js +24 -0
  106. package/src/entries/data.js +17 -0
  107. package/src/entries/dom.js +29 -0
  108. package/src/entries/form.js +16 -0
  109. package/src/entries/internal.js +63 -0
  110. package/src/entries/motion.js +13 -0
  111. package/src/entries/ui.js +13 -0
  112. package/src/env-development.js +12 -0
  113. package/src/env.js +33 -0
  114. package/src/errors.js +1144 -168
  115. package/src/form.js +723 -225
  116. package/src/guardrails.js +38 -126
  117. package/src/h.js +227 -152
  118. package/src/head.js +189 -19
  119. package/src/helpers.js +47 -75
  120. package/src/hooks.js +233 -248
  121. package/src/hot.js +316 -0
  122. package/src/hydration-data.js +859 -16
  123. package/src/import-changes.js +156 -0
  124. package/src/index.js +38 -188
  125. package/src/jsx-dev-runtime.js +7 -14
  126. package/src/jsx-runtime.js +25 -9
  127. package/src/live.js +80 -0
  128. package/src/markup-rules.js +387 -0
  129. package/src/reactive.js +4400 -862
  130. package/src/render.js +6502 -2570
  131. package/src/scheduler.js +56 -21
  132. package/src/selector.js +31 -0
  133. package/src/server-context.js +252 -22
  134. package/src/skeleton.js +22 -12
  135. package/src/store.js +17 -31
  136. package/src/testing.js +389 -63
  137. package/src/vnode-adoption.js +48 -0
  138. package/src/vnode-renderer.js +114 -0
  139. package/testing.d.ts +81 -1
  140. package/ui.d.ts +20 -0
  141. package/dist/chunk-6YOUZ6T4.min.js +0 -1
  142. package/dist/chunk-FN6HXME3.min.js +0 -11
  143. package/dist/chunk-VKZY7JPN.min.js +0 -1
  144. package/src/agent-context.js +0 -127
  145. package/src/warnings.js +0 -193
package/src/hot.js ADDED
@@ -0,0 +1,316 @@
1
+ // What Framework - Hot replacement in the dev server
2
+ //
3
+ // what-compiler's Vite plugin, in development, adds calls to this module to
4
+ // each module of the app it compiles for the browser (what-compiler
5
+ // src/hot.js writes them), and nothing else imports it: a production build,
6
+ // a server render and a page without the dev server never load it.
7
+ //
8
+ // import { _$hot } from 'what-framework/hot';
9
+ // const _$h = _$hot(import.meta.hot, 1, 'src/Counter.jsx');
10
+ // import.meta.hot.accept(_$h.accept);
11
+ // _$h.component('Counter', Counter, 'k3x9a', () => [increment]);
12
+ // const count = _$h.keep('count', 'p1q2b', () => [], () => signal(0, 'count'));
13
+ // _$h.done(['count']);
14
+ //
15
+ // A module's state between its versions is in `import.meta.hot.data`:
16
+ //
17
+ // - Each component it declares has a record, which every version of the
18
+ // function points to. A component renders through its record (render.js
19
+ // hotInstance), so a parent that holds an older version renders the current
20
+ // code. A new version whose source, or the source of the module's functions
21
+ // it calls, or an import or a kept value it reads, differs from the running
22
+ // one's builds each live instance again in place (render.js _hotSwap), once
23
+ // every module of the update has run. The others keep running the code
24
+ // they have, and their state.
25
+ // - An instance's state (each `signal()`, `useState()`, `useSignal()` and
26
+ // `useReducer()` call in its body and in the module's custom hooks it
27
+ // calls, which the compiler wraps as `_$h.state(site, () => call)`) goes
28
+ // to the instance its new version builds while the component's state
29
+ // signature (the source of those calls, in order) is unchanged, as React
30
+ // keeps hook state (render.js _hotState).
31
+ // - A module-scope value that is not a function (`const count = signal(0)`,
32
+ // `createStore(...)`, `new Map()`) is kept: the new version gets the old
33
+ // value while its initializer's source and what it reads are unchanged, so
34
+ // editing an action does not lose the state it acts on.
35
+ // - An export the new version changed, other than a component (an action
36
+ // whose code changed, a value made again), cannot reach the modules that
37
+ // imported the old one, so the update goes to them (`hot.invalidate()`),
38
+ // and up to the entry, where Vite reloads the page.
39
+ //
40
+ // The plugin reloads the page for a module that runs statements at its top
41
+ // level (a mount(), an effect, a listener): running it again would run them
42
+ // twice. A failed update (a compile error, a throw while the new code runs)
43
+ // shows in Vite's overlay and in the diagnostics (ERR_HOT_UPDATE_FAILED), and
44
+ // the page keeps the code it had.
45
+
46
+ import { __DEV__, __devtools } from './reactive.js';
47
+ import { _installHot, _hotSwap, _hotState } from './render.js';
48
+ import { report, recordObservation } from './diagnostics.js';
49
+
50
+ const HOT = Symbol('what.hot');
51
+
52
+ // Records whose code changed, waiting for the end of the update.
53
+ /** @type {Set<any>} */
54
+ const changed = new Set();
55
+ // The files whose new versions ran in the update now in flight.
56
+ /** @type {Set<string>} */
57
+ const updated = new Set();
58
+ // The payloads of each event already handled: every module's context listens.
59
+ /** @type {Map<string, WeakSet<object>>} */
60
+ const seen = new Map();
61
+ let installed = false;
62
+
63
+ function recordOf(Component) {
64
+ return Component[HOT];
65
+ }
66
+
67
+ // What another runtime knows a component it refreshes by, across that
68
+ // component's versions (_hotIdentity), or undefined.
69
+ /** @type {((fn: Function) => unknown) | null} */
70
+ let identityOf = null;
71
+
72
+ // A dependency as a version compares it: a component by its record, or by
73
+ // what the runtime that refreshes it knows it by.
74
+ function stable(value) {
75
+ if (typeof value !== 'function') return value;
76
+ if (value[HOT] !== undefined) return value[HOT];
77
+ const known = identityOf !== null ? identityOf(value) : undefined;
78
+ return known === undefined ? value : known;
79
+ }
80
+
81
+ /**
82
+ * @internal what-react's Fast Refresh (refresh.js): what a React component
83
+ * is known by across its versions (its family), or undefined. A native
84
+ * component that renders one (a React island in a native file) does not
85
+ * change when only the React component's version does: Fast Refresh
86
+ * renders the component's instances again in place, so the native one, and
87
+ * the other islands it holds, keep their state.
88
+ * @param {(fn: Function) => unknown} of
89
+ */
90
+ export function _hotIdentity(of) {
91
+ if (__DEV__) identityOf = of;
92
+ }
93
+
94
+ function sameDeps(a, b) {
95
+ if (a === null || b === null || a.length !== b.length) return false;
96
+ for (let i = 0; i < a.length; i++) if (!Object.is(a[i], b[i])) return false;
97
+ return true;
98
+ }
99
+
100
+ function readDeps(deps) {
101
+ try {
102
+ return deps().map(stable);
103
+ } catch {
104
+ // A binding not initialized yet (an import cycle): compare as changed.
105
+ return null;
106
+ }
107
+ }
108
+
109
+ // A page served without Vite's hot updates: the module runs as written.
110
+ const INERT = {
111
+ accept() {},
112
+ component: (_name, fn) => fn,
113
+ state: (_site, init) => init(),
114
+ fn() {},
115
+ keep: (_name, _sig, _deps, init) => init(),
116
+ done() {},
117
+ };
118
+
119
+ /**
120
+ * @internal A module's hot context: what-compiler writes the calls, from
121
+ * the module's own source, in the dev server.
122
+ * @param {any} hot the module's `import.meta.hot`
123
+ * @param {number} accepts 1 for a module that replaces itself (it accepts its own updates), else 0
124
+ * @param {string} file the module's path from the project root, for reports
125
+ */
126
+ export function _$hot(hot, accepts, file) {
127
+ return __DEV__ && hot != null ? hotModule(hot, accepts, file) : INERT;
128
+ }
129
+
130
+ function hotModule(hot, accepts, file) {
131
+ if (!installed) {
132
+ installed = true;
133
+ _installHot(recordOf);
134
+ }
135
+ const listen = (event, fn) => {
136
+ if (!seen.has(event)) seen.set(event, new WeakSet());
137
+ const handled = /** @type {WeakSet<object>} */ (seen.get(event));
138
+ hot.on(event, (payload) => {
139
+ if (payload !== null && typeof payload === 'object') {
140
+ if (handled.has(payload)) return;
141
+ handled.add(payload);
142
+ }
143
+ fn(payload);
144
+ });
145
+ };
146
+ // Every context listens, and the first to hear an event handles it: Vite
147
+ // drops a module's listeners when its next version starts.
148
+ listen('vite:beforeUpdate', () => updated.clear());
149
+ listen('vite:afterUpdate', flush);
150
+ listen('vite:error', (payload) => compileFailed(payload && payload.err));
151
+ /** @type {any} */
152
+ const prev = hot.data.what;
153
+ const next = { records: new Map(), values: new Map(), fns: new Map(), verdict: 'accept' };
154
+ /** @type {Array<{ record: any, fn: Function, sig: string | null, deps: () => any[], state: string | null }>} */
155
+ const registered = [];
156
+
157
+ return {
158
+ /** The callback of `import.meta.hot.accept()`: the verdict of the version that ran. */
159
+ accept(module) {
160
+ if (module === undefined) return;
161
+ const verdict = hot.data.what ? hot.data.what.verdict : 'accept';
162
+ if (verdict === 'invalidate') hot.invalidate('an export changed; its importers update');
163
+ },
164
+
165
+ /**
166
+ * `key`: the name it is exported under, else its own. `state`: its state
167
+ * signature; null (or left out) keeps none.
168
+ */
169
+ component(key, fn, sig, deps, state = null) {
170
+ let record = prev ? prev.records.get(key) : undefined;
171
+ if (record === undefined) record = { name: fn.name || key, file, impl: fn, sig, deps: null, state, slots: new Set() };
172
+ try {
173
+ Object.defineProperty(fn, HOT, { value: record, configurable: true });
174
+ } catch { /* a frozen function renders as it is */ }
175
+ if (fn.name === '') Object.defineProperty(fn, 'name', { value: key, configurable: true });
176
+ next.records.set(key, record);
177
+ registered.push({ record, fn, sig, deps, state });
178
+ return fn;
179
+ },
180
+
181
+ state: _hotState,
182
+
183
+ fn(name, sig, deps) {
184
+ next.fns.set(name, { sig, deps, read: /** @type {any[] | null} */ (null) });
185
+ },
186
+
187
+ keep(name, sig, deps, init) {
188
+ const was = prev ? prev.values.get(name) : undefined;
189
+ const read = readDeps(deps);
190
+ if (was !== undefined && sig !== null && was.sig === sig && read !== null && sameDeps(was.deps, read)) {
191
+ next.values.set(name, { sig, deps: read, value: was.value, same: true });
192
+ return was.value;
193
+ }
194
+ let value;
195
+ try {
196
+ value = init();
197
+ } catch (error) {
198
+ runFailed(error, file, `running ${name}'s initializer`);
199
+ throw error;
200
+ }
201
+ // A value no earlier version had is not held by an importer either.
202
+ next.values.set(name, { sig, deps: read, value, same: was === undefined });
203
+ return value;
204
+ },
205
+
206
+ /**
207
+ * The module ran to its end: decide each component's update, and what
208
+ * the old version's accept callback does. `exported` names the module's
209
+ * own exports that are not components.
210
+ * @param {string[]} exported
211
+ */
212
+ done(exported) {
213
+ for (const { record, fn, sig, deps, state } of registered) {
214
+ const read = readDeps(deps);
215
+ if (record.impl === fn) {
216
+ record.deps = read;
217
+ continue;
218
+ }
219
+ if (sig !== null && sig === record.sig && read !== null && sameDeps(read, record.deps)) continue;
220
+ // What the record goes back to when its instances cannot take the new code (flush).
221
+ record.was = { impl: record.impl, name: record.name, sig: record.sig, deps: record.deps, state: record.state };
222
+ record.impl = fn;
223
+ record.name = fn.name;
224
+ record.sig = sig;
225
+ record.deps = read;
226
+ record.state = state;
227
+ changed.add(record);
228
+ }
229
+ for (const entry of next.fns.values()) entry.read = readDeps(entry.deps);
230
+ if (prev) {
231
+ for (const name of exported) {
232
+ const value = next.values.get(name);
233
+ if (value !== undefined ? !value.same : fnChanged(prev.fns.get(name), next.fns.get(name))) next.verdict = 'invalidate';
234
+ }
235
+ if (!accepts) next.verdict = 'invalidate';
236
+ }
237
+ hot.data.what = next;
238
+ updated.add(file);
239
+ },
240
+ };
241
+ }
242
+
243
+ // Whether an exported function's new version differs from the one its
244
+ // importers hold: its source, or what it reads.
245
+ function fnChanged(was, now) {
246
+ if (was === undefined) return false;
247
+ if (now === undefined || now.sig === null || was.sig !== now.sig) return true;
248
+ return was.read === null || now.read === null || !sameDeps(was.read, now.read);
249
+ }
250
+
251
+ // The update is in: build the changed components' instances again.
252
+ function flush(payload) {
253
+ if (changed.size === 0 && updated.size === 0) return;
254
+ const records = [...changed];
255
+ changed.clear();
256
+ const files = payload && Array.isArray(payload.updates) ? payload.updates.map((u) => u.path) : [...updated];
257
+ updated.clear();
258
+ const started = performance.now();
259
+ const { swapped, kept, failed } = _hotSwap(records);
260
+ const ms = Math.round((performance.now() - started) * 10) / 10;
261
+ for (const { record, error } of failed) {
262
+ // No instance took the new code, so none made later does either.
263
+ Object.assign(record, record.was);
264
+ runFailed(error, record.file, `rendering <${record.name}>`);
265
+ }
266
+ if (__devtools) {
267
+ recordObservation(__devtools, 'hmr:update', null, {
268
+ files,
269
+ components: records.map((r) => r.name),
270
+ instances: swapped,
271
+ kept,
272
+ ms,
273
+ ...(failed.length ? { failed: failed.map(({ record, error }) => ({ component: record.name, message: messageOf(error) })) } : {}),
274
+ });
275
+ }
276
+ }
277
+
278
+ /**
279
+ * @internal what-react's Fast Refresh (refresh.js): a refresh of React files
280
+ * it applied, recorded as a hot update is (`hmr:update`, read with
281
+ * what_events { types: ["hmr"] }): `files`, the components that rendered
282
+ * again with their state and those `remounted` fresh, the `instances` either
283
+ * touched, `ms`, and what `failed`.
284
+ * @param {{ files: string[], components: string[], remounted: string[], instances: number, ms: number, failed?: Array<{ component: string, message: string }> }} record
285
+ */
286
+ export function _hotUpdated(record) {
287
+ if (__DEV__ && __devtools) recordObservation(__devtools, 'hmr:update', null, { kind: 'refresh', ...record });
288
+ }
289
+
290
+ function messageOf(error) {
291
+ return error instanceof Error ? error.message : String(error);
292
+ }
293
+
294
+ // The new code threw while it ran: the overlay shows it, the diagnostics
295
+ // record it, and the page keeps the code it had.
296
+ function runFailed(error, file, phase) {
297
+ report('ERR_HOT_UPDATE_FAILED', { data: { file, phase, message: messageOf(error) }, subject: { kind: 'module', name: file, id: file } });
298
+ console.error(`[what] The edit to ${file} was not applied: ${phase} threw.`, error);
299
+ const Overlay = typeof customElements !== 'undefined' ? customElements.get('vite-error-overlay') : undefined;
300
+ if (Overlay === undefined) return;
301
+ for (const open of document.querySelectorAll('vite-error-overlay')) /** @type {any} */ (open).close();
302
+ const err = error instanceof Error ? error : new Error(String(error));
303
+ document.body.appendChild(new Overlay({
304
+ message: `${phase} threw: ${err.message}\nThe page keeps running the code it had. Save a fix to apply it in place.`,
305
+ stack: err.stack || '',
306
+ id: file,
307
+ plugin: 'what-hot',
308
+ }));
309
+ }
310
+
311
+ // A compile error: Vite's overlay shows it already.
312
+ function compileFailed(err) {
313
+ if (err == null) return;
314
+ const file = err.id || err.loc?.file || '';
315
+ report('ERR_HOT_UPDATE_FAILED', { data: { file, phase: 'compiling it', message: String(err.message || '').split('\n')[0] }, subject: { kind: 'module', name: file, id: file } });
316
+ }