@uniflowed/vite 0.0.0-alpha.4 → 0.0.0-alpha.41
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 +2269 -188
- package/index.js +1180 -80
- package/internal/a11y-runtime.js +234 -0
- package/internal/a11y.js +102 -0
- package/internal/assets.js +644 -0
- package/internal/barrel-imports.js +459 -0
- package/internal/compile-assets.js +67 -0
- package/internal/config.js +39 -6
- package/internal/dev-state.js +161 -0
- package/internal/devtools.js +117 -0
- package/internal/diagnostics.js +369 -0
- package/internal/events.js +23 -0
- package/internal/flight.js +777 -0
- package/internal/flow-keywords.js +1 -1
- package/internal/frontmatter.js +33 -0
- package/internal/http.js +98 -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 +1581 -72
- package/internal/rsc.js +475 -0
- package/internal/serve.js +741 -0
- package/internal/server-components.js +109 -0
- package/internal/worker-builtins.js +170 -0
- package/package.json +27 -5
package/index.js
CHANGED
|
@@ -14,26 +14,58 @@
|
|
|
14
14
|
// development and the virtual modules that make a directory
|
|
15
15
|
// of pages an application: the route table, the client entry
|
|
16
16
|
// that hydrates it, and the server entry that renders it. In
|
|
17
|
-
// development it also
|
|
18
|
-
//
|
|
17
|
+
// development it also answers every request the way a
|
|
18
|
+
// deployment does — the guard, a server action, a route
|
|
19
|
+
// handler, then the renderer — so `uf dev` serves what
|
|
20
|
+
// `uf start` serves rather than an approximation of it, and it
|
|
21
|
+
// serves the two `/__uf/` paths a browser reports to (see
|
|
22
|
+
// `internal/diagnostics.js`).
|
|
23
|
+
// The client's copy of the route table is not the server's:
|
|
24
|
+
// `internal/rsc.js` reads the RSC analysis and leaves out the
|
|
25
|
+
// page of every route no client boundary reaches, so that
|
|
26
|
+
// route's modules never enter the browser bundle.
|
|
19
27
|
// * `uf:mdx` — `@mdx-js/rollup`, configured for React with GitHub-flavoured
|
|
20
28
|
// markdown, front matter, heading ids and build-time syntax
|
|
21
29
|
// highlighting, so `.mdx` works with
|
|
22
30
|
// no configuration.
|
|
31
|
+
// * `uf:barrel-imports` — a named import from `@uniflowed/ui` becomes an import
|
|
32
|
+
// from the file that defines the name, in every environment,
|
|
33
|
+
// so a page using one component loads that component's module
|
|
34
|
+
// rather than every module the barrel re-exports. See
|
|
35
|
+
// `internal/barrel-imports.js`.
|
|
36
|
+
// * `uf:asset` — an imported image is decoded, resized to the widths the
|
|
37
|
+
// project declares and re-encoded by `uf assets`, and an
|
|
38
|
+
// imported font is self-hosted with the `@font-face` and the
|
|
39
|
+
// metric-matched fallback that stop the swap moving the page.
|
|
40
|
+
// The import evaluates to what `Image` and `Font` need — the
|
|
41
|
+
// intrinsic size, every emitted variant, the placeholder —
|
|
42
|
+
// rather than to a URL string. See `internal/assets.js`.
|
|
23
43
|
//
|
|
24
44
|
// `uniflowed(options)` returns the array; a project that wants to add a plugin
|
|
25
45
|
// declares it in `uf.config.js` and the driver appends it after these.
|
|
26
46
|
|
|
27
|
-
import { readdirSync } from "node:fs";
|
|
28
47
|
import path from "node:path";
|
|
48
|
+
import { nativeWebPlugin } from "./internal/native-web.js";
|
|
29
49
|
|
|
30
50
|
import mdx from "@mdx-js/rollup";
|
|
31
51
|
import rehypeSlug from "rehype-slug";
|
|
52
|
+
import { relayConfig, relayDependencies, transformRelay } from "./internal/relay.js";
|
|
32
53
|
|
|
54
|
+
import {
|
|
55
|
+
AUDIT_PUBLIC_PATH,
|
|
56
|
+
AUDIT_RESOLVED_ID,
|
|
57
|
+
auditAvailable,
|
|
58
|
+
auditRuntimeSource,
|
|
59
|
+
auditTag,
|
|
60
|
+
} from "./internal/a11y.js";
|
|
61
|
+
import { assetPlugin } from "./internal/assets.js";
|
|
62
|
+
import { barrelImportsPlugin, namespaceViewOf } from "./internal/barrel-imports.js";
|
|
63
|
+
import { emit, reportRenderError, errorEvent } from "./internal/events.js";
|
|
64
|
+
import remarkFrontmatterExport from "./internal/frontmatter.js";
|
|
33
65
|
import { highlightPlugin } from "./internal/highlight.js";
|
|
66
|
+
import { moduleId } from "./internal/module-graph.js";
|
|
34
67
|
import remarkFrontmatter from "remark-frontmatter";
|
|
35
68
|
import remarkGfm from "remark-gfm";
|
|
36
|
-
import remarkMdxFrontmatter from "remark-mdx-frontmatter";
|
|
37
69
|
|
|
38
70
|
import {
|
|
39
71
|
RUNTIME_PUBLIC_PATH,
|
|
@@ -43,17 +75,85 @@ import {
|
|
|
43
75
|
refreshRuntimeSource,
|
|
44
76
|
} from "./internal/refresh.js";
|
|
45
77
|
import {
|
|
78
|
+
RSC_MANIFEST_ENV,
|
|
79
|
+
actionReferenceSource,
|
|
80
|
+
actionsModuleSource,
|
|
81
|
+
clientRouteFilter,
|
|
82
|
+
readRscManifest,
|
|
83
|
+
rscManifestKey,
|
|
84
|
+
serverActionModules,
|
|
85
|
+
serverActionTable,
|
|
86
|
+
} from "./internal/rsc.js";
|
|
87
|
+
import {
|
|
88
|
+
RESERVED,
|
|
46
89
|
VIRTUAL,
|
|
47
90
|
clientModuleSource,
|
|
91
|
+
resolveRouteTarget,
|
|
48
92
|
routesModuleSource,
|
|
93
|
+
routingRulesOf,
|
|
49
94
|
scanRoutes,
|
|
50
95
|
serverModuleSource,
|
|
51
96
|
} from "./internal/routes.js";
|
|
52
|
-
import { TransformService, isFlowModule } from "@uniflowed/host/transform";
|
|
97
|
+
import { TransformService, isFlowModule, uniflowedPackages } from "@uniflowed/host/transform";
|
|
98
|
+
import {
|
|
99
|
+
DEV_RSC_HOOK,
|
|
100
|
+
FLIGHT_BROWSER_DEPENDENCIES,
|
|
101
|
+
FLIGHT_VIRTUAL,
|
|
102
|
+
INTERCEPTED_FROM_HEADER,
|
|
103
|
+
RSC_ENVIRONMENT,
|
|
104
|
+
builtBridgeSource,
|
|
105
|
+
builtReferencesSource,
|
|
106
|
+
clientManifestSource,
|
|
107
|
+
clientModuleUrlPlugin,
|
|
108
|
+
clientReferencePlugin,
|
|
109
|
+
compilerRuntimeSource,
|
|
110
|
+
createFlightState,
|
|
111
|
+
devBridgeSource,
|
|
112
|
+
devReferencesSource,
|
|
113
|
+
devStylesheets,
|
|
114
|
+
flightClientSource,
|
|
115
|
+
flightDocumentPath,
|
|
116
|
+
flightServerSource,
|
|
117
|
+
linkStylesheets,
|
|
118
|
+
rendersFlight,
|
|
119
|
+
rscEntrySource,
|
|
120
|
+
rscEnvironment,
|
|
121
|
+
} from "./internal/flight.js";
|
|
122
|
+
import { createChannelMiddleware } from "./internal/diagnostics.js";
|
|
123
|
+
import { startDevState } from "./internal/dev-state.js";
|
|
124
|
+
import { devtoolsPreamble } from "./internal/devtools.js";
|
|
125
|
+
import { send, toAddressRequest, toRequest } from "./internal/http.js";
|
|
126
|
+
import {
|
|
127
|
+
answerRouting,
|
|
128
|
+
answersInFrontOfFiles,
|
|
129
|
+
beginRequest,
|
|
130
|
+
forViteBase,
|
|
131
|
+
rewriteRouting,
|
|
132
|
+
} from "./internal/serve.js";
|
|
133
|
+
import { serverComponentsProblem } from "./internal/server-components.js";
|
|
53
134
|
|
|
54
135
|
/** A resolved virtual id: Vite's convention is a leading NUL byte. */
|
|
55
136
|
const resolved = (id) => `\0${id}`;
|
|
56
|
-
const VIRTUAL_IDS = new Set(Object.values(VIRTUAL));
|
|
137
|
+
const VIRTUAL_IDS = new Set([...Object.values(VIRTUAL), ...Object.values(FLIGHT_VIRTUAL)]);
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* CommonJS packages the browser can reach only through an installed
|
|
141
|
+
* `@uniflowed/*` package.
|
|
142
|
+
*
|
|
143
|
+
* uf excludes its own packages from Vite's dependency optimizer because they
|
|
144
|
+
* ship Flow, so Vite will not discover a CommonJS peer through them once they
|
|
145
|
+
* live under a project's `node_modules`. React Testing's `happy-dom` edge is
|
|
146
|
+
* ESM and browser-mapped to a local stub, and the optional `axe-core` edge is
|
|
147
|
+
* named separately below only when the project actually installed it.
|
|
148
|
+
*/
|
|
149
|
+
const CLIENT_COMMONJS_DEPENDENCIES = Object.freeze([
|
|
150
|
+
"react",
|
|
151
|
+
"react/jsx-runtime",
|
|
152
|
+
"react/jsx-dev-runtime",
|
|
153
|
+
"react/compiler-runtime",
|
|
154
|
+
"react-dom",
|
|
155
|
+
"react-dom/client",
|
|
156
|
+
]);
|
|
57
157
|
|
|
58
158
|
/**
|
|
59
159
|
* Prefix of the virtual module that carries one source module's StyleX rules.
|
|
@@ -76,6 +176,7 @@ export function devUrlFor(id) {
|
|
|
76
176
|
* @typedef {object} UniflowedOptions
|
|
77
177
|
* @property {string} [root] absolute project root; Vite's root by default
|
|
78
178
|
* @property {object} [config] the loaded `uf.config.js` object
|
|
179
|
+
* @property {"web" | "native" | "ios" | "android"} [target] app target
|
|
79
180
|
* @property {string} [command] the `uf` binary to transform through
|
|
80
181
|
*/
|
|
81
182
|
|
|
@@ -87,16 +188,112 @@ export function devUrlFor(id) {
|
|
|
87
188
|
export default function uniflowed(options = {}) {
|
|
88
189
|
const ufConfig = options.config ?? {};
|
|
89
190
|
const app = ufConfig.app ?? {};
|
|
191
|
+
const routeTarget = resolveRouteTarget(ufConfig, options.target);
|
|
90
192
|
const routerRoot = app.router?.root ?? "app";
|
|
91
193
|
const appEntry = app.router?.entry ?? ufConfig.build?.entries?.[0] ?? "app.js";
|
|
92
194
|
const markdown = app.builtins?.markdown ?? {};
|
|
195
|
+
const builtins = app.builtins ?? {};
|
|
196
|
+
// On unless the project says otherwise, and read as `!== false` rather than
|
|
197
|
+
// `=== true` because that is what "on by default" means for a field almost
|
|
198
|
+
// no `uf.config.js` will mention. It only ever reaches the *development*
|
|
199
|
+
// client entry; see `flowPlugin`'s `load`. ubugeeei-prod/uf#516.
|
|
200
|
+
const strictMode = app.react?.strictMode !== false;
|
|
201
|
+
// What the browser does with a link, read here for the reason Strict Mode is
|
|
202
|
+
// and honoured in every command rather than in the build alone: it is the
|
|
203
|
+
// one setting whose whole effect is what happens on a click, so a dev server
|
|
204
|
+
// that disagreed with the deployment would be the wrong application to look
|
|
205
|
+
// at. Anything but `"document"` is the client router, which is what every
|
|
206
|
+
// project that has not heard of the key has.
|
|
207
|
+
const navigation = app.rendering?.navigation === "document" ? "document" : "client";
|
|
208
|
+
// How long, in seconds, the client router shows a route it already fetched
|
|
209
|
+
// without asking the server again, written into the client entry beside
|
|
210
|
+
// `navigation` and honoured by every command for the same reason. Anything
|
|
211
|
+
// but a positive number is `0`, which keeps nothing.
|
|
212
|
+
const staleTime =
|
|
213
|
+
typeof app.rendering?.staleTime === "number" &&
|
|
214
|
+
Number.isFinite(app.rendering.staleTime) &&
|
|
215
|
+
app.rendering.staleTime > 0
|
|
216
|
+
? app.rendering.staleTime
|
|
217
|
+
: 0;
|
|
218
|
+
// Whether this application starts by attaching to markup or by rendering
|
|
219
|
+
// into an empty root. `["csr"]` is the only list that means the second, and
|
|
220
|
+
// `uf` refuses that value beside any other while the config is read — so the
|
|
221
|
+
// question here is "is it in the list", not "is it the only thing in it",
|
|
222
|
+
// and a driver started by hand on a config `uf` never validated gets the same
|
|
223
|
+
// answer for the same reason a project would want.
|
|
224
|
+
const mount = (app.rendering?.modes ?? []).includes("csr") ? "render" : "hydrate";
|
|
225
|
+
// Whether routes render as React Server Components, which is the default: a
|
|
226
|
+
// second module graph resolved under `react-server`, a document carrying the
|
|
227
|
+
// Flight payload it was rendered from, and a browser that hydrates that
|
|
228
|
+
// payload rather than importing routes. `app.rsc: false` is the application
|
|
229
|
+
// rendered from its modules, as every uf application was before
|
|
230
|
+
// ubugeeei-prod/uf#519. See `./internal/flight.js`.
|
|
231
|
+
const flightState = rendersFlight(app, { mount, routeTarget })
|
|
232
|
+
? createFlightState({ root: options.root ?? process.cwd() })
|
|
233
|
+
: null;
|
|
234
|
+
|
|
235
|
+
const accessibility = ufConfig.accessibility ?? {};
|
|
236
|
+
// `app.router.redirects`, `rewrites` and `headers`: written into the server
|
|
237
|
+
// bundle for every host that serves a build, and asked by `uf dev` itself
|
|
238
|
+
// from the same object. See `@uniflowed/server`'s `internal/routing.js`.
|
|
239
|
+
const routing = routingRulesOf(app.router);
|
|
93
240
|
|
|
94
|
-
return [
|
|
241
|
+
return [
|
|
242
|
+
flowPlugin({
|
|
243
|
+
routerRoot,
|
|
244
|
+
appEntry,
|
|
245
|
+
routeTarget,
|
|
246
|
+
strictMode,
|
|
247
|
+
navigation,
|
|
248
|
+
staleTime,
|
|
249
|
+
mount,
|
|
250
|
+
flightState,
|
|
251
|
+
routing,
|
|
252
|
+
command: options.command,
|
|
253
|
+
accessibility,
|
|
254
|
+
relayEnabled: builtins.relay !== false,
|
|
255
|
+
}),
|
|
256
|
+
...(flightState == null ? [] : [clientReferencePlugin(flightState), clientModuleUrlPlugin()]),
|
|
257
|
+
// After the references, so a client module the rsc graph has already
|
|
258
|
+
// replaced is not read for imports it no longer has; see the file.
|
|
259
|
+
barrelImportsPlugin(),
|
|
260
|
+
mdxPlugin(markdown),
|
|
261
|
+
assetPlugin({
|
|
262
|
+
images: builtins.images ?? {},
|
|
263
|
+
fonts: builtins.fonts ?? {},
|
|
264
|
+
icons: builtins.icons ?? {},
|
|
265
|
+
og: builtins.og ?? {},
|
|
266
|
+
command: options.command,
|
|
267
|
+
}),
|
|
268
|
+
nativeWebPlugin(routeTarget),
|
|
269
|
+
];
|
|
95
270
|
}
|
|
96
271
|
|
|
97
|
-
function flowPlugin({
|
|
272
|
+
function flowPlugin({
|
|
273
|
+
routerRoot,
|
|
274
|
+
appEntry,
|
|
275
|
+
routeTarget,
|
|
276
|
+
strictMode,
|
|
277
|
+
navigation,
|
|
278
|
+
staleTime,
|
|
279
|
+
mount,
|
|
280
|
+
flightState,
|
|
281
|
+
routing,
|
|
282
|
+
command,
|
|
283
|
+
accessibility,
|
|
284
|
+
relayEnabled,
|
|
285
|
+
}) {
|
|
98
286
|
let root = process.cwd();
|
|
99
287
|
let isProduction = false;
|
|
288
|
+
/**
|
|
289
|
+
* Whether this project has axe-core, so the audit has an engine.
|
|
290
|
+
*
|
|
291
|
+
* Answered once in `configResolved` rather than per document: it is a
|
|
292
|
+
* question about `node_modules`, the answer cannot change while the server
|
|
293
|
+
* runs without a restart anyway, and asking it per render would put a module
|
|
294
|
+
* resolution on the path of every page.
|
|
295
|
+
*/
|
|
296
|
+
let auditsPage = false;
|
|
100
297
|
let base = "/";
|
|
101
298
|
let appRoot = "";
|
|
102
299
|
let entryPath = "";
|
|
@@ -104,6 +301,7 @@ function flowPlugin({ routerRoot, appEntry, command }) {
|
|
|
104
301
|
let server = null;
|
|
105
302
|
/** @type {TransformService | null} */
|
|
106
303
|
let service = null;
|
|
304
|
+
let relayOptions = null;
|
|
107
305
|
/**
|
|
108
306
|
* Each module's compiled stylesheet, keyed by the virtual id serving it.
|
|
109
307
|
*
|
|
@@ -113,33 +311,153 @@ function flowPlugin({ routerRoot, appEntry, command }) {
|
|
|
113
311
|
* hand, which is the part that goes wrong.
|
|
114
312
|
*/
|
|
115
313
|
const styles = new Map();
|
|
314
|
+
/**
|
|
315
|
+
* The React Compiler findings already reported, so each is said once.
|
|
316
|
+
*
|
|
317
|
+
* `uf build` runs Vite twice — once for the browser bundle and once for the
|
|
318
|
+
* server one — over the same modules, so every finding was made twice and
|
|
319
|
+
* printed twice. An entry records which environment reported a module's
|
|
320
|
+
* findings first: a re-transform in *that* environment (a dev server, after
|
|
321
|
+
* an edit) clears it and reports again, and the other environment's pass over
|
|
322
|
+
* the same module stays quiet. Keying on the environment rather than on a
|
|
323
|
+
* flag is what keeps the second half true without making the first half
|
|
324
|
+
* false.
|
|
325
|
+
*
|
|
326
|
+
* @type {Map<string, { environment: string, signatures: Set<string> }>}
|
|
327
|
+
*/
|
|
328
|
+
const reported = new Map();
|
|
329
|
+
/** Findings held back as a dependency's, waiting to be counted out loud. */
|
|
330
|
+
let suppressed = [];
|
|
116
331
|
|
|
117
332
|
const ensureService = () => {
|
|
118
333
|
service ??= new TransformService({ command, root });
|
|
119
334
|
return service;
|
|
120
335
|
};
|
|
121
336
|
|
|
337
|
+
/**
|
|
338
|
+
* The browser's copy of the route table.
|
|
339
|
+
*
|
|
340
|
+
* The manifest is read here rather than once at start-up because `uf dev`
|
|
341
|
+
* rewrites it whenever the graph moves, and this hook runs again when it
|
|
342
|
+
* does — a table built from a manifest read at start-up would be the answer
|
|
343
|
+
* for the project as it was when the server started.
|
|
344
|
+
*
|
|
345
|
+
* The count is emitted rather than computed on the Rust side, and that is
|
|
346
|
+
* the point of it: `uf build` prints what the table it just generated
|
|
347
|
+
* contains, not what a second implementation of this decision predicted it
|
|
348
|
+
* would. Only for a build — a dev server has no summary to be true in.
|
|
349
|
+
*/
|
|
350
|
+
const clientRoutesModule = (table) => {
|
|
351
|
+
const shipsPage = clientRouteFilter(
|
|
352
|
+
readRscManifest(process.env[RSC_MANIFEST_ENV]),
|
|
353
|
+
root,
|
|
354
|
+
table,
|
|
355
|
+
);
|
|
356
|
+
const kept = new Set(table.routes.filter(shipsPage));
|
|
357
|
+
if (server == null) {
|
|
358
|
+
emit("rsc-split", { pages: kept.size, routes: table.routes.length });
|
|
359
|
+
}
|
|
360
|
+
// `relativeTo` only here, and never for the server's copy below. Every
|
|
361
|
+
// `import()` in this table becomes a chunk URL, but `file` is a string and
|
|
362
|
+
// survives the build — so the browser's table was shipping the absolute
|
|
363
|
+
// path of every page on the machine that built the site, to every visitor.
|
|
364
|
+
// The server's table is read where those files are and keeps them.
|
|
365
|
+
// HTTP handlers are exclusively server capabilities. Even an unused
|
|
366
|
+
// dynamic import makes Vite traverse the handler's database/auth imports.
|
|
367
|
+
return routesModuleSource(
|
|
368
|
+
{ ...table, handlers: [] },
|
|
369
|
+
{
|
|
370
|
+
shipsPage: (route) => kept.has(route),
|
|
371
|
+
relativeTo: root,
|
|
372
|
+
},
|
|
373
|
+
);
|
|
374
|
+
};
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* The manifest's two action tables, re-read only when the file changes.
|
|
378
|
+
*
|
|
379
|
+
* `load` runs for every module in the graph and has to ask "is this a
|
|
380
|
+
* `"use server"` module?" about each one, so parsing the manifest per call
|
|
381
|
+
* would put a JSON parse of the whole graph between Vite and every file it
|
|
382
|
+
* opens. Size and modification time are the identity — the same pair
|
|
383
|
+
* `.uf/cache/transform` keys on — and `uf dev` drops the memo outright when
|
|
384
|
+
* its watcher sees the file change, so a rewrite inside one millisecond is
|
|
385
|
+
* still seen.
|
|
386
|
+
*/
|
|
387
|
+
let actionMemo = null;
|
|
388
|
+
const forgetActions = () => {
|
|
389
|
+
actionMemo = null;
|
|
390
|
+
};
|
|
391
|
+
const invalidateActionModules = (moduleGraph, modules) => {
|
|
392
|
+
for (const id of modules.keys()) {
|
|
393
|
+
const module = moduleGraph.getModuleById(id);
|
|
394
|
+
if (module) moduleGraph.invalidateModule(module);
|
|
395
|
+
}
|
|
396
|
+
};
|
|
397
|
+
const actionTables = () => {
|
|
398
|
+
const file = process.env[RSC_MANIFEST_ENV];
|
|
399
|
+
const key = rscManifestKey(file);
|
|
400
|
+
if (actionMemo == null || actionMemo.key !== key) {
|
|
401
|
+
const manifest = readRscManifest(file);
|
|
402
|
+
actionMemo = {
|
|
403
|
+
key,
|
|
404
|
+
modules: serverActionModules(manifest, root),
|
|
405
|
+
table: serverActionTable(manifest, root),
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
return actionMemo;
|
|
409
|
+
};
|
|
410
|
+
|
|
122
411
|
return {
|
|
123
412
|
name: "uf:flow",
|
|
124
413
|
enforce: "pre",
|
|
414
|
+
// What `driver.js` runs the three builds with; `null` for an application
|
|
415
|
+
// rendered from its modules. See `./internal/flight.js`.
|
|
416
|
+
api: { flight: flightState },
|
|
125
417
|
|
|
126
418
|
config(userConfig, env) {
|
|
127
419
|
const projectRoot = path.resolve(userConfig.root ?? process.cwd());
|
|
420
|
+
// Before anything is resolved. Routes that render as React Server
|
|
421
|
+
// Components need `react-server-dom-parcel` and React 19.3, and the router
|
|
422
|
+
// installs without either (ubugeeei-prod/uf#992). Said here, once, rather
|
|
423
|
+
// than as an unresolved import deep in a build or a failure inside a render.
|
|
424
|
+
if (flightState != null) {
|
|
425
|
+
const problem = serverComponentsProblem(projectRoot);
|
|
426
|
+
if (problem != null) {
|
|
427
|
+
throw new Error(problem);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
128
430
|
isProduction = env.mode === "production" || env.command === "build";
|
|
431
|
+
// A reference names the client manifest only in a build, where a client
|
|
432
|
+
// build writes the chunks it names; a dev server names the URL it serves.
|
|
433
|
+
if (flightState != null) flightState.production = env.command === "build";
|
|
434
|
+
// Decided here rather than in `configResolved`, because the answer has to
|
|
435
|
+
// reach `optimizeDeps.include` below and that is written in this hook.
|
|
436
|
+
auditsPage =
|
|
437
|
+
!isProduction && (accessibility?.devAudit ?? true) && auditAvailable(projectRoot);
|
|
129
438
|
return {
|
|
130
439
|
// uf serves HTML itself; there is no index.html to fall back to.
|
|
131
440
|
appType: "custom",
|
|
132
441
|
resolve: {
|
|
133
|
-
dedupe: ["react", "react-dom"],
|
|
442
|
+
dedupe: ["react", "react-dom", "react-relay", "relay-runtime"],
|
|
134
443
|
},
|
|
135
444
|
optimizeDeps: {
|
|
136
445
|
include: [
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
446
|
+
...CLIENT_COMMONJS_DEPENDENCIES,
|
|
447
|
+
...relayDependencies(projectRoot),
|
|
448
|
+
// React's Flight client, for an application whose routes render as
|
|
449
|
+
// Server Components; `FLIGHT_BROWSER_DEPENDENCIES` says why.
|
|
450
|
+
...(flightState == null ? [] : FLIGHT_BROWSER_DEPENDENCIES),
|
|
451
|
+
// The accessibility audit's engine, when this project has one.
|
|
452
|
+
//
|
|
453
|
+
// Named up front rather than left to be discovered. axe-core is
|
|
454
|
+
// CommonJS, and the only thing that imports it is a *virtual*
|
|
455
|
+
// module reached from the document — so Vite would meet it for the
|
|
456
|
+
// first time after a page had already loaded, optimise it then, and
|
|
457
|
+
// reload the page it had just served. A full reload in the middle
|
|
458
|
+
// of the first render of every `uf dev` session is a high price for
|
|
459
|
+
// a feature whose whole manner is to be quiet.
|
|
460
|
+
...(auditsPage ? ["axe-core"] : []),
|
|
143
461
|
],
|
|
144
462
|
// uf's packages ship Flow. The dependency optimiser pre-bundles
|
|
145
463
|
// with a JavaScript parser and would reject every one of them.
|
|
@@ -151,12 +469,30 @@ function flowPlugin({ routerRoot, appEntry, command }) {
|
|
|
151
469
|
// externalised.
|
|
152
470
|
noExternal: [/^@uniflowed\//],
|
|
153
471
|
},
|
|
472
|
+
// The graph React Server Components render in, beside the two Vite
|
|
473
|
+
// always has — declared only for an application that renders them, so
|
|
474
|
+
// `app.rsc: false` is a Vite configuration with nothing added.
|
|
475
|
+
...(flightState == null
|
|
476
|
+
? {}
|
|
477
|
+
: {
|
|
478
|
+
environments: {
|
|
479
|
+
[RSC_ENVIRONMENT]: rscEnvironment({
|
|
480
|
+
root: projectRoot,
|
|
481
|
+
production: env.command === "build",
|
|
482
|
+
exclude: uniflowedPackages(projectRoot),
|
|
483
|
+
}),
|
|
484
|
+
},
|
|
485
|
+
}),
|
|
154
486
|
};
|
|
155
487
|
},
|
|
156
488
|
|
|
157
489
|
configResolved(config) {
|
|
158
490
|
root = config.root;
|
|
159
491
|
base = config.base;
|
|
492
|
+
if (flightState != null) {
|
|
493
|
+
flightState.root = config.root;
|
|
494
|
+
flightState.base = config.base;
|
|
495
|
+
}
|
|
160
496
|
appRoot = path.resolve(root, routerRoot);
|
|
161
497
|
entryPath = path.resolve(root, appEntry);
|
|
162
498
|
},
|
|
@@ -165,8 +501,43 @@ function flowPlugin({ routerRoot, appEntry, command }) {
|
|
|
165
501
|
ensureService();
|
|
166
502
|
},
|
|
167
503
|
|
|
168
|
-
resolveId(id) {
|
|
504
|
+
resolveId(id, importer, resolveOptions) {
|
|
505
|
+
// The client's graph, and the rsc graph too. `@uniflowed/react` is
|
|
506
|
+
// `export * from "react"`, and the rsc graph pre-bundles React's CommonJS
|
|
507
|
+
// under `react-server`: a star re-export of that namespace names nothing
|
|
508
|
+
// Vite's module runner can forward, so under `uf dev` every hook a server
|
|
509
|
+
// component imported from `@uniflowed/react` was `undefined` — `use` first.
|
|
510
|
+
// Importing `react` itself is the module those names are on. The ssr graph
|
|
511
|
+
// keeps its own resolution, because React is external there and Node's
|
|
512
|
+
// interop forwards the names.
|
|
513
|
+
if (
|
|
514
|
+
id === "@uniflowed/react" &&
|
|
515
|
+
(this.environment?.name === RSC_ENVIRONMENT || !isSsr(this, resolveOptions))
|
|
516
|
+
) {
|
|
517
|
+
return this.resolve("react", importer, { ...resolveOptions, skipSelf: true });
|
|
518
|
+
}
|
|
169
519
|
if (id === RUNTIME_PUBLIC_PATH) return RUNTIME_RESOLVED_ID;
|
|
520
|
+
if (id === AUDIT_PUBLIC_PATH) return AUDIT_RESOLVED_ID;
|
|
521
|
+
// The rsc build cannot know a client chunk's URL, because the client
|
|
522
|
+
// build that writes the chunks runs after it. It leaves the manifest as an
|
|
523
|
+
// import, and the ssr build — which bundles the rsc output in — resolves it.
|
|
524
|
+
if (
|
|
525
|
+
id === FLIGHT_VIRTUAL.manifest &&
|
|
526
|
+
flightState?.production === true &&
|
|
527
|
+
this.environment?.name === RSC_ENVIRONMENT
|
|
528
|
+
) {
|
|
529
|
+
return { id, external: true };
|
|
530
|
+
}
|
|
531
|
+
// The React Compiler's runtime, which reads the client's internals and so
|
|
532
|
+
// cannot run where `react` resolved under `react-server`; see
|
|
533
|
+
// `compilerRuntimeSource`.
|
|
534
|
+
if (
|
|
535
|
+
id === "react/compiler-runtime" &&
|
|
536
|
+
flightState != null &&
|
|
537
|
+
this.environment?.name === RSC_ENVIRONMENT
|
|
538
|
+
) {
|
|
539
|
+
return resolved(FLIGHT_VIRTUAL.compilerRuntime);
|
|
540
|
+
}
|
|
170
541
|
if (VIRTUAL_IDS.has(id)) return resolved(id);
|
|
171
542
|
// A module's own stylesheet, which `transform` below asked for by
|
|
172
543
|
// importing this id. Returning it unchanged marks it resolved without
|
|
@@ -175,18 +546,125 @@ function flowPlugin({ routerRoot, appEntry, command }) {
|
|
|
175
546
|
return null;
|
|
176
547
|
},
|
|
177
548
|
|
|
178
|
-
load(id) {
|
|
549
|
+
load(id, loadOptions) {
|
|
179
550
|
if (id === RUNTIME_RESOLVED_ID) return refreshRuntimeSource();
|
|
180
|
-
if (id ===
|
|
181
|
-
if (id === resolved(VIRTUAL.
|
|
182
|
-
|
|
551
|
+
if (id === AUDIT_RESOLVED_ID) return auditRuntimeSource(accessibility?.axe);
|
|
552
|
+
if (id === resolved(VIRTUAL.routes)) {
|
|
553
|
+
const table = scanRoutes(appRoot, { target: routeTarget });
|
|
554
|
+
// Under React Server Components the table is split by graph rather than
|
|
555
|
+
// filtered. The rsc graph renders routes, so it gets every route and
|
|
556
|
+
// boundary and no handler or middleware: those answer a request, and
|
|
557
|
+
// importing one here would resolve its dependencies under
|
|
558
|
+
// `react-server` for nothing. The ssr graph gets exactly those two,
|
|
559
|
+
// because every route it renders reaches it as a payload.
|
|
560
|
+
if (flightState != null && this.environment?.name === RSC_ENVIRONMENT) {
|
|
561
|
+
return routesModuleSource({ ...table, handlers: [], middleware: [] });
|
|
562
|
+
}
|
|
563
|
+
if (flightState != null && isSsr(this, loadOptions)) {
|
|
564
|
+
return routesModuleSource({ ...table, routes: [], notFound: [], errors: [] });
|
|
565
|
+
}
|
|
566
|
+
// The server renders every route, so the server's table is the whole
|
|
567
|
+
// one and is generated with no filter at all. Only the browser's copy
|
|
568
|
+
// is split.
|
|
569
|
+
if (isSsr(this, loadOptions)) return routesModuleSource(table);
|
|
570
|
+
return clientRoutesModule(table);
|
|
571
|
+
}
|
|
572
|
+
// Strict Mode belongs to the client entry and to development only: a
|
|
573
|
+
// build passes `false`, so the generated module is the one that existed
|
|
574
|
+
// before #516 and a visitor's browser renders once.
|
|
575
|
+
//
|
|
576
|
+
// `navigation` is in the same entry and has no `isProduction` beside it,
|
|
577
|
+
// deliberately: it is what a link does, and a dev server whose links
|
|
578
|
+
// behave differently from the deployment is the wrong thing to be
|
|
579
|
+
// looking at. See `clientModuleSource`.
|
|
580
|
+
if (id === resolved(VIRTUAL.client) && flightState != null) {
|
|
581
|
+
return flightClientSource(entryPath, {
|
|
582
|
+
strictMode: strictMode && !isProduction,
|
|
583
|
+
navigation,
|
|
584
|
+
staleTime,
|
|
585
|
+
routing,
|
|
586
|
+
});
|
|
587
|
+
}
|
|
588
|
+
if (id === resolved(VIRTUAL.client)) {
|
|
589
|
+
return clientModuleSource(entryPath, {
|
|
590
|
+
strictMode: strictMode && !isProduction,
|
|
591
|
+
navigation,
|
|
592
|
+
staleTime,
|
|
593
|
+
mount,
|
|
594
|
+
routing,
|
|
595
|
+
});
|
|
596
|
+
}
|
|
597
|
+
if (id === resolved(VIRTUAL.server)) {
|
|
598
|
+
return flightState == null
|
|
599
|
+
? serverModuleSource(entryPath, routing)
|
|
600
|
+
: flightServerSource(entryPath, VIRTUAL.routes, VIRTUAL.actions, routing);
|
|
601
|
+
}
|
|
602
|
+
if (flightState != null) {
|
|
603
|
+
if (id === resolved(FLIGHT_VIRTUAL.entry)) return rscEntrySource(VIRTUAL.routes, routing);
|
|
604
|
+
if (id === resolved(FLIGHT_VIRTUAL.compilerRuntime)) return compilerRuntimeSource();
|
|
605
|
+
if (id === resolved(FLIGHT_VIRTUAL.bridge)) {
|
|
606
|
+
if (server != null) return devBridgeSource();
|
|
607
|
+
if (flightState.rscOutput == null) {
|
|
608
|
+
throw new Error(
|
|
609
|
+
"uf: the server bundle was asked to bundle the rsc graph before it was built. " +
|
|
610
|
+
"`uf build` builds `virtual:uf/rsc` first; a build started some other way has to as well.",
|
|
611
|
+
);
|
|
612
|
+
}
|
|
613
|
+
return builtBridgeSource(flightState.rscOutput);
|
|
614
|
+
}
|
|
615
|
+
if (id === resolved(FLIGHT_VIRTUAL.manifest)) {
|
|
616
|
+
return clientManifestSource(flightState.chunkUrls);
|
|
617
|
+
}
|
|
618
|
+
if (id === resolved(FLIGHT_VIRTUAL.references)) {
|
|
619
|
+
return server != null
|
|
620
|
+
? devReferencesSource(root, base)
|
|
621
|
+
: builtReferencesSource(flightState.chunkUrls);
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
// Only `virtual:uf/server` imports this, so it is only ever asked for in
|
|
625
|
+
// the server environment — but the table it carries is every callable
|
|
626
|
+
// endpoint of the build, so it is worth saying that a browser asking for
|
|
627
|
+
// it gets nothing rather than getting the list.
|
|
628
|
+
if (id === resolved(VIRTUAL.actions)) {
|
|
629
|
+
if (!isSsr(this, loadOptions))
|
|
630
|
+
return "export const actions = [];\nexport default actions;\n";
|
|
631
|
+
return actionsModuleSource(actionTables().table);
|
|
632
|
+
}
|
|
183
633
|
if (id.startsWith(STYLE_PREFIX)) return styles.get(id) ?? "";
|
|
634
|
+
|
|
635
|
+
// A `"use server"` module, in the browser's graph only: what the client
|
|
636
|
+
// gets is one `createServerReference` per callable export, and never the
|
|
637
|
+
// file. This is where the second half of the RSC split actually happens
|
|
638
|
+
// — the route filter above decides which *pages* the browser is given,
|
|
639
|
+
// and this decides that an action module's body, its imports and
|
|
640
|
+
// everything only they reached are not the browser's business at all.
|
|
641
|
+
//
|
|
642
|
+
// Substituting the source rather than rewriting it: a transform that
|
|
643
|
+
// stripped the body would have to be right about every way a module can
|
|
644
|
+
// name something, and being wrong once means shipping a database handle.
|
|
645
|
+
// The exports the reference module declares come from the manifest, so
|
|
646
|
+
// they are exactly the exports `uf_rsc` decided are callable endpoints
|
|
647
|
+
// and an import of anything else is a build error rather than a silent
|
|
648
|
+
// `undefined`. `crates/uf_rsc/src/graph/build.rs` colours these modules
|
|
649
|
+
// server for the same reason, so the analysis and the bundle agree.
|
|
650
|
+
//
|
|
651
|
+
// Nor in the rsc graph: a server component that calls an action is calling
|
|
652
|
+
// a function on the server, and it gets the function.
|
|
653
|
+
if (!isSsr(this, loadOptions) && this.environment?.name !== RSC_ENVIRONMENT) {
|
|
654
|
+
const references = actionTables().modules.get(cleanId(id));
|
|
655
|
+
if (references != null) return actionReferenceSource(references);
|
|
656
|
+
}
|
|
184
657
|
return null;
|
|
185
658
|
},
|
|
186
659
|
|
|
187
660
|
async transform(code, id, transformOptions) {
|
|
188
|
-
|
|
189
|
-
|
|
661
|
+
// A view of a barrel's namespace has the barrel's path and none of its
|
|
662
|
+
// source: `uf:barrel-imports` generates it as JavaScript.
|
|
663
|
+
if (!isFlowModule(id) || namespaceViewOf(id) != null) return null;
|
|
664
|
+
// Both server graphs: neither gets a refresh wrapper, and the rsc graph's
|
|
665
|
+
// findings are reported as that graph's.
|
|
666
|
+
const rsc = this.environment?.name === RSC_ENVIRONMENT;
|
|
667
|
+
const ssr = transformOptions?.ssr === true || this.environment?.name === "ssr" || rsc;
|
|
190
668
|
const refresh = !isProduction && !ssr && server != null;
|
|
191
669
|
const out = await ensureService().transform(cleanId(id), code, {
|
|
192
670
|
development: !isProduction,
|
|
@@ -194,10 +672,24 @@ function flowPlugin({ routerRoot, appEntry, command }) {
|
|
|
194
672
|
sourceMap: true,
|
|
195
673
|
});
|
|
196
674
|
if (out == null) return null;
|
|
197
|
-
|
|
198
|
-
|
|
675
|
+
reportDiagnostics(this, {
|
|
676
|
+
id: cleanId(id),
|
|
677
|
+
root,
|
|
678
|
+
diagnostics: out.diagnostics,
|
|
679
|
+
environment: rsc ? RSC_ENVIRONMENT : ssr ? "ssr" : "client",
|
|
680
|
+
reported,
|
|
681
|
+
suppressed,
|
|
682
|
+
});
|
|
683
|
+
let map = out.map == null ? null : JSON.parse(out.map);
|
|
684
|
+
let output = out.code;
|
|
685
|
+
if (relayEnabled && output.includes("graphql")) {
|
|
686
|
+
relayOptions ??= relayConfig(root);
|
|
687
|
+
const transformed = await transformRelay(output, cleanId(id), map, await relayOptions);
|
|
688
|
+
if (transformed != null) {
|
|
689
|
+
output = transformed.code;
|
|
690
|
+
map = transformed.map;
|
|
691
|
+
}
|
|
199
692
|
}
|
|
200
|
-
const map = out.map == null ? null : JSON.parse(out.map);
|
|
201
693
|
// StyleX. `uf transform` compiled the module's `stylex.create` calls into
|
|
202
694
|
// class names and handed back the rules they declared; the rules become a
|
|
203
695
|
// module of their own that this one imports.
|
|
@@ -207,18 +699,40 @@ function flowPlugin({ routerRoot, appEntry, command }) {
|
|
|
207
699
|
// business: Vite already injects a stylesheet in dev, extracts it in a
|
|
208
700
|
// build, code-splits it per chunk, and replaces it over HMR. A module
|
|
209
701
|
// whose styles are gone stops importing it, and Vite notices.
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
702
|
+
//
|
|
703
|
+
// The stylesheet is named by the module's path from the project root, the
|
|
704
|
+
// spelling `moduleId` gives the module graph report, and not by its
|
|
705
|
+
// absolute path. In a client chunk the stylesheet is one of the chunk's
|
|
706
|
+
// sources, so its name is written into the source map a site publishes,
|
|
707
|
+
// and an absolute name would publish where the machine that built it
|
|
708
|
+
// keeps its files. A module outside the root climbs out with `../`.
|
|
709
|
+
const styled = out.css != null && out.css !== "";
|
|
710
|
+
if (styled) {
|
|
711
|
+
const styleId = `${STYLE_PREFIX}${moduleId(root, cleanId(id))}.css`;
|
|
213
712
|
styles.set(styleId, out.css);
|
|
214
713
|
output = `import ${JSON.stringify(styleId)};\n${output}`;
|
|
215
714
|
}
|
|
216
|
-
|
|
715
|
+
// A module that compiled a stylesheet has a side effect, whatever its
|
|
716
|
+
// package says. `@uniflowed/stylex` declares `sideEffects: false` and is
|
|
717
|
+
// right about its source: `tokens.stylex.js` only exports a token set.
|
|
718
|
+
// What it exports after this transform is a token set *and* a `:root`
|
|
719
|
+
// block, and the page that imports `ufTokens` no longer names it at
|
|
720
|
+
// runtime — the compiler turned every read into the `var(--…)` it minted.
|
|
721
|
+
// So the import was unused, a side-effect-free module with no used
|
|
722
|
+
// exports was dropped, and the custom properties every one of those
|
|
723
|
+
// `var()`s resolves against went with it: rules that referred to nothing.
|
|
724
|
+
// Declaring the side effect here rather than editing the package is
|
|
725
|
+
// deliberate — the side effect is one this plugin added, so it is this
|
|
726
|
+
// plugin's to admit to. See ubugeeei-prod/uf#306.
|
|
727
|
+
const moduleSideEffects = styled ? true : undefined;
|
|
728
|
+
if (!refresh) return { code: output, map, moduleSideEffects };
|
|
217
729
|
const relative = path.relative(root, cleanId(id)).split(path.sep).join("/");
|
|
218
|
-
return addRefreshWrapper(output, map, relative);
|
|
730
|
+
return { ...addRefreshWrapper(output, map, relative), moduleSideEffects };
|
|
219
731
|
},
|
|
220
732
|
|
|
221
733
|
buildEnd() {
|
|
734
|
+
summariseSuppressed(this, suppressed);
|
|
735
|
+
suppressed = [];
|
|
222
736
|
// A dev server keeps its service for the whole session; a build is
|
|
223
737
|
// done with it here.
|
|
224
738
|
if (server == null) {
|
|
@@ -227,28 +741,127 @@ function flowPlugin({ routerRoot, appEntry, command }) {
|
|
|
227
741
|
}
|
|
228
742
|
},
|
|
229
743
|
|
|
744
|
+
// The two scripts a development document loads before its own, and
|
|
745
|
+
// nothing at all in a build — which is the whole of "the hook is out of a
|
|
746
|
+
// production build" (ubugeeei-prod/uf#503) and of "production does not run
|
|
747
|
+
// under Strict Mode" (#516, whose flag is generated into
|
|
748
|
+
// `virtual:uf/client` rather than injected here).
|
|
749
|
+
//
|
|
750
|
+
// DevTools first, and as a *classic* script rather than a module: React
|
|
751
|
+
// registers itself with `__REACT_DEVTOOLS_GLOBAL_HOOK__` while `react-dom`
|
|
752
|
+
// is evaluated and never again, so the hook has to exist before any module
|
|
753
|
+
// runs. A classic inline script runs while the parser is on it; a module
|
|
754
|
+
// waits for the document. `internal/devtools.js` has the rest of the
|
|
755
|
+
// argument, and the three conditions DevTools needs.
|
|
230
756
|
transformIndexHtml() {
|
|
231
757
|
if (isProduction) return [];
|
|
232
|
-
|
|
758
|
+
const tags = [
|
|
759
|
+
{
|
|
760
|
+
tag: "script",
|
|
761
|
+
attrs: { "data-uf-dev-head-preamble": "react-devtools" },
|
|
762
|
+
children: devtoolsPreamble(),
|
|
763
|
+
injectTo: "head-prepend",
|
|
764
|
+
},
|
|
233
765
|
{
|
|
234
766
|
tag: "script",
|
|
235
|
-
attrs: { type: "module" },
|
|
767
|
+
attrs: { type: "module", "data-uf-dev-head-preamble": "react-refresh" },
|
|
236
768
|
children: preambleCode(base),
|
|
237
769
|
injectTo: "head-prepend",
|
|
238
770
|
},
|
|
239
771
|
];
|
|
772
|
+
// Only when the project has the engine. A tag pointing at a module that
|
|
773
|
+
// cannot resolve `axe-core` would be a red console on every page of a
|
|
774
|
+
// project that never asked for an audit, which is a worse default than
|
|
775
|
+
// no audit.
|
|
776
|
+
const audit = auditTag(base, auditsPage);
|
|
777
|
+
if (audit != null) tags.push(audit);
|
|
778
|
+
return tags;
|
|
779
|
+
},
|
|
780
|
+
|
|
781
|
+
// An edit to a server component changes what the rsc graph renders and no
|
|
782
|
+
// module the browser holds, so Vite has nothing to tell the browser. It is
|
|
783
|
+
// reloaded, which renders the edit; an edit to a client module — which the
|
|
784
|
+
// rsc graph only holds references to — is left to Fast Refresh.
|
|
785
|
+
hotUpdate({ modules }) {
|
|
786
|
+
if (flightState == null || server == null) return;
|
|
787
|
+
if (this.environment?.name !== RSC_ENVIRONMENT) return;
|
|
788
|
+
const serverSide = modules.some((module) => {
|
|
789
|
+
const file = module.file ?? cleanId(module.id ?? "");
|
|
790
|
+
return file !== "" && !flightState.clientModules.has(file);
|
|
791
|
+
});
|
|
792
|
+
if (serverSide) server.environments.client.hot.send({ type: "full-reload", path: "*" });
|
|
240
793
|
},
|
|
241
794
|
|
|
242
795
|
configureServer(devServer) {
|
|
243
796
|
server = devServer;
|
|
797
|
+
const diagnostics = startDevState(root, () => ({
|
|
798
|
+
routes: scanRoutes(appRoot, { target: routeTarget }).routes.map((route) => ({
|
|
799
|
+
path: route.path,
|
|
800
|
+
page: route.page,
|
|
801
|
+
layouts: route.layouts,
|
|
802
|
+
})),
|
|
803
|
+
actions: actionTables().table,
|
|
804
|
+
}));
|
|
805
|
+
devServer.middlewares.use(diagnostics.middleware);
|
|
806
|
+
devServer.httpServer?.once("close", diagnostics.close);
|
|
807
|
+
// The ssr graph's way into the rsc graph; see `devBridgeSource`.
|
|
808
|
+
if (flightState != null) {
|
|
809
|
+
globalThis[Symbol.for(DEV_RSC_HOOK)] = () =>
|
|
810
|
+
devServer.environments[RSC_ENVIRONMENT].runner.import(FLIGHT_VIRTUAL.entry);
|
|
811
|
+
}
|
|
244
812
|
devServer.httpServer?.once("close", () => {
|
|
245
813
|
service?.close();
|
|
246
814
|
service = null;
|
|
247
815
|
});
|
|
248
816
|
|
|
249
|
-
//
|
|
817
|
+
// `app.router.headers` and `redirects`, in front of Vite's own middleware
|
|
818
|
+
// — the hook's body runs before those are installed — so a redirect
|
|
819
|
+
// answers before `public/` is looked in and a header reaches a file Vite
|
|
820
|
+
// serves, as both do in front of the static half of every other door.
|
|
821
|
+
// Vite's module server is left alone: `/@vite/client` and `/@fs/…` are
|
|
822
|
+
// development plumbing no deployment has, and `/__uf/` is the browser's
|
|
823
|
+
// channel back. Not mounted at all for a project with neither list.
|
|
824
|
+
if (answersInFrontOfFiles(routing)) {
|
|
825
|
+
devServer.middlewares.use((request, response, next) => {
|
|
826
|
+
const url = request.url ?? "/";
|
|
827
|
+
// Vite serves its own paths under the base path too, so they are
|
|
828
|
+
// recognised once it is taken off.
|
|
829
|
+
const under =
|
|
830
|
+
routing.basePath !== "" && url.startsWith(`${routing.basePath}/`)
|
|
831
|
+
? url.slice(routing.basePath.length)
|
|
832
|
+
: url;
|
|
833
|
+
if (
|
|
834
|
+
under.startsWith("/@") ||
|
|
835
|
+
under.startsWith("/node_modules/") ||
|
|
836
|
+
under.startsWith("/__uf/")
|
|
837
|
+
) {
|
|
838
|
+
next();
|
|
839
|
+
return;
|
|
840
|
+
}
|
|
841
|
+
answerRouting(routing, toAddressRequest(request), response)
|
|
842
|
+
.then((answered) => {
|
|
843
|
+
if (answered) return;
|
|
844
|
+
// The bare base path is the root, which Vite only knows as
|
|
845
|
+
// `/docs/`; see `forViteBase`.
|
|
846
|
+
forViteBase(routing, request);
|
|
847
|
+
next();
|
|
848
|
+
})
|
|
849
|
+
.catch(next);
|
|
850
|
+
});
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
// A reserved file appearing or disappearing changes the route table,
|
|
250
854
|
// which lives in a virtual module the watcher knows nothing about.
|
|
251
|
-
|
|
855
|
+
//
|
|
856
|
+
// Built from `RESERVED` rather than written out. It used to be the
|
|
857
|
+
// literal `(page|layout|middleware|not-found)`, which is a fourth
|
|
858
|
+
// spelling of a grammar that already has three, and it was already
|
|
859
|
+
// missing `route` — so adding a route handler to a running dev server
|
|
860
|
+
// did not rebuild the table and the handler stayed invisible until a
|
|
861
|
+
// restart. A list that has to match another list has to be that list.
|
|
862
|
+
const escapeRegExp = (value) => value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
863
|
+
const stems = Object.values(RESERVED).map(escapeRegExp).join("|");
|
|
864
|
+
const reserved = new RegExp(`/(${stems})(\\.[a-z]+)?\\.(js|jsx|mdx)$`);
|
|
252
865
|
const onRouteFile = (file) => {
|
|
253
866
|
if (!reserved.test(file) || !file.startsWith(appRoot)) return;
|
|
254
867
|
const routes = devServer.moduleGraph.getModuleById(resolved(VIRTUAL.routes));
|
|
@@ -258,29 +871,332 @@ function flowPlugin({ routerRoot, appEntry, command }) {
|
|
|
258
871
|
devServer.watcher.on("add", onRouteFile);
|
|
259
872
|
devServer.watcher.on("unlink", onRouteFile);
|
|
260
873
|
|
|
874
|
+
// The same problem one level up. Adding `"use client"` to a module, or
|
|
875
|
+
// deleting the import that reached it, changes which routes the browser
|
|
876
|
+
// is given a page for — and touches no reserved file name, so nothing
|
|
877
|
+
// above notices. `uf dev` rewrites the RSC manifest when the analysis
|
|
878
|
+
// moves and only then, so this fires when the answer changed rather than
|
|
879
|
+
// on every keystroke. Watched explicitly because the file is uf's own
|
|
880
|
+
// artefact and is in no module graph.
|
|
881
|
+
const manifestFile = process.env[RSC_MANIFEST_ENV];
|
|
882
|
+
if (manifestFile != null && manifestFile !== "") {
|
|
883
|
+
const manifestPath = path.resolve(manifestFile);
|
|
884
|
+
devServer.watcher.add(manifestPath);
|
|
885
|
+
const onManifest = (file) => {
|
|
886
|
+
if (path.resolve(file) !== manifestPath) return;
|
|
887
|
+
const previousActions = actionTables().modules;
|
|
888
|
+
// The action tables are read from the same file and are memoised on
|
|
889
|
+
// its size and modification time, which is a pair two writes inside
|
|
890
|
+
// one millisecond can share. This is the answer that does not
|
|
891
|
+
// depend on a clock.
|
|
892
|
+
forgetActions();
|
|
893
|
+
const nextActions = actionTables().modules;
|
|
894
|
+
invalidateActionModules(devServer.moduleGraph, previousActions);
|
|
895
|
+
invalidateActionModules(devServer.moduleGraph, nextActions);
|
|
896
|
+
const routes = devServer.moduleGraph.getModuleById(resolved(VIRTUAL.routes));
|
|
897
|
+
if (routes) devServer.moduleGraph.invalidateModule(routes);
|
|
898
|
+
const actions = devServer.moduleGraph.getModuleById(resolved(VIRTUAL.actions));
|
|
899
|
+
if (actions) devServer.moduleGraph.invalidateModule(actions);
|
|
900
|
+
devServer.ws.send({ type: "full-reload", path: "*" });
|
|
901
|
+
};
|
|
902
|
+
devServer.watcher.on("add", onManifest);
|
|
903
|
+
devServer.watcher.on("change", onManifest);
|
|
904
|
+
}
|
|
905
|
+
|
|
261
906
|
// After Vite's own middlewares, so `/@vite/client`, `/@id/...` and
|
|
262
|
-
// static files are served first and only
|
|
263
|
-
//
|
|
907
|
+
// static files are served first and only what Vite declined reaches uf.
|
|
908
|
+
//
|
|
909
|
+
// # One renderer
|
|
910
|
+
//
|
|
911
|
+
// This is the only middleware that renders a document under `uf dev`,
|
|
912
|
+
// and that is worth stating because there were two. `driver.js` added a
|
|
913
|
+
// second one after `createServer` had returned, which put it *later* in
|
|
914
|
+
// the connect stack than this one — so for every request this one
|
|
915
|
+
// claimed, this one decided, and the other was reached only for what
|
|
916
|
+
// this one declined. Route-handler dispatch was in the other. A
|
|
917
|
+
// `GET /feed` from a browser is `Accept: text/html` with no extension,
|
|
918
|
+
// so it looked like a document, so `app/feed/$route.js` was never
|
|
919
|
+
// asked and the reader got the route table's page — or the not-found
|
|
920
|
+
// page — for a path that had a handler. See ubugeeei-prod/uf#349.
|
|
921
|
+
//
|
|
922
|
+
// Two renderers is also how the two came to disagree about the render
|
|
923
|
+
// result's `headers`: this one wrote them and the driver's did not, so a
|
|
924
|
+
// loader calling `redirect()` answered a browser with a `307` carrying
|
|
925
|
+
// no `Location` and a meta-refresh body — a redirect that works when you
|
|
926
|
+
// deploy it and not while you are writing it. See
|
|
927
|
+
// ubugeeei-prod/uf#338.
|
|
928
|
+
//
|
|
929
|
+
// So the driver's middleware is gone and everything it did is here: it
|
|
930
|
+
// claims every request, runs the guard, the action endpoint and the
|
|
931
|
+
// dispatcher for every method, and hands back to Vite's chain what none
|
|
932
|
+
// of them answered.
|
|
933
|
+
//
|
|
934
|
+
// # What is still not `createFetchHandler`
|
|
935
|
+
//
|
|
936
|
+
// One middleware rather than two, and still not the function every
|
|
937
|
+
// deployment runs. It cannot be: `transformIndexHtml` takes a whole
|
|
938
|
+
// document, so the render has to be collected here rather than streamed
|
|
939
|
+
// (ubugeeei-prod/uf#374), and a request nothing claimed has to go back
|
|
940
|
+
// to Vite's chain rather than become a 404 — neither of which a handler
|
|
941
|
+
// that always answers with a `Response` can do.
|
|
942
|
+
//
|
|
943
|
+
// What that still costs, written down so the next reader does not have
|
|
944
|
+
// to find it: `createFetchHandler` renders inside a cache scope even
|
|
945
|
+
// with no cache configured, so a component calling `cacheLife` states a
|
|
946
|
+
// lifetime nobody honours; here there is no scope, so the same component
|
|
947
|
+
// throws under `uf dev` and renders under `uf start`. Closing that means
|
|
948
|
+
// the generated server entry handing the host a scope the way it already
|
|
949
|
+
// hands it `beginRequest` — see `serverModuleSource` in
|
|
950
|
+
// `./internal/routes.js` — and it is the next thing to remove from this
|
|
951
|
+
// list rather than something this middleware can decide on its own.
|
|
264
952
|
return () => {
|
|
953
|
+
// The browser's own reporting channel, mounted above the application
|
|
954
|
+
// so that a report never reaches a project's `$middleware.js` or
|
|
955
|
+
// its route table. See `internal/diagnostics.js`.
|
|
956
|
+
devServer.middlewares.use(
|
|
957
|
+
createChannelMiddleware((diagnostic) => emit("diagnostic", diagnostic)),
|
|
958
|
+
);
|
|
959
|
+
|
|
265
960
|
devServer.middlewares.use(async (request, response, next) => {
|
|
266
|
-
|
|
961
|
+
// `request.url` and not `originalUrl`, which is the URL Vite's base
|
|
962
|
+
// middleware has already stripped the base from — and the route
|
|
963
|
+
// table's paths have no base in them either.
|
|
964
|
+
//
|
|
965
|
+
// `let`, because a rewrite moves it: from here on it is the address
|
|
966
|
+
// the application answers for, which is what the render, the payload
|
|
967
|
+
// and the document test below all have to agree on.
|
|
968
|
+
let url = request.url ?? "/";
|
|
969
|
+
// Declared out here so the catch below can still settle: a request
|
|
970
|
+
// that failed is a request that happened, and a middleware that
|
|
971
|
+
// logged its arrival is owed its callback either way.
|
|
972
|
+
let lifecycle = null;
|
|
267
973
|
try {
|
|
268
|
-
const
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
974
|
+
const entry = await importServerEntry(devServer);
|
|
975
|
+
// At `request.url`, which Vite's base middleware has already taken
|
|
976
|
+
// `app.router.basePath` off: the application path, as every other
|
|
977
|
+
// front door hands the application once it has admitted a request.
|
|
978
|
+
const arrived = await toRequest(request, devServer.config, url);
|
|
979
|
+
// `app.router.rewrites`, where `createFetchHandler` applies them for
|
|
980
|
+
// every other door: after the files Vite already served, before the
|
|
981
|
+
// guard — so the guard that runs is the destination's.
|
|
982
|
+
let asRequest = (await rewriteRouting(entry.routing, arrived)) ?? arrived;
|
|
983
|
+
if (asRequest !== arrived) url = addressOf(asRequest);
|
|
984
|
+
|
|
985
|
+
// The request begins here and ends when the response has been
|
|
986
|
+
// written, which is what `after()` promises and what `uf preview`,
|
|
987
|
+
// `uf start` and a compiled binary all do too — a middleware that
|
|
988
|
+
// logs a response's status has to mean the same thing in
|
|
989
|
+
// development as in production. See `internal/serve.js` and
|
|
990
|
+
// ubugeeei-prod/uf#389.
|
|
991
|
+
lifecycle = await beginRequest(entry, arrived);
|
|
992
|
+
const answered = await lifecycle.run(async () => {
|
|
993
|
+
// Before anything answers: a middleware guards a subtree, so it
|
|
994
|
+
// has to run for a page, for a route handler, and for a path
|
|
995
|
+
// under it that matches neither. Running it inside the
|
|
996
|
+
// dispatcher and again inside the renderer would have left
|
|
997
|
+
// `/dashboard/typo` unguarded and run it twice for a path that
|
|
998
|
+
// is both. See ubugeeei-prod/uf#260.
|
|
999
|
+
//
|
|
1000
|
+
// A `Request` back is a middleware's `rewrite()`, already past
|
|
1001
|
+
// the destination's own middleware.
|
|
1002
|
+
const guarded = await entry.runMiddleware(asRequest);
|
|
1003
|
+
if (guarded instanceof Request) {
|
|
1004
|
+
asRequest = guarded;
|
|
1005
|
+
url = addressOf(guarded);
|
|
1006
|
+
} else if (guarded != null) {
|
|
1007
|
+
await send(response, guarded);
|
|
1008
|
+
return true;
|
|
1009
|
+
}
|
|
1010
|
+
|
|
1011
|
+
// Then a server action, below the guard and above the handlers.
|
|
1012
|
+
// It declines every request that carries no action id, so this
|
|
1013
|
+
// costs an ordinary request one `headers.get`; and it answers
|
|
1014
|
+
// every request that carries one, refusals included, so an
|
|
1015
|
+
// action can never fall through to a route handler that happens
|
|
1016
|
+
// to sit at the URL it was posted to. The same two lines are in
|
|
1017
|
+
// `@uniflowed/server`'s `fetch.js`, which is what every
|
|
1018
|
+
// deployment runs.
|
|
1019
|
+
const acted = await entry.callAction(asRequest);
|
|
1020
|
+
if (acted != null) {
|
|
1021
|
+
await send(response, acted);
|
|
1022
|
+
return true;
|
|
1023
|
+
}
|
|
1024
|
+
|
|
1025
|
+
// Then the route handlers, above the renderer and for every
|
|
1026
|
+
// method: a path that answers a request is not a document,
|
|
1027
|
+
// whatever the client said it would accept. `curl /api/thing`
|
|
1028
|
+
// and a `<form action>` navigation both send `Accept:
|
|
1029
|
+
// text/html`, and both want the handler's answer — which is the
|
|
1030
|
+
// whole of ubugeeei-prod/uf#349.
|
|
1031
|
+
// A browser that is navigating, asking for the next route's
|
|
1032
|
+
// payload: after the guard and before the handlers, for the
|
|
1033
|
+
// reasons `@uniflowed/server`'s `internal/flight.js` gives.
|
|
1034
|
+
const payloadFor =
|
|
1035
|
+
entry.flight == null ? null : flightDocumentPath(url.split("?")[0]);
|
|
1036
|
+
if (payloadFor != null && (request.method === "GET" || request.method === "HEAD")) {
|
|
1037
|
+
const query = url.includes("?") ? url.slice(url.indexOf("?")) : "";
|
|
1038
|
+
const target = payloadFor + query;
|
|
1039
|
+
const fromHeader = request.headers[INTERCEPTED_FROM_HEADER];
|
|
1040
|
+
const answered = await entry.flight(target, {
|
|
1041
|
+
onError: (error) => reportRenderError(devServer, target, error),
|
|
1042
|
+
interceptedFrom: Array.isArray(fromHeader) ? fromHeader[0] : fromHeader,
|
|
1043
|
+
});
|
|
1044
|
+
if (answered.error != null) reportRenderError(devServer, target, answered.error);
|
|
1045
|
+
if (request.method === "HEAD") await answered.stream?.cancel();
|
|
1046
|
+
await send(
|
|
1047
|
+
response,
|
|
1048
|
+
new Response(request.method === "HEAD" ? null : answered.stream, {
|
|
1049
|
+
status: answered.status,
|
|
1050
|
+
headers: answered.headers,
|
|
1051
|
+
}),
|
|
1052
|
+
);
|
|
1053
|
+
return true;
|
|
1054
|
+
}
|
|
1055
|
+
|
|
1056
|
+
const handled = await entry.dispatch(asRequest);
|
|
1057
|
+
if (handled != null) {
|
|
1058
|
+
await send(response, handled);
|
|
1059
|
+
return true;
|
|
1060
|
+
}
|
|
1061
|
+
|
|
1062
|
+
// Only a navigation reaches the renderer. A page cannot answer a
|
|
1063
|
+
// `POST`, and letting one try would turn a missing handler into
|
|
1064
|
+
// a rendered page with a 200 rather than a 404.
|
|
1065
|
+
//
|
|
1066
|
+
// `notADocumentBecause` is stricter than the production handler, which
|
|
1067
|
+
// renders anything a static file did not answer, and the
|
|
1068
|
+
// difference is Vite's chain: `/@id/…`, `/node_modules/…` and
|
|
1069
|
+
// any path with an extension belong to the module server, and a
|
|
1070
|
+
// request one of those declined has to go back to it rather than
|
|
1071
|
+
// become a rendered 404 page. What it costs is that
|
|
1072
|
+
// `/favicon.svg` on a project that has none is a bare 404 here
|
|
1073
|
+
// and the project's own not-found *page* under `uf preview` and
|
|
1074
|
+
// `uf start` — a difference in the body of a 404 for a path that
|
|
1075
|
+
// is an asset request in the first place.
|
|
1076
|
+
const notDocument = notADocumentBecause(request, url);
|
|
1077
|
+
if (notDocument === "accept") {
|
|
1078
|
+
// Everything about this is a navigation except the header, and
|
|
1079
|
+
// the path is one uf renders. Say so, rather than letting the
|
|
1080
|
+
// chain below answer `Cannot GET /` in somebody else's words.
|
|
1081
|
+
documentNeedsHtml(request, response);
|
|
1082
|
+
return true;
|
|
1083
|
+
}
|
|
1084
|
+
if (notDocument != null) return false;
|
|
1085
|
+
|
|
1086
|
+
// A single-page project's deployment answers every navigation
|
|
1087
|
+
// with the same empty shell, so this does too. Rendering the
|
|
1088
|
+
// route here instead would have been the better-looking dev
|
|
1089
|
+
// server and the wrong one: a page that only works because the
|
|
1090
|
+
// server rendered it would work all through development and be
|
|
1091
|
+
// blank the day it shipped. It is the same argument
|
|
1092
|
+
// `app.rendering.navigation` makes about a link, one level up.
|
|
1093
|
+
//
|
|
1094
|
+
// The three steps above still ran — the guard, the action, the
|
|
1095
|
+
// handler — and each of them is something `uf build` refuses in
|
|
1096
|
+
// a `["csr"]` project by name. A dev server that skipped them
|
|
1097
|
+
// would hide the very thing the build is going to stop.
|
|
1098
|
+
if (mount === "render") {
|
|
1099
|
+
response.statusCode = 200;
|
|
1100
|
+
response.setHeader("content-type", "text/html; charset=utf-8");
|
|
1101
|
+
const shell = entry.shellDocument({
|
|
1102
|
+
scripts: [devUrlFor(VIRTUAL.client)],
|
|
1103
|
+
styles: [],
|
|
1104
|
+
preloads: [],
|
|
1105
|
+
});
|
|
1106
|
+
response.end(await devServer.transformIndexHtml(url, shell));
|
|
1107
|
+
return true;
|
|
1108
|
+
}
|
|
1109
|
+
|
|
1110
|
+
const result = await entry.render(
|
|
1111
|
+
url,
|
|
1112
|
+
{ scripts: [devUrlFor(VIRTUAL.client)], styles: [], preloads: [] },
|
|
1113
|
+
{
|
|
1114
|
+
// A boundary that threw after the shell went out.
|
|
1115
|
+
// `result.error` cannot carry it — the caller already has the
|
|
1116
|
+
// result by then — so the terminal hears about it here or not
|
|
1117
|
+
// at all.
|
|
1118
|
+
onError: (error) => reportRenderError(devServer, url, error),
|
|
1119
|
+
// Vite sees the head and only the head. That is what lets the
|
|
1120
|
+
// development server stream like every other host — see below.
|
|
1121
|
+
//
|
|
1122
|
+
// Under React Server Components it is also where a server
|
|
1123
|
+
// component's stylesheets are linked in development: they are
|
|
1124
|
+
// in the rsc graph, which the browser never loads, and they
|
|
1125
|
+
// enter it when the route's modules are imported — after this
|
|
1126
|
+
// call and before the head is written, which is when this
|
|
1127
|
+
// runs. See `devStylesheets`.
|
|
1128
|
+
transformHead: (head) =>
|
|
1129
|
+
devServer.transformIndexHtml(
|
|
1130
|
+
url,
|
|
1131
|
+
flightState == null ? head : linkStylesheets(head, devStylesheets(devServer)),
|
|
1132
|
+
),
|
|
1133
|
+
// And what the streaming actually did, when it changed. The
|
|
1134
|
+
// router has already decided there is something worth saying
|
|
1135
|
+
// and written the words — see its `internal/inspector.js`,
|
|
1136
|
+
// which cannot be imported from here because this file is
|
|
1137
|
+
// plain JavaScript that Vite loads before any Flow transform
|
|
1138
|
+
// exists. `info`, because a page that streamed is a
|
|
1139
|
+
// measurement and not a problem; `origin`, because a report
|
|
1140
|
+
// that does not say which page produced it is one somebody
|
|
1141
|
+
// has to reproduce before they can act on it.
|
|
1142
|
+
onStream: (diagnostic) =>
|
|
1143
|
+
emit("diagnostic", {
|
|
1144
|
+
severity: "info",
|
|
1145
|
+
origin: url,
|
|
1146
|
+
message: diagnostic.message,
|
|
1147
|
+
detail: diagnostic.detail,
|
|
1148
|
+
}),
|
|
1149
|
+
},
|
|
1150
|
+
);
|
|
1151
|
+
if (result.error != null) reportRenderError(devServer, url, result.error);
|
|
1152
|
+
// Piped, like every other host. This used to collect the whole
|
|
1153
|
+
// document and transform it at the end, because
|
|
1154
|
+
// `transformIndexHtml` is a *whole document* hook — which made the
|
|
1155
|
+
// one place a developer would notice streaming the one place it
|
|
1156
|
+
// did not happen: a slow page showed nothing until it was finished
|
|
1157
|
+
// and `$loading.js` looked broken.
|
|
1158
|
+
//
|
|
1159
|
+
// `transformHead` above is the seam. `internal/stream.js` already
|
|
1160
|
+
// held the opening chunk back until the head was complete and
|
|
1161
|
+
// forwarded everything after it untouched, so the hook only ever
|
|
1162
|
+
// sees the head — and Vite's dev hook handles a document that ends
|
|
1163
|
+
// mid-`<body>` without complaint, which was the open question on
|
|
1164
|
+
// ubugeeei-prod/uf#374.
|
|
1165
|
+
response.statusCode = result.status ?? 200;
|
|
1166
|
+
// The render's own headers, then the content type over the top:
|
|
1167
|
+
// exactly the order `@uniflowed/server`'s `fetch.js` writes them
|
|
1168
|
+
// in, so a `Location` from `redirect()` survives here and a
|
|
1169
|
+
// render cannot claim to be something other than a document.
|
|
1170
|
+
for (const [name, value] of Object.entries(result.headers ?? {})) {
|
|
1171
|
+
response.setHeader(name, value);
|
|
1172
|
+
}
|
|
1173
|
+
response.setHeader("content-type", "text/html; charset=utf-8");
|
|
1174
|
+
await result.pipe(response);
|
|
1175
|
+
return true;
|
|
274
1176
|
});
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
1177
|
+
|
|
1178
|
+
if (!answered) {
|
|
1179
|
+
// The one path where uf is not the one writing the response: a
|
|
1180
|
+
// request nothing claimed goes back to Vite's chain. The guard
|
|
1181
|
+
// has still run and may have deferred work, so `close` — the
|
|
1182
|
+
// socket saying the response is over, however it ended — is the
|
|
1183
|
+
// only honest signal left that the bytes are out.
|
|
1184
|
+
response.once("close", lifecycle.settle);
|
|
1185
|
+
next();
|
|
1186
|
+
return;
|
|
280
1187
|
}
|
|
281
|
-
|
|
1188
|
+
await lifecycle.settle();
|
|
282
1189
|
} catch (error) {
|
|
283
|
-
|
|
1190
|
+
if (lifecycle != null) await lifecycle.settle();
|
|
1191
|
+
// Map the stack back onto the Flow source before it reaches the
|
|
1192
|
+
// overlay.
|
|
1193
|
+
if (error instanceof Error) devServer.ssrFixStacktrace(error);
|
|
1194
|
+
emit("diagnostic", {
|
|
1195
|
+
severity: "error",
|
|
1196
|
+
kind: "runtime",
|
|
1197
|
+
origin: url,
|
|
1198
|
+
...errorEvent(error),
|
|
1199
|
+
});
|
|
284
1200
|
next(error);
|
|
285
1201
|
}
|
|
286
1202
|
});
|
|
@@ -303,11 +1219,7 @@ function mdxPlugin(markdown) {
|
|
|
303
1219
|
enforce: "pre",
|
|
304
1220
|
...mdx({
|
|
305
1221
|
jsxImportSource: "react",
|
|
306
|
-
remarkPlugins: [
|
|
307
|
-
remarkGfm,
|
|
308
|
-
remarkFrontmatter,
|
|
309
|
-
[remarkMdxFrontmatter, { name: "frontmatter" }],
|
|
310
|
-
],
|
|
1222
|
+
remarkPlugins: [remarkGfm, remarkFrontmatter, remarkFrontmatterExport],
|
|
311
1223
|
rehypePlugins,
|
|
312
1224
|
}),
|
|
313
1225
|
name: "uf:mdx",
|
|
@@ -332,40 +1244,228 @@ async function importServerEntry(devServer) {
|
|
|
332
1244
|
return devServer.ssrLoadModule(VIRTUAL.server);
|
|
333
1245
|
}
|
|
334
1246
|
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
1247
|
+
/**
|
|
1248
|
+
* The path and query a `Request` is for, which is what the dev server's
|
|
1249
|
+
* renderer and payload test are keyed on.
|
|
1250
|
+
*
|
|
1251
|
+
* @param {Request} request
|
|
1252
|
+
*/
|
|
1253
|
+
function addressOf(request) {
|
|
1254
|
+
const url = new URL(request.url);
|
|
1255
|
+
return url.pathname + url.search;
|
|
1256
|
+
}
|
|
1257
|
+
|
|
1258
|
+
/**
|
|
1259
|
+
* Why `request` is not a document request, or `null` when it is one.
|
|
1260
|
+
*
|
|
1261
|
+
* `url` is the address the application answers for, which a rewrite may have
|
|
1262
|
+
* moved away from the one the request line named.
|
|
1263
|
+
*
|
|
1264
|
+
* A reason rather than a boolean because one of the four is worth saying out
|
|
1265
|
+
* loud. Three of them mean the request belongs to somebody else — Vite's module
|
|
1266
|
+
* server, a static file, or a method a page cannot answer — and handing it back
|
|
1267
|
+
* is the whole point. The fourth means the client asked for a page uf would
|
|
1268
|
+
* have rendered and did not say it accepts HTML, and `curl` is the client that
|
|
1269
|
+
* does that. See ubugeeei-prod/uf#675.
|
|
1270
|
+
*
|
|
1271
|
+
* The extension test moved above the `accept` test so the two cannot be
|
|
1272
|
+
* confused: `/favicon.svg` with `Accept: *\/*` is an asset, not a navigation
|
|
1273
|
+
* with the wrong header, and still goes back to Vite's chain untouched.
|
|
1274
|
+
*/
|
|
1275
|
+
function notADocumentBecause(request, url = request.url ?? "/") {
|
|
1276
|
+
if (request.method !== "GET" && request.method !== "HEAD") return "method";
|
|
1277
|
+
if (url.startsWith("/@") || url.startsWith("/node_modules/")) return "module-server";
|
|
341
1278
|
const pathname = url.split("?")[0];
|
|
342
1279
|
// A request for a file — `/favicon.svg`, `/assets/x.js` — that no static
|
|
343
1280
|
// middleware answered is a 404, not a page.
|
|
344
|
-
|
|
1281
|
+
if (/\.[a-z0-9]+$/i.test(pathname)) return "asset";
|
|
1282
|
+
const accept = request.headers.accept ?? "";
|
|
1283
|
+
if (!accept.includes("text/html")) return "accept";
|
|
1284
|
+
return null;
|
|
345
1285
|
}
|
|
346
1286
|
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
1287
|
+
/**
|
|
1288
|
+
* The 404 for a navigation that did not ask for HTML.
|
|
1289
|
+
*
|
|
1290
|
+
* uf's own words rather than the framework underneath saying `Cannot GET /` in
|
|
1291
|
+
* its: the path is one uf would have rendered, and the only thing that stopped
|
|
1292
|
+
* it is a header the reader cannot see from the terminal. Development only —
|
|
1293
|
+
* this middleware is the dev server — and `uf preview` and `uf start` answer a
|
|
1294
|
+
* 404 with nothing in it, as `docs/security.md` requires.
|
|
1295
|
+
*
|
|
1296
|
+
* The client's `Accept` is quoted back, so it is treated the way every other
|
|
1297
|
+
* piece of foreign text here is treated: control characters removed and the
|
|
1298
|
+
* length capped. A header is not a thing a terminal should be asked to run.
|
|
1299
|
+
* See ubugeeei-prod/uf#649.
|
|
1300
|
+
*/
|
|
1301
|
+
function documentNeedsHtml(request, response) {
|
|
1302
|
+
const accept = String(request.headers.accept ?? "")
|
|
1303
|
+
// eslint-disable-next-line no-control-regex
|
|
1304
|
+
.replace(/[\u0000-\u001f\u007f]/g, " ")
|
|
1305
|
+
.slice(0, 80);
|
|
1306
|
+
const path = String(request.url ?? "/").slice(0, 200);
|
|
1307
|
+
const body =
|
|
1308
|
+
`404 ${request.method} ${path}\n\n` +
|
|
1309
|
+
"This path is a page, and a page is rendered for a request that accepts\n" +
|
|
1310
|
+
`HTML. This request sent \`Accept: ${accept || "(none)"}\`.\n\n` +
|
|
1311
|
+
` curl -H 'Accept: text/html' http://${request.headers.host ?? "localhost"}${path}\n\n` +
|
|
1312
|
+
"A browser sends it; `curl` does not. Nothing is wrong with the route.\n";
|
|
1313
|
+
response.statusCode = 404;
|
|
1314
|
+
response.setHeader("content-type", "text/plain; charset=utf-8");
|
|
1315
|
+
response.end(body);
|
|
350
1316
|
}
|
|
351
1317
|
|
|
1318
|
+
/** The name of the environment variable that turns every finding back on. */
|
|
1319
|
+
const ALL_DIAGNOSTICS = "UF_REACT_COMPILER_DIAGNOSTICS";
|
|
1320
|
+
|
|
352
1321
|
/**
|
|
353
|
-
*
|
|
354
|
-
*
|
|
1322
|
+
* Report what the React Compiler said about one module.
|
|
1323
|
+
*
|
|
1324
|
+
* Every finding used to be printed as `a function: <message>` — no file, no
|
|
1325
|
+
* line, no column, and the fallback string doing all the work, because
|
|
1326
|
+
* `diagnostic.function` was read out of a field only a *success* event carries
|
|
1327
|
+
* and so was null for every finding there has ever been (#371), not because
|
|
1328
|
+
* the compiler was reporting on anonymous inner functions. The transform hook
|
|
1329
|
+
* knows the module and the compiler gives a position for most findings, so
|
|
1330
|
+
* both go into the message: Vite prints a plugin warning's `message` and
|
|
1331
|
+
* nothing else, so a location that is not in the string is a location the
|
|
1332
|
+
* reader never sees. `id` and `loc` go along for anything reading the log
|
|
1333
|
+
* object rather than the line. See ubugeeei-prod/uf#307.
|
|
1334
|
+
*
|
|
1335
|
+
* `(in Form)` is real now: `uf transform` recovers the name from the tree it
|
|
1336
|
+
* compiled, and leaves it off the diagnostic when the function genuinely has
|
|
1337
|
+
* no name to give.
|
|
1338
|
+
*
|
|
1339
|
+
* The position is real too, and is the expression the compiler objected to
|
|
1340
|
+
* rather than the function containing it, so the two halves of the line say
|
|
1341
|
+
* different things — `packages/hooks/dom.js:195:7: This value cannot be
|
|
1342
|
+
* modified (in useLongPress)` locates the write and names the hook to look in.
|
|
1343
|
+
*
|
|
1344
|
+
* A dependency's findings are held back. A React Compiler bailout inside
|
|
1345
|
+
* `@uniflowed/form` is not something the person running the build can fix, and
|
|
1346
|
+
* a channel carrying forty of them on every build is a channel people stop
|
|
1347
|
+
* reading — which costs them the one finding that was theirs. They are counted
|
|
1348
|
+
* and said once instead, and `UF_REACT_COMPILER_DIAGNOSTICS=all` prints every
|
|
1349
|
+
* one for whoever is fixing the dependency.
|
|
355
1350
|
*/
|
|
356
|
-
function
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
1351
|
+
function reportDiagnostics(context, { id, root, diagnostics, environment, reported, suppressed }) {
|
|
1352
|
+
if (diagnostics.length === 0) return;
|
|
1353
|
+
// A module transformed again by the environment that first reported it has
|
|
1354
|
+
// been edited; anything else is the second bundle passing over the same file.
|
|
1355
|
+
const previous = reported.get(id);
|
|
1356
|
+
const ledger =
|
|
1357
|
+
previous != null && previous.environment !== environment
|
|
1358
|
+
? previous
|
|
1359
|
+
: { environment, signatures: new Set() };
|
|
1360
|
+
reported.set(id, ledger);
|
|
1361
|
+
|
|
1362
|
+
const file = relativeId(root, id);
|
|
1363
|
+
const mine = isProjectModule(root, id) || process.env[ALL_DIAGNOSTICS] === "all";
|
|
1364
|
+
for (const diagnostic of diagnostics) {
|
|
1365
|
+
// Everything a reader would be shown, so two findings that would print as
|
|
1366
|
+
// the same line collapse into one.
|
|
1367
|
+
//
|
|
1368
|
+
// This used to collapse far more than that, and the note here used to say
|
|
1369
|
+
// the compiler reports "Cannot access refs during render" once per pass
|
|
1370
|
+
// that noticed it — three times for one `ref`. That was the wrong reading
|
|
1371
|
+
// of the evidence. The position in a finding was the position of the
|
|
1372
|
+
// *function* it was found in, so every finding in one function shared a
|
|
1373
|
+
// line and a column and any two with the same message were, to this
|
|
1374
|
+
// signature, the same finding. They were not: over this repository's own
|
|
1375
|
+
// packages it collapsed 173 findings into 119 lines, and the five that
|
|
1376
|
+
// became one line in `DatePickerInput` are five different reads of a ref
|
|
1377
|
+
// on five different lines. `uf transform` now reports where the compiler
|
|
1378
|
+
// actually objected, so the signature separates them, and what it still
|
|
1379
|
+
// collapses is a genuine repeat of one site.
|
|
1380
|
+
const signature = `${diagnostic.kind}\0${diagnostic.line}\0${diagnostic.column}\0${diagnostic.message}`;
|
|
1381
|
+
if (ledger.signatures.has(signature)) continue;
|
|
1382
|
+
ledger.signatures.add(signature);
|
|
1383
|
+
if (!mine) {
|
|
1384
|
+
suppressed.push(file);
|
|
1385
|
+
continue;
|
|
365
1386
|
}
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
1387
|
+
// Two conventions, both honoured. uf's own frames count columns from one
|
|
1388
|
+
// (`uf_term::diagnostic`) and so does every editor a reader will paste
|
|
1389
|
+
// `file:line:column` into; Rollup's `loc.column` counts from zero, which is
|
|
1390
|
+
// what the compiler already gave us. The string gets the reader's number
|
|
1391
|
+
// and the log object gets Rollup's.
|
|
1392
|
+
const at = diagnostic.line == null ? "" : `:${diagnostic.line}:${(diagnostic.column ?? 0) + 1}`;
|
|
1393
|
+
const who = diagnostic.function == null ? "" : ` (in ${diagnostic.function})`;
|
|
1394
|
+
context.warn?.({
|
|
1395
|
+
message: `${file}${at}: ${diagnostic.message}${who}`,
|
|
1396
|
+
id,
|
|
1397
|
+
loc:
|
|
1398
|
+
diagnostic.line == null
|
|
1399
|
+
? undefined
|
|
1400
|
+
: { file: id, line: diagnostic.line, column: diagnostic.column ?? 0 },
|
|
1401
|
+
});
|
|
369
1402
|
}
|
|
370
|
-
|
|
1403
|
+
}
|
|
1404
|
+
|
|
1405
|
+
/**
|
|
1406
|
+
* Say how many findings were a dependency's, and whose.
|
|
1407
|
+
*
|
|
1408
|
+
* Held back is not the same as hidden: a build that quietly drops forty
|
|
1409
|
+
* findings is a build that has decided for the reader that uf has no bugs. One
|
|
1410
|
+
* line names the packages and how to see the rest.
|
|
1411
|
+
*/
|
|
1412
|
+
function summariseSuppressed(context, suppressed) {
|
|
1413
|
+
if (suppressed.length === 0) return;
|
|
1414
|
+
const packages = [...new Set(suppressed.map(packageOf))].sort();
|
|
1415
|
+
const count = suppressed.length;
|
|
1416
|
+
context.warn?.(
|
|
1417
|
+
`${count} React Compiler ${count === 1 ? "finding" : "findings"} in ` +
|
|
1418
|
+
`${packages.join(", ")} — not this application's to fix; ` +
|
|
1419
|
+
`set ${ALL_DIAGNOSTICS}=all to see them`,
|
|
1420
|
+
);
|
|
1421
|
+
}
|
|
1422
|
+
|
|
1423
|
+
/**
|
|
1424
|
+
* Whether a finding about this module is the application author's to act on.
|
|
1425
|
+
*
|
|
1426
|
+
* Inside the project root *and* outside `node_modules`, rather than
|
|
1427
|
+
* `node_modules` alone: uf's own packages reach an application through a
|
|
1428
|
+
* workspace link in this repository and through `node_modules` everywhere
|
|
1429
|
+
* else, and they are no more the reader's code in one case than the other.
|
|
1430
|
+
*/
|
|
1431
|
+
function isProjectModule(root, id) {
|
|
1432
|
+
const relative = path.relative(root, id);
|
|
1433
|
+
if (relative.startsWith("..") || path.isAbsolute(relative)) return false;
|
|
1434
|
+
return !relative.split(path.sep).includes("node_modules");
|
|
1435
|
+
}
|
|
1436
|
+
|
|
1437
|
+
/** A module's path as a reader would write it: relative, with forward slashes. */
|
|
1438
|
+
function relativeId(root, id) {
|
|
1439
|
+
const relative = path.relative(root, id);
|
|
1440
|
+
return relative === "" ? id : relative.split(path.sep).join("/");
|
|
1441
|
+
}
|
|
1442
|
+
|
|
1443
|
+
/** The package a module belongs to, for the one line that names them. */
|
|
1444
|
+
function packageOf(file) {
|
|
1445
|
+
const parts = file.split("/");
|
|
1446
|
+
const at = parts.lastIndexOf("node_modules");
|
|
1447
|
+
if (at !== -1) {
|
|
1448
|
+
const scoped = parts[at + 1]?.startsWith("@");
|
|
1449
|
+
return parts.slice(at + 1, at + (scoped ? 3 : 2)).join("/");
|
|
1450
|
+
}
|
|
1451
|
+
// No `node_modules` in the path: a workspace link, resolved to a checkout.
|
|
1452
|
+
// The directory the module hangs off is the closest thing to a package name
|
|
1453
|
+
// that is true without reading its `package.json` from a warning path.
|
|
1454
|
+
const up = parts.lastIndexOf("packages");
|
|
1455
|
+
return up === -1 ? parts.slice(0, -1).join("/") || file : parts.slice(up, up + 2).join("/");
|
|
1456
|
+
}
|
|
1457
|
+
|
|
1458
|
+
/**
|
|
1459
|
+
* Whether a hook is running for the server environment.
|
|
1460
|
+
*
|
|
1461
|
+
* Both spellings, for the reason `transform` above checks both: Vite 6 moved
|
|
1462
|
+
* the answer onto the plugin context and the `ssr` option is the older one.
|
|
1463
|
+
*/
|
|
1464
|
+
function isSsr(context, options) {
|
|
1465
|
+
return options?.ssr === true || context?.environment?.name === "ssr";
|
|
1466
|
+
}
|
|
1467
|
+
|
|
1468
|
+
function cleanId(id) {
|
|
1469
|
+
const at = id.indexOf("?");
|
|
1470
|
+
return at === -1 ? id : id.slice(0, at);
|
|
371
1471
|
}
|