-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.ts
528 lines (489 loc) · 16.1 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
import path from "node:path";
import { fileURLToPath } from "node:url";
import { createDebug, tinyassert } from "@hiogawa/utils";
import {
type ConfigEnv,
type InlineConfig,
type Plugin,
type PluginOption,
type ResolvedConfig,
type ViteDevServer,
build,
createLogger,
createServer,
mergeConfig,
} from "vite";
import { crawlFrameworkPkgs } from "vitefu";
import { CSS_LANGS_RE } from "../features/assets/css";
import {
serverAssertsPluginServer,
vitePluginServerAssets,
} from "../features/assets/plugin";
import { SERVER_CSS_PROXY } from "../features/assets/shared";
import {
vitePluginClientUseClient,
vitePluginServerUseClient,
} from "../features/client-component/plugin";
import {
OUTPUT_SERVER_JS_EXT,
createServerPackageJson,
} from "../features/next/plugin";
import {
type PrerenderFn,
type PrerenderManifest,
prerenderPlugin,
} from "../features/prerender/plugin";
import type { RouteManifest } from "../features/router/manifest";
import {
routeManifestPluginClient,
routeManifestPluginServer,
} from "../features/router/plugin";
import {
vitePluginClientUseServer,
vitePluginServerUseServer,
} from "../features/server-action/plugin";
import { $__global } from "../global";
import {
ENTRY_BROWSER_WRAPPER,
ENTRY_SERVER_WRAPPER,
createVirtualPlugin,
hashString,
vitePluginSilenceDirectiveBuildWarning,
} from "./utils";
const debug = createDebug("react-server:plugin");
// resolve import paths for `createClientReference`, `createServerReference`, etc...
// since `import "@hiogawa/react-server"` is not always visible for exernal library.
const RUNTIME_BROWSER_PATH = fileURLToPath(
new URL("../runtime/browser.js", import.meta.url),
);
const RUNTIME_SSR_PATH = fileURLToPath(
new URL("../runtime/ssr.js", import.meta.url),
);
const RUNTIME_SERVER_PATH = fileURLToPath(
new URL("../runtime/server.js", import.meta.url),
);
export type { PrerenderManifest };
// convenient singleton to share states
export type { PluginStateManager };
class PluginStateManager {
server?: ViteDevServer;
config!: ResolvedConfig;
configEnv!: ConfigEnv;
buildType?: "scan" | "server" | "browser" | "ssr";
routeToClientReferences: Record<string, string[]> = {};
routeManifest?: RouteManifest;
serverAssets: string[] = [];
// expose "use client" node modules to client via virtual modules
// to avoid dual package due to deps optimization hash during dev
nodeModules = {
useClient: new Map<string, { id: string; exportNames: Set<string> }>(),
};
// all files in parent server
parentIds = new Set<string>();
// all files in rsc server
serverIds = new Set<string>();
// "use client" files
clientReferenceMap = new Map<string, string>();
// "use server" files
serverReferenceMap = new Map<string, string>();
shouldReloadRsc(id: string) {
const ok = this.serverIds.has(id) && !this.clientReferenceMap.has(id);
debug("[RscManager.shouldReloadRsc]", { ok, id });
return ok;
}
normalizeReferenceId(id: string) {
id = path.relative(this.config.root, id);
return this.buildType ? hashString(id) : id;
}
}
// persist singleton during build
if (!process.argv.includes("build")) {
delete (globalThis as any).__VITE_REACT_SERVER_MANAGER;
}
const manager: PluginStateManager = ((
globalThis as any
).__VITE_REACT_SERVER_MANAGER ??= new PluginStateManager());
export type ReactServerPluginOptions = {
plugins?: PluginOption[];
prerender?: PrerenderFn;
entryBrowser?: string;
entryServer?: string;
routeDir?: string;
noAsyncLocalStorage?: boolean;
};
export function vitePluginReactServer(
options?: ReactServerPluginOptions,
): Plugin[] {
const entryBrowser =
options?.entryBrowser ?? "@hiogawa/react-server/entry/browser";
const entryServer =
options?.entryServer ?? "@hiogawa/react-server/entry/server";
const routeDir = options?.routeDir ?? "src/routes";
const reactServerViteConfig: InlineConfig = {
customLogger: createLogger(undefined, {
prefix: "[react-server]",
allowClearScreen: false,
}),
clearScreen: false,
configFile: false,
cacheDir: "./node_modules/.vite-rsc",
optimizeDeps: {
noDiscovery: true,
include: [],
},
plugins: [
...(options?.plugins ?? []),
vitePluginSilenceDirectiveBuildWarning(),
// expose server reference to react-server itself
vitePluginServerUseServer({
manager,
runtimePath: RUNTIME_SERVER_PATH,
}),
// transform "use client" into client referecnes
vitePluginServerUseClient({
manager,
runtimePath: RUNTIME_SERVER_PATH,
}),
routeManifestPluginServer({ manager, routeDir }),
createVirtualPlugin("server-routes", () => {
return `
const glob = import.meta.glob(
"/${routeDir}/**/(page|layout|error|not-found|loading|template|route).(js|jsx|ts|tsx)",
{ eager: true },
);
export default Object.fromEntries(
Object.entries(glob).map(
([k, v]) => [k.slice("/${routeDir}".length), v]
)
);
const globMiddleware = import.meta.glob("/middleware.(js|jsx|ts|tsx)", { eager: true });
export const middleware = Object.values(globMiddleware)[0];
`;
}),
createVirtualPlugin(
ENTRY_SERVER_WRAPPER.slice("virtual:".length),
() => `
import "virtual:inject-async-local-storage";
export { handler } from "${entryServer}";
export { router } from "@hiogawa/react-server/entry/server";
`,
),
// make `AsyncLocalStorage` available globally for React.cache from edge build
// https://github.com/facebook/react/blob/f14d7f0d2597ea25da12bcf97772e8803f2a394c/packages/react-server/src/forks/ReactFlightServerConfig.dom-edge.js#L16-L19
createVirtualPlugin("inject-async-local-storage", () => {
if (options?.noAsyncLocalStorage) {
return "export {}";
}
return `
import { AsyncLocalStorage } from "node:async_hooks";
Object.assign(globalThis, { AsyncLocalStorage });
`;
}),
validateImportPlugin({
"client-only": `'client-only' is included in server build`,
"server-only": true,
}),
serverAssertsPluginServer({ manager }),
serverDepsConfigPlugin(),
{
name: "patch-react-server-dom-webpack",
transform(code, id, _options) {
if (id.includes("react-server-dom-webpack")) {
// rename webpack markers in react server runtime
// to avoid conflict with ssr runtime which shares same globals
code = code.replaceAll(
"__webpack_require__",
"__vite_react_server_webpack_require__",
);
code = code.replaceAll(
"__webpack_chunk_load__",
"__vite_react_server_webpack_chunk_load__",
);
// make server reference async for simplicity (stale chunkCache, etc...)
// see TODO in https://github.com/facebook/react/blob/33a32441e991e126e5e874f831bd3afc237a3ecf/packages/react-server-dom-webpack/src/ReactFlightClientConfigBundlerWebpack.js#L131-L132
code = code.replaceAll("if (isAsyncImport(metadata))", "if (true)");
code = code.replaceAll("4 === metadata.length", "true");
return code;
}
return;
},
},
],
build: {
ssr: true,
manifest: true,
ssrEmitAssets: true,
outDir: "dist/rsc",
rollupOptions: {
input: {
index: ENTRY_SERVER_WRAPPER,
},
output: OUTPUT_SERVER_JS_EXT,
},
},
};
const rscParentPlugin: Plugin = {
name: vitePluginReactServer.name,
config(_config, env) {
manager.configEnv = env;
return {
optimizeDeps: {
// this can potentially include unnecessary server only deps for client,
// but there should be no issues except making deps optimization slightly slower.
entries: [
path.posix.join(
routeDir,
`**/(page|layout|error|not-found|loading|template).(js|jsx|ts|tsx)`,
),
],
exclude: ["@hiogawa/react-server"],
include: [
"react",
"react/jsx-runtime",
"react/jsx-dev-runtime",
"react-dom",
"react-dom/client",
"react-server-dom-webpack/client.browser",
],
},
ssr: {
noExternal: ["@hiogawa/react-server"],
optimizeDeps: {
exclude: ["@hiogawa/react-server"],
},
},
build: {
manifest: true,
outDir: env.isSsrBuild ? "dist/server" : "dist/client",
rollupOptions: env.isSsrBuild
? {
input: options?.prerender
? {
__entry_ssr: "@hiogawa/react-server/entry/ssr",
}
: undefined,
output: OUTPUT_SERVER_JS_EXT,
}
: {
input: ENTRY_BROWSER_WRAPPER,
},
},
};
},
configResolved(config) {
manager.config = config;
},
async configureServer(server) {
manager.server = server;
},
async buildStart(_options) {
if (manager.configEnv.command === "serve") {
tinyassert(manager.server);
const reactServer = await createServer(reactServerViteConfig);
reactServer.pluginContainer.buildStart({});
$__global.dev = {
server: manager.server,
reactServer: reactServer,
manager,
};
}
},
async buildEnd(_options) {
if (manager.configEnv.command === "serve") {
await $__global.dev.reactServer.close();
delete ($__global as any).dev;
}
},
transform(_code, id, _options) {
if (!id.includes("/node_modules/")) {
manager.parentIds.add(id);
}
},
async handleHotUpdate(ctx) {
tinyassert(manager.server);
// re-render RSC with custom event
if (ctx.modules.every((m) => m.id && manager.shouldReloadRsc(m.id))) {
manager.server.hot.send({
type: "custom",
event: "rsc:update",
data: {
file: ctx.file,
},
});
// Some rsc files are included in parent module graph
// due to postcss creating dependency from style.css to all source files.
// In this case, reload all importers (for css hmr),
// and return empty modules to avoid full-reload
if (ctx.modules.every((m) => m.id && !manager.parentIds.has(m.id))) {
for (const m of ctx.modules) {
for (const imod of m.importers) {
await manager.server.reloadModule(imod);
}
}
return [];
}
}
// css module is not self-accepting, so we filter out
// `?direct` module (used for SSR CSS) to avoid browser full reload.
// (see packages/react-server/src/features/assets/css.ts)
if (CSS_LANGS_RE.test(ctx.file)) {
return ctx.modules.filter((m) => !m.id?.includes("?direct"));
}
return;
},
};
// orchestrate four builds from a single vite (browser) build
const buildOrchestrationPlugin: Plugin = {
name: vitePluginReactServer.name + ":build",
apply: "build",
async buildStart(_options) {
if (!manager.buildType) {
await createServerPackageJson();
console.log("▶▶▶ REACT SERVER BUILD (scan) [1/4]");
manager.buildType = "scan";
await build(
mergeConfig(reactServerViteConfig, {
build: { write: false },
} satisfies InlineConfig),
);
console.log("▶▶▶ REACT SERVER BUILD (server) [2/4]");
manager.buildType = "server";
manager.clientReferenceMap.clear();
await build(reactServerViteConfig);
console.log("▶▶▶ REACT SERVER BUILD (browser) [3/4]");
manager.buildType = "browser";
}
},
writeBundle: {
order: "post",
sequential: true,
async handler(_options, _bundle) {
if (manager.buildType === "browser") {
console.log("▶▶▶ REACT SERVER BUILD (ssr) [4/4]");
manager.buildType = "ssr";
await build({
build: {
ssr: true,
},
});
}
},
},
};
// plugins for main vite dev server (browser / ssr)
return [
rscParentPlugin,
buildOrchestrationPlugin,
vitePluginSilenceDirectiveBuildWarning(),
vitePluginClientUseServer({
manager,
runtimePath: RUNTIME_BROWSER_PATH,
ssrRuntimePath: RUNTIME_SSR_PATH,
}),
...vitePluginClientUseClient({ manager }),
...vitePluginServerAssets({ manager, entryBrowser, entryServer }),
...routeManifestPluginClient({ manager }),
...(options?.prerender
? prerenderPlugin({ manager, prerender: options.prerender })
: []),
validateImportPlugin({
"client-only": true,
"server-only": `'server-only' is included in client build`,
}),
createVirtualPlugin(ENTRY_BROWSER_WRAPPER.slice("virtual:".length), () => {
// dev
if (!manager.buildType) {
// wrapper entry to ensure client entry runs after vite/react inititialization
return /* js */ `
import "${SERVER_CSS_PROXY}";
for (let i = 0; !window.$RefreshReg$; i++) {
await new Promise(resolve => setTimeout(resolve, 10 * (2 ** i)));
}
await import("${entryBrowser}");
`;
}
// build
if (manager.buildType === "browser") {
// import "runtime/client" for preload
return /* js */ `
import "${SERVER_CSS_PROXY}";
import("@hiogawa/react-server/runtime/client");
import "${entryBrowser}";
`;
}
tinyassert(false);
}),
];
}
// https://github.com/vercel/next.js/blob/90f564d376153fe0b5808eab7b83665ee5e08aaf/packages/next/src/build/webpack-config.ts#L1249-L1280
// https://github.com/pcattori/vite-env-only/blob/68a0cc8546b9a37c181c0b0a025eb9b62dbedd09/src/deny-imports.ts
// https://github.com/sveltejs/kit/blob/84298477a014ec471839adf7a4448d91bc7949e4/packages/kit/src/exports/vite/index.js#L513
function validateImportPlugin(entries: Record<string, string | true>): Plugin {
return {
name: validateImportPlugin.name,
enforce: "pre",
resolveId(source, importer, options) {
const entry = entries[source];
if (entry) {
// skip validation during optimizeDeps scan since for now
// we want to allow going through server/client boundary loosely
if (
entry === true ||
manager.buildType === "scan" ||
("scan" in options && options.scan)
) {
return "\0virtual:validate-import";
}
throw new Error(entry + ` (importer: ${importer ?? "unknown"})`);
}
return;
},
load(id, _options) {
if (id === "\0virtual:validate-import") {
return "export {}";
}
return;
},
};
}
function serverDepsConfigPlugin(): Plugin {
return {
name: serverDepsConfigPlugin.name,
async config(_config, env) {
// crawl packages with "react" or "next" in "peerDependencies"
// see https://github.com/svitejs/vitefu/blob/d8d82fa121e3b2215ba437107093c77bde51b63b/src/index.js#L95-L101
const result = await crawlFrameworkPkgs({
root: process.cwd(),
isBuild: env.command === "build",
isFrameworkPkgByJson(pkgJson) {
const deps = pkgJson["peerDependencies"];
return deps && ("react" in deps || "next" in deps);
},
});
return {
ssr: {
resolve: {
conditions: ["react-server"],
},
noExternal: [
"react",
"react-dom",
"react-server-dom-webpack",
"server-only",
"client-only",
...result.ssr.noExternal,
],
// pre-bundle cjs deps
optimizeDeps: {
include: [
"react",
"react/jsx-runtime",
"react/jsx-dev-runtime",
"react-server-dom-webpack/server.edge",
],
},
},
};
},
};
}