@uniflowed/vite 0.0.0-alpha.9 → 0.1.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/driver.js +1854 -190
- package/index.js +931 -121
- package/internal/a11y-runtime.js +234 -0
- package/internal/a11y.js +102 -0
- package/internal/assets.js +366 -18
- package/internal/barrel-imports.js +459 -0
- package/internal/compile-assets.js +67 -0
- package/internal/config.js +30 -3
- package/internal/dev-state.js +161 -0
- package/internal/devtools.js +117 -0
- package/internal/diagnostics.js +369 -0
- package/internal/events.js +8 -5
- package/internal/flight.js +803 -0
- package/internal/flow-keywords.js +1 -1
- package/internal/frontmatter.js +33 -0
- package/internal/http.js +21 -2
- package/internal/image-endpoint.js +118 -0
- package/internal/instrumentation.js +23 -0
- package/internal/module-graph.js +134 -0
- package/internal/native-web.js +26 -0
- package/internal/openapi.js +218 -0
- package/internal/relay.js +74 -0
- package/internal/routes.js +1294 -81
- package/internal/rsc.js +335 -11
- package/internal/serve.js +583 -29
- package/internal/server-components.js +109 -0
- package/internal/worker-builtins.js +170 -0
- package/package.json +28 -6
package/internal/rsc.js
CHANGED
|
@@ -17,10 +17,13 @@
|
|
|
17
17
|
//
|
|
18
18
|
// Dropping a single Server Component from the client bundle is what Next.js
|
|
19
19
|
// does, and it works there because the browser is handed a Flight payload
|
|
20
|
-
// describing the tree the server rendered. uf
|
|
21
|
-
// `packages/router/
|
|
22
|
-
//
|
|
23
|
-
//
|
|
20
|
+
// describing the tree the server rendered. uf's payload
|
|
21
|
+
// (`packages/router/internal/payload.js`) carries the route's *data* and not
|
|
22
|
+
// its tree — that half needs a second React module graph, see
|
|
23
|
+
// ubugeeei-prod/uf#519 — so `packages/router/client.js` still hydrates by
|
|
24
|
+
// re-rendering the matched tree from the same modules the server rendered it
|
|
25
|
+
// from, and a module missing from the client bundle is a module React cannot
|
|
26
|
+
// hydrate. What *can* be dropped is a
|
|
24
27
|
// route the browser never renders at all — one where no client boundary is
|
|
25
28
|
// reachable from the page, its layouts, its loading fallbacks or the
|
|
26
29
|
// boundaries that cover it. Nothing under it is ever re-rendered in the
|
|
@@ -35,7 +38,7 @@
|
|
|
35
38
|
// route uf has positively decided needs no browser — and a manifest that is
|
|
36
39
|
// missing, unreadable, or written by an older uf removes nothing at all.
|
|
37
40
|
|
|
38
|
-
import { readFileSync } from "node:fs";
|
|
41
|
+
import { readFileSync, statSync } from "node:fs";
|
|
39
42
|
import path from "node:path";
|
|
40
43
|
|
|
41
44
|
/** Environment variable naming the manifest, set by `uf build` and `uf dev`. */
|
|
@@ -44,13 +47,12 @@ export const RSC_MANIFEST_ENV = "UF_RSC_MANIFEST";
|
|
|
44
47
|
/**
|
|
45
48
|
* The manifest schema this understands.
|
|
46
49
|
*
|
|
47
|
-
* Version
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
* `"reaches-boundary"`, and quietly drop every route from the client bundle.
|
|
50
|
+
* Version 3 lets client boundaries name package specifiers as well as project
|
|
51
|
+
* paths. An older manifest is therefore refused rather than read
|
|
52
|
+
* optimistically: it cannot know a route imports a package client module, and
|
|
53
|
+
* quietly dropping that route would be the worst possible answer.
|
|
52
54
|
*/
|
|
53
|
-
const SUPPORTED_VERSION =
|
|
55
|
+
const SUPPORTED_VERSION = 3;
|
|
54
56
|
|
|
55
57
|
/**
|
|
56
58
|
* Read the RSC manifest, or `null` when there is nothing usable to read.
|
|
@@ -90,6 +92,14 @@ function clientModules(manifest) {
|
|
|
90
92
|
module.environment === "client" || module.proximity === "reaches-boundary",
|
|
91
93
|
);
|
|
92
94
|
}
|
|
95
|
+
for (const boundary of manifest.clientBoundaries ?? []) {
|
|
96
|
+
if (boundary == null || typeof boundary.importer !== "string") continue;
|
|
97
|
+
modules.set(boundary.importer, true);
|
|
98
|
+
}
|
|
99
|
+
for (const root of manifest.clientBundleRoots ?? []) {
|
|
100
|
+
if (root == null || root.kind !== "module" || typeof root.path !== "string") continue;
|
|
101
|
+
modules.set(root.path, true);
|
|
102
|
+
}
|
|
93
103
|
return modules;
|
|
94
104
|
}
|
|
95
105
|
|
|
@@ -135,17 +145,331 @@ export function clientRouteFilter(manifest, root, boundaries = {}) {
|
|
|
135
145
|
|
|
136
146
|
const notFound = boundaries.notFound ?? [];
|
|
137
147
|
const errors = boundaries.errors ?? [];
|
|
148
|
+
const needsSlot = slotPredicate(needed);
|
|
138
149
|
|
|
139
150
|
return (route) => {
|
|
140
151
|
if (needed(route.page)) return true;
|
|
141
152
|
if (route.layouts.some(needed)) return true;
|
|
142
153
|
if ((route.loading ?? []).some((entry) => needed(entry.module))) return true;
|
|
154
|
+
if ((route.templates ?? []).some((entry) => needed(entry.module))) return true;
|
|
155
|
+
// A slot renders inside this route, so a `"use client"` anywhere in one is
|
|
156
|
+
// this route's reason to ship. Without this line a page whose only
|
|
157
|
+
// interactive part is in a slot would be dropped from the client bundle
|
|
158
|
+
// and served as a document — rendered correctly and never hydrated, which
|
|
159
|
+
// is the quietest way a feature can be half-implemented.
|
|
160
|
+
if ((route.slots ?? []).some(needsSlot)) return true;
|
|
161
|
+
// A boundary with no module of its own is the record the scan synthesises
|
|
162
|
+
// at the router root, and what renders there is the framework's own page —
|
|
163
|
+
// already in `@uniflowed/router`, reaching nothing this project wrote. It
|
|
164
|
+
// is skipped rather than left to `needed`, whose answer for a value that is
|
|
165
|
+
// not a file is "assume it is needed": that answer is right for a path the
|
|
166
|
+
// manifest has never heard of and wrong for the absence of a path, and
|
|
167
|
+
// taking it here would have kept every page of every project in the client
|
|
168
|
+
// bundle. See ubugeeei-prod/uf#351.
|
|
143
169
|
for (const boundary of notFound) {
|
|
170
|
+
if (boundary.page == null) continue;
|
|
144
171
|
if (covers(boundary.path, route.path) && needed(boundary.page)) return true;
|
|
145
172
|
}
|
|
146
173
|
for (const boundary of errors) {
|
|
174
|
+
if (boundary.module == null) continue;
|
|
147
175
|
if (covers(boundary.path, route.path) && needed(boundary.module)) return true;
|
|
148
176
|
}
|
|
149
177
|
return false;
|
|
150
178
|
};
|
|
151
179
|
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Whether anything in a slot tree has to reach the browser.
|
|
183
|
+
*
|
|
184
|
+
* Recursive because slots nest: a slot's own layout may declare slots of its
|
|
185
|
+
* own, and a `"use client"` at any depth is still inside the page this route
|
|
186
|
+
* renders.
|
|
187
|
+
*
|
|
188
|
+
* @param {ReadonlyArray<{
|
|
189
|
+
* defaultPage: ?string,
|
|
190
|
+
* routes: ReadonlyArray<{
|
|
191
|
+
* page: string,
|
|
192
|
+
* layouts: ReadonlyArray<string>,
|
|
193
|
+
* loading?: ReadonlyArray<{module: string}>,
|
|
194
|
+
* templates?: ReadonlyArray<{module: string}>,
|
|
195
|
+
* slots: ReadonlyArray<*>,
|
|
196
|
+
* }>,
|
|
197
|
+
* @param {(file: ?string) => boolean} needed
|
|
198
|
+
* @returns {(slot: *) => boolean}
|
|
199
|
+
*/
|
|
200
|
+
function slotPredicate(needed) {
|
|
201
|
+
const cache = new WeakMap();
|
|
202
|
+
const needsSlot = (slot) => {
|
|
203
|
+
const cached = cache.get(slot);
|
|
204
|
+
if (cached !== undefined) return cached;
|
|
205
|
+
// A slot that intercepts anything needs the client router on every page it
|
|
206
|
+
// is rendered on, whatever its modules are. An intercepting route renders
|
|
207
|
+
// only for a client navigation that starts on such a page, and a page
|
|
208
|
+
// served as a document with no router hydrated on it is a page no
|
|
209
|
+
// navigation starts from — so dropping it would leave the interception a
|
|
210
|
+
// file nothing ever renders, which is the silence #267 is about.
|
|
211
|
+
let answer = (slot.intercepts ?? []).length > 0;
|
|
212
|
+
if (slot.defaultPage != null && needed(slot.defaultPage)) {
|
|
213
|
+
answer = true;
|
|
214
|
+
}
|
|
215
|
+
for (const route of slot.routes) {
|
|
216
|
+
if (answer) break;
|
|
217
|
+
if (
|
|
218
|
+
needed(route.page) ||
|
|
219
|
+
route.layouts.some(needed) ||
|
|
220
|
+
(route.loading ?? []).some((entry) => needed(entry.module)) ||
|
|
221
|
+
(route.templates ?? []).some((entry) => needed(entry.module)) ||
|
|
222
|
+
route.slots.some(needsSlot)
|
|
223
|
+
) {
|
|
224
|
+
answer = true;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
cache.set(slot, answer);
|
|
228
|
+
return answer;
|
|
229
|
+
};
|
|
230
|
+
return needsSlot;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// ---------------------------------------------------------------------------
|
|
234
|
+
// Server actions
|
|
235
|
+
//
|
|
236
|
+
// The other half of the split, and the one the manifest was already carrying
|
|
237
|
+
// an answer for. `serverActions` in the manifest is every action `uf_rsc`
|
|
238
|
+
// decided is a *callable endpoint* — an action some module that can hand it
|
|
239
|
+
// across a client boundary reaches — with the keyed id
|
|
240
|
+
// `crates/uf_rsc/src/action.rs` derived for it. An action nothing exposes is
|
|
241
|
+
// tracked in the registry and never written here, so a table built out of this
|
|
242
|
+
// file cannot contain a row that was not meant to be dialable.
|
|
243
|
+
//
|
|
244
|
+
// Two tables come out of it, for the two graphs:
|
|
245
|
+
//
|
|
246
|
+
// * `serverActionModules` is the browser's. It says, for each `"use server"`
|
|
247
|
+
// file, which exports become `createServerReference` calls — and the plugin
|
|
248
|
+
// answers that source *instead of the file*, so the module's body never
|
|
249
|
+
// enters the client graph and neither does anything only it imported.
|
|
250
|
+
// * `serverActionTable` is the server's. It is what `virtual:uf/actions`
|
|
251
|
+
// emits and what `createActionDispatcher` dials into.
|
|
252
|
+
//
|
|
253
|
+
// Both are keyed on the id and nothing else. No request-derived value ever
|
|
254
|
+
// becomes a path, a specifier or an export name here or downstream; see the
|
|
255
|
+
// header of `packages/router/internal/action-endpoint.js`.
|
|
256
|
+
// ---------------------------------------------------------------------------
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* The request header carrying an action id, lowercased as Node delivers it.
|
|
260
|
+
*
|
|
261
|
+
* A second spelling of `ACTION_HEADER` in
|
|
262
|
+
* `packages/router/internal/action-wire.js`, and it has to be one: this module
|
|
263
|
+
* is plain JavaScript the Vite host imports before any transform, and that one
|
|
264
|
+
* is Flow, which Node cannot import at all. `RSC_MANIFEST_ENV` above is the
|
|
265
|
+
* same situation with `crates/uf_rsc/src/manifest.rs`. What keeps a second
|
|
266
|
+
* spelling from becoming a second answer is
|
|
267
|
+
* `tests/library/server-actions.test.js`, which reads both and compares them.
|
|
268
|
+
*/
|
|
269
|
+
export const ACTION_HEADER = "uf-action";
|
|
270
|
+
|
|
271
|
+
/** The id an action row must carry: 64 lowercase hexadecimal characters. */
|
|
272
|
+
function isActionId(value) {
|
|
273
|
+
if (typeof value !== "string" || value.length !== 64) return false;
|
|
274
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
275
|
+
const code = value.charCodeAt(index);
|
|
276
|
+
const digit = code >= 0x30 && code <= 0x39;
|
|
277
|
+
const lower = code >= 0x61 && code <= 0x66;
|
|
278
|
+
if (!digit && !lower) return false;
|
|
279
|
+
}
|
|
280
|
+
return true;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Whether a name can be written as `export const <name>`.
|
|
285
|
+
*
|
|
286
|
+
* The scanner only ever produces identifiers, so this refuses nothing a real
|
|
287
|
+
* project has. It is here because the alternative to refusing is emitting a
|
|
288
|
+
* module that does not parse, and a generated file that does not parse fails a
|
|
289
|
+
* build somewhere far from the module that caused it. `default` is handled by
|
|
290
|
+
* the caller, which writes `export default`.
|
|
291
|
+
*/
|
|
292
|
+
function isExportableName(name) {
|
|
293
|
+
if (typeof name !== "string" || name.length === 0) return false;
|
|
294
|
+
const first = name.charCodeAt(0);
|
|
295
|
+
const startish = (code) =>
|
|
296
|
+
(code >= 0x41 && code <= 0x5a) ||
|
|
297
|
+
(code >= 0x61 && code <= 0x7a) ||
|
|
298
|
+
code === 0x24 ||
|
|
299
|
+
code === 0x5f;
|
|
300
|
+
if (!startish(first)) return false;
|
|
301
|
+
for (let index = 1; index < name.length; index += 1) {
|
|
302
|
+
const code = name.charCodeAt(index);
|
|
303
|
+
if (!startish(code) && !(code >= 0x30 && code <= 0x39)) return false;
|
|
304
|
+
}
|
|
305
|
+
return true;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/** Every callable action of the manifest, in the manifest's own order. */
|
|
309
|
+
function callableActions(manifest) {
|
|
310
|
+
if (manifest == null || !Array.isArray(manifest.serverActions)) return [];
|
|
311
|
+
return manifest.serverActions.filter(
|
|
312
|
+
(action) =>
|
|
313
|
+
action != null &&
|
|
314
|
+
isActionId(action.id) &&
|
|
315
|
+
typeof action.module === "string" &&
|
|
316
|
+
action.module !== "" &&
|
|
317
|
+
// An inline `"use server"` closure has no export name to import, so it
|
|
318
|
+
// has no reference in the client bundle and no row in the server's
|
|
319
|
+
// table. It is in the manifest, and reaching it needs the payload
|
|
320
|
+
// ubugeeei-prod/uf#252 is about.
|
|
321
|
+
action.kind === "module-export" &&
|
|
322
|
+
isExportableName(action.export === "default" ? "default_" : action.export),
|
|
323
|
+
);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* The absolute path of a module the manifest names, or `null`.
|
|
328
|
+
*
|
|
329
|
+
* The manifest's paths are project-relative with forward slashes and were
|
|
330
|
+
* written by a walk that already refused anything outside the root; joined
|
|
331
|
+
* here and checked again, because a path that escapes the project is a path
|
|
332
|
+
* this plugin would otherwise hand to Rollup as a module to emit.
|
|
333
|
+
*/
|
|
334
|
+
function moduleFile(root, relative) {
|
|
335
|
+
const joined = path.resolve(root, relative);
|
|
336
|
+
const inside = path.relative(root, joined);
|
|
337
|
+
if (inside === "" || inside.startsWith("..") || path.isAbsolute(inside)) return null;
|
|
338
|
+
return joined;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* Which exports of each `"use server"` file become references in the browser.
|
|
343
|
+
*
|
|
344
|
+
* Keyed by absolute path, because that is what Vite's `load` hook is given.
|
|
345
|
+
* A file with no callable action is absent rather than present-and-empty: the
|
|
346
|
+
* plugin substitutes a module only for a key it finds, and substituting an
|
|
347
|
+
* empty module for a file something imports would be a build error in place of
|
|
348
|
+
* a working import.
|
|
349
|
+
*
|
|
350
|
+
* @param {object | null} manifest from {@link readRscManifest}
|
|
351
|
+
* @param {string} root absolute project root
|
|
352
|
+
* @returns {Map<string, Array<{id: string, module: string, export: string}>>}
|
|
353
|
+
*/
|
|
354
|
+
export function serverActionModules(manifest, root) {
|
|
355
|
+
const modules = new Map();
|
|
356
|
+
for (const action of callableActions(manifest)) {
|
|
357
|
+
const file = moduleFile(root, action.module);
|
|
358
|
+
if (file == null) continue;
|
|
359
|
+
const rows = modules.get(file);
|
|
360
|
+
const row = { id: action.id, module: action.module, export: action.export };
|
|
361
|
+
if (rows === undefined) modules.set(file, [row]);
|
|
362
|
+
else rows.push(row);
|
|
363
|
+
}
|
|
364
|
+
return modules;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* Every callable action, as the server's dispatcher table.
|
|
369
|
+
*
|
|
370
|
+
* @param {object | null} manifest from {@link readRscManifest}
|
|
371
|
+
* @param {string} root absolute project root
|
|
372
|
+
* @returns {Array<{id: string, module: string, export: string, file: string}>}
|
|
373
|
+
*/
|
|
374
|
+
export function serverActionTable(manifest, root) {
|
|
375
|
+
const rows = [];
|
|
376
|
+
for (const action of callableActions(manifest)) {
|
|
377
|
+
const file = moduleFile(root, action.module);
|
|
378
|
+
if (file == null) continue;
|
|
379
|
+
rows.push({ id: action.id, module: action.module, export: action.export, file });
|
|
380
|
+
}
|
|
381
|
+
return rows;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* The client bundle's stand-in for one `"use server"` module.
|
|
386
|
+
*
|
|
387
|
+
* What the browser gets in place of the file: one `createServerReference` per
|
|
388
|
+
* callable export, an id each, and nothing the module itself imported. This is
|
|
389
|
+
* the whole of how a database handle reached only through an action stays on
|
|
390
|
+
* the server — `crates/uf_rsc/src/graph/build.rs` colours the module server for
|
|
391
|
+
* the same reason, so that the analysis and the bundle agree about it.
|
|
392
|
+
*
|
|
393
|
+
* @param {Array<{id: string, module: string, export: string}>} actions
|
|
394
|
+
*/
|
|
395
|
+
export function actionReferenceSource(actions) {
|
|
396
|
+
const lines = ['import { createServerReference } from "@uniflowed/router/action";', ""];
|
|
397
|
+
for (const action of actions) {
|
|
398
|
+
const reference = `createServerReference(${JSON.stringify(action.id)}, ${JSON.stringify(
|
|
399
|
+
`${action.module}#${action.export}`,
|
|
400
|
+
)})`;
|
|
401
|
+
lines.push(
|
|
402
|
+
action.export === "default"
|
|
403
|
+
? `export default ${reference};`
|
|
404
|
+
: `export const ${action.export} = ${reference};`,
|
|
405
|
+
);
|
|
406
|
+
}
|
|
407
|
+
return `${lines.join("\n")}\n`;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* The source of `virtual:uf/actions`: the table the endpoint dials into.
|
|
412
|
+
*
|
|
413
|
+
* One `import()` thunk per file rather than one per action, so a module with
|
|
414
|
+
* four actions is one chunk of the server bundle and not four. Lazy for the
|
|
415
|
+
* reason the handler table is: an action module is loaded when an action in it
|
|
416
|
+
* is called, and a project's actions are not something every request should
|
|
417
|
+
* pay to import.
|
|
418
|
+
*
|
|
419
|
+
* With no manifest the table is empty and every action call is a `404` — the
|
|
420
|
+
* same answer a project driving Vite itself gets for the route split, and for
|
|
421
|
+
* the same reason: uf will not guess at an analysis it was not given.
|
|
422
|
+
*
|
|
423
|
+
* @param {Array<{id: string, module: string, export: string, file: string}>} actions
|
|
424
|
+
*/
|
|
425
|
+
export function actionsModuleSource(actions) {
|
|
426
|
+
const loaders = new Map();
|
|
427
|
+
const declarations = [];
|
|
428
|
+
const loaderId = (file) => {
|
|
429
|
+
let id = loaders.get(file);
|
|
430
|
+
if (id === undefined) {
|
|
431
|
+
id = `load${loaders.size}`;
|
|
432
|
+
loaders.set(file, id);
|
|
433
|
+
declarations.push(`const ${id} = () => import(${JSON.stringify(file)});`);
|
|
434
|
+
}
|
|
435
|
+
return id;
|
|
436
|
+
};
|
|
437
|
+
|
|
438
|
+
const entries = actions.map(
|
|
439
|
+
(action) => ` {
|
|
440
|
+
id: ${JSON.stringify(action.id)},
|
|
441
|
+
module: ${JSON.stringify(action.module)},
|
|
442
|
+
export: ${JSON.stringify(action.export)},
|
|
443
|
+
load: ${loaderId(action.file)},
|
|
444
|
+
}`,
|
|
445
|
+
);
|
|
446
|
+
|
|
447
|
+
return `${declarations.join("\n")}
|
|
448
|
+
export const actions = [
|
|
449
|
+
${entries.join(",\n")}
|
|
450
|
+
];
|
|
451
|
+
export default actions;
|
|
452
|
+
`;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* A cheap identity for the manifest file, so a reader can tell it has changed.
|
|
457
|
+
*
|
|
458
|
+
* The plugin's `load` hook runs for every module in the graph and cannot parse
|
|
459
|
+
* the manifest each time. Size and modification time together are what
|
|
460
|
+
* `.uf/cache/transform` already keys on for the binary that wrote it, and the
|
|
461
|
+
* same reasoning applies: a file that differs in neither is the file that was
|
|
462
|
+
* read. `uf dev` also clears the cache outright when its watcher sees the
|
|
463
|
+
* manifest change, so this is the build's answer rather than the only one.
|
|
464
|
+
*
|
|
465
|
+
* @param {string | undefined} file
|
|
466
|
+
*/
|
|
467
|
+
export function rscManifestKey(file) {
|
|
468
|
+
if (file == null || file === "") return "";
|
|
469
|
+
try {
|
|
470
|
+
const stats = statSync(file);
|
|
471
|
+
return `${String(stats.size)}:${String(stats.mtimeMs)}`;
|
|
472
|
+
} catch {
|
|
473
|
+
return "";
|
|
474
|
+
}
|
|
475
|
+
}
|