@farm.js/plugin 0.1.61 → 0.1.62
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/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/middleware/index.d.ts +8 -1
- package/dist/middleware/index.d.ts.map +1 -1
- package/dist/middleware/index.js +16 -10
- package/dist/middleware/matching.d.ts +4 -2
- package/dist/middleware/matching.d.ts.map +1 -1
- package/dist/middleware/matching.js +7 -47
- package/dist/rsc/client-sourcemaps.d.ts +13 -0
- package/dist/rsc/client-sourcemaps.d.ts.map +1 -0
- package/dist/rsc/client-sourcemaps.js +52 -0
- package/dist/rsc/document-layouts.d.ts +16 -0
- package/dist/rsc/document-layouts.d.ts.map +1 -0
- package/dist/rsc/document-layouts.js +82 -0
- package/dist/rsc/entries/client.d.ts.map +1 -1
- package/dist/rsc/entries/client.js +92 -16
- package/dist/rsc/entries/rsc.d.ts.map +1 -1
- package/dist/rsc/entries/rsc.js +563 -45
- package/dist/rsc/entries/ssr.d.ts.map +1 -1
- package/dist/rsc/entries/ssr.js +7 -6
- package/dist/rsc/index.d.ts +38 -3
- package/dist/rsc/index.d.ts.map +1 -1
- package/dist/rsc/index.js +282 -292
- package/dist/rsc/middleware-config-module.d.ts +3 -0
- package/dist/rsc/middleware-config-module.d.ts.map +1 -1
- package/dist/rsc/middleware-config-module.js +41 -28
- package/dist/rsc/mobile-client.d.ts +67 -0
- package/dist/rsc/mobile-client.d.ts.map +1 -0
- package/dist/rsc/mobile-client.js +148 -0
- package/dist/rsc/nitro-build.d.ts.map +1 -1
- package/dist/rsc/nitro-build.js +268 -81
- package/dist/rsc/server-fn-transform.d.ts +1 -5
- package/dist/rsc/server-fn-transform.d.ts.map +1 -1
- package/dist/rsc/server-fn-transform.js +3 -338
- package/dist/rsc/types.d.ts +39 -0
- package/dist/rsc/types.d.ts.map +1 -1
- package/dist/rsc/vite-plugin-nitro.d.ts +2 -0
- package/dist/rsc/vite-plugin-nitro.d.ts.map +1 -1
- package/dist/rsc/vite-plugin-nitro.js +12 -2
- package/package.json +3 -3
package/dist/rsc/nitro-build.js
CHANGED
|
@@ -5,8 +5,10 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import path from "path";
|
|
7
7
|
import { createRequire } from "node:module";
|
|
8
|
-
import { writeFileSync, readFileSync, mkdirSync, copyFileSync, existsSync, readdirSync, statSync, } from "fs";
|
|
8
|
+
import { writeFileSync, readFileSync, mkdirSync, copyFileSync, existsSync, readdirSync, lstatSync, rmSync, statSync, symlinkSync, } from "fs";
|
|
9
|
+
import { FARM_NODE_ENV_MODULE_FILE, FARM_NODE_ENV_MODULE_SOURCE, } from "@farm.js/core/internal/production-node-env";
|
|
9
10
|
import { resolveRscBuildOutputPath } from "./build-paths.js";
|
|
11
|
+
import { finalizeFarmRscMobileClient, patchFarmRscMobileShellClient } from "./mobile-client.js";
|
|
10
12
|
const MANIFEST_FILENAME = "__vite_rsc_assets_manifest.js";
|
|
11
13
|
const MIN_MANIFEST_BYTES = 100; // real manifest is ~500+, stub is 64
|
|
12
14
|
const POLL_INTERVAL_MS = 50;
|
|
@@ -62,9 +64,16 @@ export function rewriteRscEntryImports(serverDir, serverDistDir) {
|
|
|
62
64
|
const relative = path.relative(path.dirname(entryChunkPath), target).replace(/\\/g, "/");
|
|
63
65
|
return relative.startsWith(".") ? relative : `./${relative}`;
|
|
64
66
|
};
|
|
65
|
-
// Match any path that ends with dist/rsc/index.js
|
|
66
|
-
|
|
67
|
-
|
|
67
|
+
// Match any path that ends with dist/rsc/index.js, dist/ssr/index.js or the NODE_ENV module
|
|
68
|
+
// (minified, no spaces)
|
|
69
|
+
for (const [environment, file] of [
|
|
70
|
+
["rsc", "index.js"],
|
|
71
|
+
["rsc", FARM_NODE_ENV_MODULE_FILE],
|
|
72
|
+
["ssr", "index.js"],
|
|
73
|
+
]) {
|
|
74
|
+
const pattern = new RegExp(`(["'\`])[^"'\`]*?dist/${environment}/${file.replace(/\./g, "\\.")}\\1`, "g");
|
|
75
|
+
code = code.replace(pattern, JSON.stringify(relativeImport(path.join(serverDistDir, environment, file))));
|
|
76
|
+
}
|
|
68
77
|
writeFileSync(entryChunkPath, code, "utf-8");
|
|
69
78
|
}
|
|
70
79
|
}
|
|
@@ -98,6 +107,36 @@ function detectBundledRscRuntimes(rendererPath) {
|
|
|
98
107
|
}
|
|
99
108
|
return packages;
|
|
100
109
|
}
|
|
110
|
+
/**
|
|
111
|
+
* The prerender server runs the Vite output in place, before the runtime packages are copied next
|
|
112
|
+
* to the server output, and a strict install (pnpm) does not expose a package the plugin depends
|
|
113
|
+
* on from the app root. Links them where the Vite output resolves from; returns the cleanup.
|
|
114
|
+
*/
|
|
115
|
+
function linkRuntimePackages(viteDistDir, packages) {
|
|
116
|
+
const nodeModules = path.join(viteDistDir, "node_modules");
|
|
117
|
+
const ownsNodeModules = !existsSync(nodeModules);
|
|
118
|
+
const links = [];
|
|
119
|
+
for (const [packageName, packageRoot] of packages) {
|
|
120
|
+
const link = path.join(nodeModules, ...packageName.split("/"));
|
|
121
|
+
try {
|
|
122
|
+
lstatSync(link);
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
catch { }
|
|
126
|
+
mkdirSync(path.dirname(link), { recursive: true });
|
|
127
|
+
// A junction needs no privileges on Windows; elsewhere the type is ignored.
|
|
128
|
+
symlinkSync(packageRoot, link, "junction");
|
|
129
|
+
links.push(link);
|
|
130
|
+
}
|
|
131
|
+
return () => {
|
|
132
|
+
// Removes the links, never what they point at.
|
|
133
|
+
if (ownsNodeModules)
|
|
134
|
+
rmSync(nodeModules, { recursive: true, force: true });
|
|
135
|
+
else
|
|
136
|
+
for (const link of links)
|
|
137
|
+
rmSync(link, { force: true });
|
|
138
|
+
};
|
|
139
|
+
}
|
|
101
140
|
function copyRuntimePackages(serverDir, packages) {
|
|
102
141
|
if (packages.size === 0)
|
|
103
142
|
return;
|
|
@@ -207,7 +246,13 @@ function writeRscEntryFile(buildDir, rendererPath, ssrPath) {
|
|
|
207
246
|
const setSsLoader = relSsr
|
|
208
247
|
? `globalThis.__VITE_RSC_LOAD_SSR__ = () => import("./${relSsr}");`
|
|
209
248
|
: "";
|
|
249
|
+
// Imported first and kept external like the bundles, so it runs before React picks its
|
|
250
|
+
// development or production build from NODE_ENV. Inlined, it would run after them.
|
|
251
|
+
const relNodeEnv = path
|
|
252
|
+
.relative(buildDir, path.join(path.dirname(resolvedRenderer), FARM_NODE_ENV_MODULE_FILE))
|
|
253
|
+
.replace(/\\/g, "/");
|
|
210
254
|
const code = `
|
|
255
|
+
import "./${relNodeEnv}";
|
|
211
256
|
${setSsLoader}
|
|
212
257
|
import * as entryExports from "./${relRenderer}";
|
|
213
258
|
import { defineEventHandler } from "h3";
|
|
@@ -282,13 +327,208 @@ export default defineEventHandler(async (event) => {
|
|
|
282
327
|
writeFileSync(entryPath, code, "utf-8");
|
|
283
328
|
return entryPath;
|
|
284
329
|
}
|
|
330
|
+
function listModuleFiles(dir) {
|
|
331
|
+
if (!existsSync(dir))
|
|
332
|
+
return [];
|
|
333
|
+
const files = [];
|
|
334
|
+
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
335
|
+
const full = path.join(dir, entry.name);
|
|
336
|
+
if (entry.isDirectory())
|
|
337
|
+
files.push(...listModuleFiles(full));
|
|
338
|
+
else if (/\.(?:m|c)?js$/.test(entry.name))
|
|
339
|
+
files.push(full);
|
|
340
|
+
}
|
|
341
|
+
return files;
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* Copy the packages the RSC and SSR bundles load into the server output. Nitro traces only its own
|
|
345
|
+
* bundle, and these are copied in afterwards; their code-split chunks and dynamic imports (such as
|
|
346
|
+
* shiki's WebAssembly engine) reach package files the entry files alone do not. The RSC bundle
|
|
347
|
+
* resolves packages with the react-server condition, the SSR bundle without it.
|
|
348
|
+
*/
|
|
349
|
+
async function traceRscBundleDependencies(root, serverDir, rscDir, ssrDir) {
|
|
350
|
+
const { traceNodeModules } = await import("nf3");
|
|
351
|
+
const traces = [
|
|
352
|
+
[listModuleFiles(rscDir), ["react-server", "node", "import", "default"]],
|
|
353
|
+
[ssrDir ? listModuleFiles(ssrDir) : [], ["node", "import", "default"]],
|
|
354
|
+
];
|
|
355
|
+
for (const [files, conditions] of traces) {
|
|
356
|
+
if (files.length === 0)
|
|
357
|
+
continue;
|
|
358
|
+
await traceNodeModules(files, { rootDir: root, outDir: serverDir, conditions });
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* Give the SSR bundle the production client CSS href and bootstrap script, so its HTML has styles
|
|
363
|
+
* and hydrates. Runs on the Vite output before Nitro, so the prerender pass and the copies in the
|
|
364
|
+
* server output render the same document. Only placeholders are replaced, so it is idempotent.
|
|
365
|
+
*/
|
|
366
|
+
function patchRscServerBundles(root, rscDir, ssrDir, publicDir, assetsDir) {
|
|
367
|
+
// Patch SSR bundle: inject production client CSS href and bootstrap script so HTML has styles and hydration works
|
|
368
|
+
const ssrIndexPath = path.join(ssrDir, "index.js");
|
|
369
|
+
const manifestPath = path.join(rscDir, MANIFEST_FILENAME);
|
|
370
|
+
let bootstrapScriptContent = "";
|
|
371
|
+
let clientEntryHref = "";
|
|
372
|
+
const RSC_CALL_SERVER_PLACEHOLDER = "(function(){if(typeof globalThis.__viteRscCallServer!=='function'){globalThis.__viteRscCallServer=function(){return Promise.reject(new Error(\"Farm.js: server actions not ready\"));}}})();\n";
|
|
373
|
+
if (existsSync(manifestPath)) {
|
|
374
|
+
try {
|
|
375
|
+
const manifestContent = readFileSync(manifestPath, "utf-8");
|
|
376
|
+
const start = manifestContent.indexOf("{");
|
|
377
|
+
const end = manifestContent.lastIndexOf("}") + 1;
|
|
378
|
+
if (start >= 0 && end > start) {
|
|
379
|
+
const manifest = JSON.parse(manifestContent.slice(start, end));
|
|
380
|
+
bootstrapScriptContent = manifest.bootstrapScriptContent || "";
|
|
381
|
+
const importMatch = bootstrapScriptContent.match(/^import\s*\(\s*["']([^"']+)["']\s*\)\s*$/);
|
|
382
|
+
if (importMatch)
|
|
383
|
+
clientEntryHref = importMatch[1];
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
catch {
|
|
387
|
+
// ignore
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
const fullBootstrap = bootstrapScriptContent
|
|
391
|
+
? RSC_CALL_SERVER_PLACEHOLDER + bootstrapScriptContent
|
|
392
|
+
: "";
|
|
393
|
+
// Patch manifest so production SSR gets bootstrap with __viteRscCallServer placeholder (SSR reads assetsManifest.bootstrapScriptContent)
|
|
394
|
+
if (fullBootstrap && existsSync(manifestPath)) {
|
|
395
|
+
try {
|
|
396
|
+
let manifestCode = readFileSync(manifestPath, "utf-8");
|
|
397
|
+
const escaped = JSON.stringify(bootstrapScriptContent);
|
|
398
|
+
const escapedFull = JSON.stringify(fullBootstrap);
|
|
399
|
+
if (manifestCode.includes(escaped) && escaped !== escapedFull) {
|
|
400
|
+
manifestCode = manifestCode.replace(escaped, escapedFull);
|
|
401
|
+
writeFileSync(manifestPath, manifestCode, "utf-8");
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
catch {
|
|
405
|
+
// ignore
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
const assetsDirName = assetsDir ?? "assets";
|
|
409
|
+
const assetsRoot = path.join(publicDir, assetsDirName);
|
|
410
|
+
let clientCssHref = "";
|
|
411
|
+
if (existsSync(assetsRoot)) {
|
|
412
|
+
const files = readdirSync(assetsRoot);
|
|
413
|
+
const cssFile = files.find((f) => f.endsWith(".css"));
|
|
414
|
+
if (cssFile)
|
|
415
|
+
clientCssHref = `/${assetsDirName}/${cssFile}`;
|
|
416
|
+
}
|
|
417
|
+
// An app with a `mobile` block: the server learns the mobile entry its shell documents load.
|
|
418
|
+
// Only a build with the mobile entry has the placeholder, so other apps are left as they are.
|
|
419
|
+
patchFarmRscMobileShellClient(rscDir, () => finalizeFarmRscMobileClient({
|
|
420
|
+
root,
|
|
421
|
+
clientDir: publicDir,
|
|
422
|
+
webEntry: clientEntryHref,
|
|
423
|
+
stylesheet: clientCssHref,
|
|
424
|
+
}));
|
|
425
|
+
if (existsSync(ssrIndexPath)) {
|
|
426
|
+
try {
|
|
427
|
+
let ssrCode = readFileSync(ssrIndexPath, "utf-8");
|
|
428
|
+
// Only the assignment: the bundle also compares against the placeholder, and a second
|
|
429
|
+
// run over an already patched bundle must leave that comparison intact.
|
|
430
|
+
ssrCode = ssrCode.replace(/(CLIENT_CSS_HREF\s*=\s*)"__FARM_CLIENT_CSS_HREF__"/, "$1" + JSON.stringify(clientCssHref));
|
|
431
|
+
if (clientEntryHref && ssrCode.includes("__FARM_CLIENT_ENTRY_HREF__")) {
|
|
432
|
+
ssrCode = ssrCode.replace(/(CLIENT_ENTRY_HREF\s*=\s*)"__FARM_CLIENT_ENTRY_HREF__"/, "$1" + JSON.stringify(clientEntryHref));
|
|
433
|
+
}
|
|
434
|
+
if (fullBootstrap && ssrCode.includes("__FARM_BOOTSTRAP_SCRIPT__")) {
|
|
435
|
+
ssrCode = ssrCode.replace(/=\s*["']__FARM_BOOTSTRAP_SCRIPT__["']/, "= " + JSON.stringify(fullBootstrap));
|
|
436
|
+
}
|
|
437
|
+
writeFileSync(ssrIndexPath, ssrCode, "utf-8");
|
|
438
|
+
}
|
|
439
|
+
catch {
|
|
440
|
+
// ignore
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
/** The route the RSC entry answers, during the prerender pass only, with its static pages. */
|
|
445
|
+
const FARM_PRERENDER_ROUTES_PATH = "/__farm/rsc-prerender-routes";
|
|
446
|
+
/**
|
|
447
|
+
* Prerender the app's static pages and their RSC payloads into the public output with Nitro's
|
|
448
|
+
* prerenderer, which serves them before the server on every preset. The RSC entry lists the pages
|
|
449
|
+
* (it has the route modules and their generateStaticParams), through Nitro's x-nitro-prerender
|
|
450
|
+
* header on a seed route that is removed afterwards.
|
|
451
|
+
*/
|
|
452
|
+
async function prerenderRscPages(nitro, prerender, viteDistDir) {
|
|
453
|
+
nitro.options.prerender = {
|
|
454
|
+
...nitro.options.prerender,
|
|
455
|
+
routes: [FARM_PRERENDER_ROUTES_PATH],
|
|
456
|
+
crawlLinks: false,
|
|
457
|
+
// Failures are judged below: a seed that cannot run only skips prerendering.
|
|
458
|
+
failOnError: false,
|
|
459
|
+
};
|
|
460
|
+
// The prerender server is a separate Nitro build of the same renderer entry. Point its imports of
|
|
461
|
+
// the prebuilt bundles at the Vite output, as the server output's are pointed at their copies.
|
|
462
|
+
nitro.hooks.hook("prerender:init", (prerenderer) => {
|
|
463
|
+
prerenderer.hooks.hook("compiled", () => {
|
|
464
|
+
rewriteRscEntryImports(prerenderer.options.output.serverDir, viteDistDir);
|
|
465
|
+
});
|
|
466
|
+
});
|
|
467
|
+
let seedError;
|
|
468
|
+
const failedPages = [];
|
|
469
|
+
nitro.hooks.hook("prerender:route", (route) => {
|
|
470
|
+
if (route.route !== FARM_PRERENDER_ROUTES_PATH) {
|
|
471
|
+
if (route.error)
|
|
472
|
+
failedPages.push(`${route.route} (${route.error.message})`);
|
|
473
|
+
return;
|
|
474
|
+
}
|
|
475
|
+
seedError = route.error;
|
|
476
|
+
removeSeedOutput(nitro, route.fileName);
|
|
477
|
+
});
|
|
478
|
+
const previous = process.env.FARM_RSC_PRERENDER;
|
|
479
|
+
process.env.FARM_RSC_PRERENDER = "1";
|
|
480
|
+
try {
|
|
481
|
+
await prerender(nitro);
|
|
482
|
+
}
|
|
483
|
+
finally {
|
|
484
|
+
if (previous === undefined)
|
|
485
|
+
delete process.env.FARM_RSC_PRERENDER;
|
|
486
|
+
else
|
|
487
|
+
process.env.FARM_RSC_PRERENDER = previous;
|
|
488
|
+
}
|
|
489
|
+
if (seedError) {
|
|
490
|
+
// The server could not start in place (for example, a runtime package only the server output
|
|
491
|
+
// carries). Every page still renders on request, so the build goes on without static files.
|
|
492
|
+
console.warn(`[FARM] Prerendering skipped: the server could not list its static pages (${seedError.message}). Pages render on each request.`);
|
|
493
|
+
return;
|
|
494
|
+
}
|
|
495
|
+
if (failedPages.length > 0) {
|
|
496
|
+
// A page that asks to be static and fails to render fails the build, as in Next.js.
|
|
497
|
+
throw new Error(`[FARM] Prerendering failed for:\n ${failedPages.join("\n ")}`);
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
/** Remove the seed route's file, and the directories it created, from the public output. */
|
|
501
|
+
function removeSeedOutput(nitro, fileName) {
|
|
502
|
+
const publicDir = path.resolve(nitro.options.output.publicDir);
|
|
503
|
+
const prerendered = nitro
|
|
504
|
+
._prerenderedRoutes;
|
|
505
|
+
const index = prerendered?.findIndex((entry) => entry.route === FARM_PRERENDER_ROUTES_PATH) ?? -1;
|
|
506
|
+
if (index >= 0)
|
|
507
|
+
prerendered.splice(index, 1);
|
|
508
|
+
if (!fileName)
|
|
509
|
+
return;
|
|
510
|
+
let target = path.join(publicDir, fileName);
|
|
511
|
+
rmSync(target, { force: true });
|
|
512
|
+
for (target = path.dirname(target); target.startsWith(publicDir + path.sep) &&
|
|
513
|
+
existsSync(target) &&
|
|
514
|
+
readdirSync(target).length === 0; target = path.dirname(target)) {
|
|
515
|
+
rmSync(target, { recursive: true });
|
|
516
|
+
}
|
|
517
|
+
}
|
|
285
518
|
export async function buildRscNitro(options) {
|
|
286
|
-
const { rendererPath, publicDir, ssrPath, assetsDir, root,
|
|
287
|
-
const
|
|
519
|
+
const { rendererPath, publicDir, ssrPath, assetsDir, root, preset = "vercel" } = options;
|
|
520
|
+
const outputDir = options.outputDir ?? path.join(root, ".output");
|
|
521
|
+
const { build, copyPublicAssets, createNitro, prepare, prerender } = await import("nitro/builder");
|
|
288
522
|
console.log(`[FARM] Building RSC server with Nitro (preset: ${preset})...`);
|
|
289
523
|
const buildDir = path.join(root, ".nitro");
|
|
290
|
-
|
|
291
|
-
|
|
524
|
+
// Vercel's Build Output API has its own layout (functions/, static/), which the preset owns when
|
|
525
|
+
// the caller names the deployment directory (`farm build`). Without one, the output keeps the
|
|
526
|
+
// server/ and public/ layout that hand-written post-build scripts copy from.
|
|
527
|
+
const presetOwnsLayout = options.outputDir !== undefined && (preset === "vercel" || preset === "vercel-edge");
|
|
528
|
+
// The server sets NODE_ENV to "production" unless the deployment chose one, as the core
|
|
529
|
+
// node-server output does. The RSC and SSR bundles read it at runtime: left unset, React loads
|
|
530
|
+
// its development builds and Flight responses carry server component props, stacks and paths.
|
|
531
|
+
writeFileSync(path.join(path.dirname(path.resolve(rendererPath)), FARM_NODE_ENV_MODULE_FILE), FARM_NODE_ENV_MODULE_SOURCE, "utf-8");
|
|
292
532
|
const entryPath = writeRscEntryFile(buildDir, rendererPath, ssrPath);
|
|
293
533
|
const registeredRuntimePackages = new Map(runtimePackagesByRoot.get(path.resolve(root)) ?? []);
|
|
294
534
|
for (const [packageName, packageRoot] of detectBundledRscRuntimes(rendererPath)) {
|
|
@@ -317,11 +557,13 @@ export async function buildRscNitro(options) {
|
|
|
317
557
|
alias: {
|
|
318
558
|
h3: runtimeRequire.resolve("h3"),
|
|
319
559
|
},
|
|
320
|
-
output:
|
|
321
|
-
dir: outputDir
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
560
|
+
output: presetOwnsLayout
|
|
561
|
+
? { dir: outputDir }
|
|
562
|
+
: {
|
|
563
|
+
dir: outputDir,
|
|
564
|
+
serverDir: path.join(outputDir, "server"),
|
|
565
|
+
publicDir: path.join(outputDir, "public"),
|
|
566
|
+
},
|
|
325
567
|
publicAssets,
|
|
326
568
|
renderer: { handler: entryPath },
|
|
327
569
|
// The prebuilt RSC/SSR bundles are copied after Nitro finishes, so Nitro's
|
|
@@ -343,9 +585,21 @@ export async function buildRscNitro(options) {
|
|
|
343
585
|
compatibilityDate: "2024-12-01",
|
|
344
586
|
minify: true,
|
|
345
587
|
};
|
|
588
|
+
if (ssrPath) {
|
|
589
|
+
patchRscServerBundles(path.resolve(root), path.dirname(path.resolve(rendererPath)), path.dirname(path.resolve(ssrPath)), publicDir, assetsDir);
|
|
590
|
+
}
|
|
346
591
|
const nitro = await createNitro(config);
|
|
592
|
+
const serverDir = nitro.options.output.serverDir;
|
|
347
593
|
await prepare(nitro);
|
|
348
594
|
await copyPublicAssets(nitro);
|
|
595
|
+
const viteDistDir = path.dirname(path.dirname(path.resolve(rendererPath)));
|
|
596
|
+
const unlinkRuntimePackages = linkRuntimePackages(viteDistDir, registeredRuntimePackages);
|
|
597
|
+
try {
|
|
598
|
+
await prerenderRscPages(nitro, prerender, viteDistDir);
|
|
599
|
+
}
|
|
600
|
+
finally {
|
|
601
|
+
unlinkRuntimePackages();
|
|
602
|
+
}
|
|
349
603
|
await build(nitro);
|
|
350
604
|
await nitro.close();
|
|
351
605
|
copyRuntimePackages(serverDir, registeredRuntimePackages);
|
|
@@ -357,6 +611,7 @@ export async function buildRscNitro(options) {
|
|
|
357
611
|
copyDir(rscSrc, path.join(serverDistDir, "rsc"));
|
|
358
612
|
if (ssrSrc)
|
|
359
613
|
copyDir(ssrSrc, path.join(serverDistDir, "ssr"));
|
|
614
|
+
await traceRscBundleDependencies(root, serverDir, rscSrc, ssrSrc);
|
|
360
615
|
// Stub manifest if missing (RSC plugin may write it after our hook runs)
|
|
361
616
|
const rscDestDir = path.join(serverDistDir, "rsc");
|
|
362
617
|
const manifestDest = path.join(rscDestDir, "__vite_rsc_assets_manifest.js");
|
|
@@ -367,74 +622,6 @@ export async function buildRscNitro(options) {
|
|
|
367
622
|
// Nitro may group the entry differently by builder. Make copied-dist imports
|
|
368
623
|
// relative to the emitted chunk so output survives moving away from the project.
|
|
369
624
|
rewriteRscEntryImports(serverDir, serverDistDir);
|
|
370
|
-
// Patch SSR bundle: inject production client CSS href and bootstrap script so HTML has styles and hydration works
|
|
371
|
-
const ssrIndexPath = path.join(serverDistDir, "ssr", "index.js");
|
|
372
|
-
const manifestPath = path.join(serverDistDir, "rsc", MANIFEST_FILENAME);
|
|
373
|
-
let bootstrapScriptContent = "";
|
|
374
|
-
let clientEntryHref = "";
|
|
375
|
-
const RSC_CALL_SERVER_PLACEHOLDER = "(function(){if(typeof globalThis.__viteRscCallServer!=='function'){globalThis.__viteRscCallServer=function(){return Promise.reject(new Error(\"Farm.js: server actions not ready\"));}}})();\n";
|
|
376
|
-
if (existsSync(manifestPath)) {
|
|
377
|
-
try {
|
|
378
|
-
const manifestContent = readFileSync(manifestPath, "utf-8");
|
|
379
|
-
const start = manifestContent.indexOf("{");
|
|
380
|
-
const end = manifestContent.lastIndexOf("}") + 1;
|
|
381
|
-
if (start >= 0 && end > start) {
|
|
382
|
-
const manifest = JSON.parse(manifestContent.slice(start, end));
|
|
383
|
-
bootstrapScriptContent = manifest.bootstrapScriptContent || "";
|
|
384
|
-
const importMatch = bootstrapScriptContent.match(/^import\s*\(\s*["']([^"']+)["']\s*\)\s*$/);
|
|
385
|
-
if (importMatch)
|
|
386
|
-
clientEntryHref = importMatch[1];
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
catch {
|
|
390
|
-
// ignore
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
const fullBootstrap = bootstrapScriptContent
|
|
394
|
-
? RSC_CALL_SERVER_PLACEHOLDER + bootstrapScriptContent
|
|
395
|
-
: "";
|
|
396
|
-
// Patch manifest so production SSR gets bootstrap with __viteRscCallServer placeholder (SSR reads assetsManifest.bootstrapScriptContent)
|
|
397
|
-
if (fullBootstrap && existsSync(manifestPath)) {
|
|
398
|
-
try {
|
|
399
|
-
let manifestCode = readFileSync(manifestPath, "utf-8");
|
|
400
|
-
const escaped = JSON.stringify(bootstrapScriptContent);
|
|
401
|
-
const escapedFull = JSON.stringify(fullBootstrap);
|
|
402
|
-
if (manifestCode.includes(escaped) && escaped !== escapedFull) {
|
|
403
|
-
manifestCode = manifestCode.replace(escaped, escapedFull);
|
|
404
|
-
writeFileSync(manifestPath, manifestCode, "utf-8");
|
|
405
|
-
}
|
|
406
|
-
}
|
|
407
|
-
catch {
|
|
408
|
-
// ignore
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
if (existsSync(ssrIndexPath)) {
|
|
412
|
-
const assetsDirName = assetsDir ?? "assets";
|
|
413
|
-
const assetsRoot = path.join(publicDir, assetsDirName);
|
|
414
|
-
let clientCssHref = "";
|
|
415
|
-
if (existsSync(assetsRoot)) {
|
|
416
|
-
const files = readdirSync(assetsRoot);
|
|
417
|
-
const cssFile = files.find((f) => f.endsWith(".css"));
|
|
418
|
-
if (cssFile)
|
|
419
|
-
clientCssHref = `/${assetsDirName}/${cssFile}`;
|
|
420
|
-
}
|
|
421
|
-
try {
|
|
422
|
-
let ssrCode = readFileSync(ssrIndexPath, "utf-8");
|
|
423
|
-
if (ssrCode.includes("__FARM_CLIENT_CSS_HREF__")) {
|
|
424
|
-
ssrCode = ssrCode.replace("__FARM_CLIENT_CSS_HREF__", clientCssHref);
|
|
425
|
-
}
|
|
426
|
-
if (clientEntryHref && ssrCode.includes("__FARM_CLIENT_ENTRY_HREF__")) {
|
|
427
|
-
ssrCode = ssrCode.replace(/(CLIENT_ENTRY_HREF\s*=\s*)"__FARM_CLIENT_ENTRY_HREF__"/, "$1" + JSON.stringify(clientEntryHref));
|
|
428
|
-
}
|
|
429
|
-
if (fullBootstrap && ssrCode.includes("__FARM_BOOTSTRAP_SCRIPT__")) {
|
|
430
|
-
ssrCode = ssrCode.replace(/=\s*["']__FARM_BOOTSTRAP_SCRIPT__["']/, "= " + JSON.stringify(fullBootstrap));
|
|
431
|
-
}
|
|
432
|
-
writeFileSync(ssrIndexPath, ssrCode, "utf-8");
|
|
433
|
-
}
|
|
434
|
-
catch {
|
|
435
|
-
// ignore
|
|
436
|
-
}
|
|
437
|
-
}
|
|
438
625
|
console.log("[FARM] RSC Nitro build completed");
|
|
439
626
|
console.log(`[FARM] Output: ${outputDir}`);
|
|
440
627
|
}
|
|
@@ -1,6 +1,2 @@
|
|
|
1
|
-
export
|
|
2
|
-
code: string;
|
|
3
|
-
exports: string[];
|
|
4
|
-
};
|
|
5
|
-
export declare function transformFarmServerFns(code: string, id: string): FarmServerFnTransformResult | null;
|
|
1
|
+
export * from "@farm.js/core/internal/server-fn-transform";
|
|
6
2
|
//# sourceMappingURL=server-fn-transform.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server-fn-transform.d.ts","sourceRoot":"","sources":["../../src/rsc/server-fn-transform.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"server-fn-transform.d.ts","sourceRoot":"","sources":["../../src/rsc/server-fn-transform.ts"],"names":[],"mappings":"AAEA,cAAc,4CAA4C,CAAC"}
|