@farm.js/plugin 0.1.36 → 0.1.38
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 +7 -0
- package/dist/middleware/index.d.ts.map +1 -1
- package/dist/middleware/index.js +4 -1
- package/dist/rsc/index.d.ts.map +1 -1
- package/dist/rsc/index.js +15 -3
- package/dist/rsc/nitro-build.d.ts.map +1 -1
- package/dist/rsc/nitro-build.js +21 -3
- package/dist/rsc/server-fn-transform.d.ts +18 -1
- package/dist/rsc/server-fn-transform.d.ts.map +1 -1
- package/dist/rsc/server-fn-transform.js +100 -21
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -50,7 +50,14 @@ export interface MiddlewareContext {
|
|
|
50
50
|
request: IncomingMessage;
|
|
51
51
|
response: ServerResponse;
|
|
52
52
|
url: URL;
|
|
53
|
+
/** Request pathname as received: still percent-encoded. */
|
|
53
54
|
pathname: string;
|
|
55
|
+
/**
|
|
56
|
+
* The pathname the router matches: each segment decoded once, as core's
|
|
57
|
+
* `ctx.routePathname`. `.when()` conditions and function matchers also see it,
|
|
58
|
+
* so `/%61dmin` cannot skip a check written for `/admin`. Updated by `rewrite()`.
|
|
59
|
+
*/
|
|
60
|
+
readonly routePathname: string;
|
|
54
61
|
searchParams: URLSearchParams;
|
|
55
62
|
method: string;
|
|
56
63
|
params: Record<string, string>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/middleware/index.ts"],"names":[],"mappings":"AACA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,MAAM,CAAC;AAClD,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceR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EACtB,IAAI,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,KACtB,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;AAE1B,MAAM,WAAW,oBAAoB;IACnC,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EACF,iBAAiB,GACjB;QAAE,KAAK,EAAE,MAAM;YAAE,QAAQ,EAAE,iBAAiB,EAAE,CAAA;SAAE,CAAC;QAAC,WAAW,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAA;KAAE,CAAC;IAC7F,MAAM,CAAC,EAAE,gBAAgB,CAAC;CAC3B;AA6KD;;GAEG;AACH,MAAM,CAAC,OAAO,UAAU,cAAc,CAAC,OAAO,GAAE,qBAA0B,GAAG,MAAM,CAoUlF;AAED,OAAO,EAAE,cAAc,EAAE,CAAC"}
|
package/dist/middleware/index.js
CHANGED
|
@@ -3,7 +3,7 @@ import { createRequire } from "node:module";
|
|
|
3
3
|
import { _withAfterNodeMiddleware } from "@farm.js/core/after";
|
|
4
4
|
import { devServableFileExists } from "../dev-static.js";
|
|
5
5
|
import { resolveDevRequestURL, resolveViteHtmlFallbackUrl } from "../dev-request.js";
|
|
6
|
-
import { matchesMiddlewareConfig, serializeMiddlewareCookieDeletion, } from "@farm.js/core/middleware";
|
|
6
|
+
import { canonicalizeRequestPathname, matchesMiddlewareConfig, serializeMiddlewareCookieDeletion, } from "@farm.js/core/middleware";
|
|
7
7
|
import { middlewareMatchesPath, parseCookies, serializeCookie } from "./matching.js";
|
|
8
8
|
// Create require for ESM compatibility
|
|
9
9
|
const require_ = createRequire(import.meta.url);
|
|
@@ -94,6 +94,8 @@ function createContext(req, res, viteServer) {
|
|
|
94
94
|
response: res,
|
|
95
95
|
url,
|
|
96
96
|
pathname: url.pathname,
|
|
97
|
+
// Vite has already removed its base from the URL, so this is the app's path.
|
|
98
|
+
routePathname: canonicalizeRequestPathname(url.pathname),
|
|
97
99
|
searchParams: url.searchParams,
|
|
98
100
|
method: req.method || "GET",
|
|
99
101
|
params: {},
|
|
@@ -128,6 +130,7 @@ function createContext(req, res, viteServer) {
|
|
|
128
130
|
const newUrl = new URL(rewriteUrl, `http://${req.headers.host || "localhost"}`);
|
|
129
131
|
ctx.url = newUrl;
|
|
130
132
|
ctx.pathname = newUrl.pathname;
|
|
133
|
+
ctx.routePathname = canonicalizeRequestPathname(newUrl.pathname);
|
|
131
134
|
ctx.searchParams = newUrl.searchParams;
|
|
132
135
|
// Update the original request URL
|
|
133
136
|
req.url = rewriteUrl;
|
package/dist/rsc/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/rsc/index.ts"],"names":[],"mappings":"AACA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,OAAO,EAIL,KAAK,MAAM,EACX,KAAK,UAAU,EAChB,MAAM,MAAM,CAAC;AAEd,OAAO,KAAK,EAAE,oBAAoB,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AACrE,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,sCAAsC,CAAC;AACpF,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AACvD,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,0BAA0B,CAAC;AACrE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,2CAA2C,CAAC;AAClF,OAAO,KAAK,EAAE,cAAc,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAS9E,OAAO,EAAyB,KAAK,sBAAsB,EAAE,MAAM,wBAAwB,CAAC;AAC5F,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,2CAA2C,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/rsc/index.ts"],"names":[],"mappings":"AACA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,OAAO,EAIL,KAAK,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C,EAAE,oBAAoB,CAAC;IAClC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,YAAY,CAAC,EAAE;QACb;;;WAGG;QACH,gBAAgB,CAAC,EAAE,OAAO,CAAC;QAC3B;;;WAGG;QACH,aAAa,CAAC,EAAE,OAAO,CAAC;QACxB;;;;;;;WAOG;QACH,iBAAiB,CAAC,EAAE,OAAO,CAAC;KAC7B,CAAC;IACF,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,aAAa,CAAC,EAAE,uBAAuB,CAAC;IACxC,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,eAAe,CAAC,EAAE,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IACjD,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE;QACR,GAAG,CAAC,EAAE,MAAM,CAAC;QACb,GAAG,CAAC,EAAE,MAAM,CAAC;QACb,MAAM,CAAC,EAAE,MAAM,CAAC;KACjB,CAAC;IACF,gHAAgH;IAChH,OAAO,CAAC,EAAE,GAAG,EAAE,CAAC;IAChB;;;;OAIG;IACH,EAAE,CAAC,EAAE,sBAAsB,GAAG,OAAO,CAAC;IACtC;;;;;;OAMG;IACH,KAAK,CAAC,EAAE;QAAE,OAAO,CAAC,EAAE,OAAO,GAAG,qBAAqB,CAAA;KAAE,CAAC;IACtD;;;;;OAKG;IACH,GAAG,CAAC,EAAE,iBAAiB,CAAC;CACzB;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,MAAM,GAAE,aAAkB,GAAG,UAAU,CA6DnE;AAibD;;;;;GAKG;AACH,MAAM,CAAC,OAAO,UAAU,OAAO,CAAC,OAAO,GAAE,oBAAyB,GAAG,MAAM,EAAE,CA+kC5E"}
|
package/dist/rsc/index.js
CHANGED
|
@@ -32,7 +32,7 @@ import { generateSsrEntry } from "./entries/ssr.js";
|
|
|
32
32
|
import { generateErrorFallbackEntry } from "./entries/error-fallback.js";
|
|
33
33
|
import { generateClientEntry, serverFnTransportErrorClientRuntime } from "./entries/client.js";
|
|
34
34
|
import { transformFarmRouteActionClients } from "./route-action-transform.js";
|
|
35
|
-
import { transformFarmServerFns } from "./server-fn-transform.js";
|
|
35
|
+
import { createFarmServerFnBoundaryModule, isFarmServerFnBoundaryId, transformFarmServerFns, } from "./server-fn-transform.js";
|
|
36
36
|
import { transformAutomaticOptimizedBoundaries } from "./automatic-optimized-boundary.js";
|
|
37
37
|
import { resolveRscBuildOutputPath } from "./build-paths.js";
|
|
38
38
|
import { assertRscPackageCompatibility } from "./compatibility.js";
|
|
@@ -1032,10 +1032,22 @@ export default function farmRsc(options = {}) {
|
|
|
1032
1032
|
{
|
|
1033
1033
|
name: "@farm.js/plugin/rsc:server-fn-actions",
|
|
1034
1034
|
enforce: "pre",
|
|
1035
|
+
// The "use server" boundary for a module whose server functions were
|
|
1036
|
+
// found without an explicit directive. Its exports come from the source
|
|
1037
|
+
// file on disk, never from the request, so nothing else can be named.
|
|
1038
|
+
async load(id) {
|
|
1039
|
+
if (!rscEnabled || !actionsEnabled || !isFarmServerFnBoundaryId(id))
|
|
1040
|
+
return null;
|
|
1041
|
+
const file = id.split("?", 1)[0];
|
|
1042
|
+
this.addWatchFile(file);
|
|
1043
|
+
const source = await fs.readFile(file, "utf8");
|
|
1044
|
+
return createFarmServerFnBoundaryModule(source, file) ?? "export {};\n";
|
|
1045
|
+
},
|
|
1035
1046
|
transform(code, id) {
|
|
1036
|
-
if (!rscEnabled || !actionsEnabled)
|
|
1047
|
+
if (!rscEnabled || !actionsEnabled || isFarmServerFnBoundaryId(id))
|
|
1037
1048
|
return null;
|
|
1038
|
-
const
|
|
1049
|
+
const environment = this.environment?.name;
|
|
1050
|
+
const result = transformFarmServerFns(code, id, { environment: environment ?? "rsc" });
|
|
1039
1051
|
if (!result)
|
|
1040
1052
|
return null;
|
|
1041
1053
|
return {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"nitro-build.d.ts","sourceRoot":"","sources":["../../src/rsc/nitro-build.ts"],"names":[],"mappings":"AAAA;;;;GAIG;
|
|
1
|
+
{"version":3,"file":"nitro-build.d.ts","sourceRoot":"","sources":["../../src/rsc/nitro-build.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AA4BH;;;GAGG;AACH,wBAAgB,8BAA8B,CAC5C,IAAI,EAAE,MAAM,EACZ,WAAW,EAAE,MAAM,EACnB,WAAW,EAAE,MAAM,GAClB,IAAI,CAKN;AAcD,gBAAgB;AAChB,wBAAgB,sBAAsB,CAAC,SAAS,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,GAAG,IAAI,CAqCrF;AA4ED;;;GAGG;AACH,wBAAgB,kBAAkB,CAChC,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,MAAM,EACd,OAAO,GAAE;IAAE,UAAU,CAAC,EAAE,MAAM,CAAC;IAAC,SAAS,CAAC,EAAE,MAAM,CAAA;CAAO,GACxD,OAAO,CAAC,IAAI,CAAC,CA0Bf;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAC/B,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,MAAM,EACd,OAAO,GAAE;IAAE,UAAU,CAAC,EAAE,MAAM,CAAC;IAAC,SAAS,CAAC,EAAE,MAAM,CAAA;CAAO,GACxD,OAAO,CAAC,IAAI,CAAC,CAgCf;AA8FD,MAAM,MAAM,oBAAoB,GAAG;IACjC,YAAY,EAAE,MAAM,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,wBAAsB,aAAa,CAAC,OAAO,EAAE,oBAAoB,GAAG,OAAO,CAAC,IAAI,CAAC,CAiMhF"}
|
package/dist/rsc/nitro-build.js
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
import path from "path";
|
|
7
7
|
import { createRequire } from "node:module";
|
|
8
8
|
import { writeFileSync, readFileSync, mkdirSync, copyFileSync, existsSync, readdirSync, statSync, } 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";
|
|
10
11
|
const MANIFEST_FILENAME = "__vite_rsc_assets_manifest.js";
|
|
11
12
|
const MIN_MANIFEST_BYTES = 100; // real manifest is ~500+, stub is 64
|
|
@@ -62,9 +63,16 @@ export function rewriteRscEntryImports(serverDir, serverDistDir) {
|
|
|
62
63
|
const relative = path.relative(path.dirname(entryChunkPath), target).replace(/\\/g, "/");
|
|
63
64
|
return relative.startsWith(".") ? relative : `./${relative}`;
|
|
64
65
|
};
|
|
65
|
-
// Match any path that ends with dist/rsc/index.js
|
|
66
|
-
|
|
67
|
-
|
|
66
|
+
// Match any path that ends with dist/rsc/index.js, dist/ssr/index.js or the NODE_ENV module
|
|
67
|
+
// (minified, no spaces)
|
|
68
|
+
for (const [environment, file] of [
|
|
69
|
+
["rsc", "index.js"],
|
|
70
|
+
["rsc", FARM_NODE_ENV_MODULE_FILE],
|
|
71
|
+
["ssr", "index.js"],
|
|
72
|
+
]) {
|
|
73
|
+
const pattern = new RegExp(`(["'\`])[^"'\`]*?dist/${environment}/${file.replace(/\./g, "\\.")}\\1`, "g");
|
|
74
|
+
code = code.replace(pattern, JSON.stringify(relativeImport(path.join(serverDistDir, environment, file))));
|
|
75
|
+
}
|
|
68
76
|
writeFileSync(entryChunkPath, code, "utf-8");
|
|
69
77
|
}
|
|
70
78
|
}
|
|
@@ -207,7 +215,13 @@ function writeRscEntryFile(buildDir, rendererPath, ssrPath) {
|
|
|
207
215
|
const setSsLoader = relSsr
|
|
208
216
|
? `globalThis.__VITE_RSC_LOAD_SSR__ = () => import("./${relSsr}");`
|
|
209
217
|
: "";
|
|
218
|
+
// Imported first and kept external like the bundles, so it runs before React picks its
|
|
219
|
+
// development or production build from NODE_ENV. Inlined, it would run after them.
|
|
220
|
+
const relNodeEnv = path
|
|
221
|
+
.relative(buildDir, path.join(path.dirname(resolvedRenderer), FARM_NODE_ENV_MODULE_FILE))
|
|
222
|
+
.replace(/\\/g, "/");
|
|
210
223
|
const code = `
|
|
224
|
+
import "./${relNodeEnv}";
|
|
211
225
|
${setSsLoader}
|
|
212
226
|
import * as entryExports from "./${relRenderer}";
|
|
213
227
|
import { defineEventHandler } from "h3";
|
|
@@ -289,6 +303,10 @@ export async function buildRscNitro(options) {
|
|
|
289
303
|
const buildDir = path.join(root, ".nitro");
|
|
290
304
|
const serverDir = path.join(outputDir, "server");
|
|
291
305
|
const publicOutDir = path.join(outputDir, "public");
|
|
306
|
+
// The server sets NODE_ENV to "production" unless the deployment chose one, as the core
|
|
307
|
+
// node-server output does. The RSC and SSR bundles read it at runtime: left unset, React loads
|
|
308
|
+
// its development builds and Flight responses carry server component props, stacks and paths.
|
|
309
|
+
writeFileSync(path.join(path.dirname(path.resolve(rendererPath)), FARM_NODE_ENV_MODULE_FILE), FARM_NODE_ENV_MODULE_SOURCE, "utf-8");
|
|
292
310
|
const entryPath = writeRscEntryFile(buildDir, rendererPath, ssrPath);
|
|
293
311
|
const registeredRuntimePackages = new Map(runtimePackagesByRoot.get(path.resolve(root)) ?? []);
|
|
294
312
|
for (const [packageName, packageRoot] of detectBundledRscRuntimes(rendererPath)) {
|
|
@@ -1,6 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Query that marks the generated "use server" boundary of a module whose server
|
|
3
|
+
* functions were found implicitly. Only the server functions are exported from
|
|
4
|
+
* it, so @vitejs/plugin-rsc registers nothing else from the source module.
|
|
5
|
+
*/
|
|
6
|
+
export declare const FARM_SERVER_FN_BOUNDARY_QUERY = "?farm-server-fns";
|
|
7
|
+
export type FarmServerFnTransformOptions = {
|
|
8
|
+
/** Vite environment being transformed. Anything other than "rsc" gets the client view. */
|
|
9
|
+
environment?: string;
|
|
10
|
+
};
|
|
1
11
|
export type FarmServerFnTransformResult = {
|
|
2
12
|
code: string;
|
|
3
13
|
exports: string[];
|
|
4
14
|
};
|
|
5
|
-
export declare function transformFarmServerFns(code: string, id: string): FarmServerFnTransformResult | null;
|
|
15
|
+
export declare function transformFarmServerFns(code: string, id: string, options?: FarmServerFnTransformOptions): FarmServerFnTransformResult | null;
|
|
16
|
+
/**
|
|
17
|
+
* Source of the generated boundary for a module without an explicit
|
|
18
|
+
* "use server". Its wrappers call the source module's private bindings lazily,
|
|
19
|
+
* so the import cycle between the two modules is safe in any load order.
|
|
20
|
+
*/
|
|
21
|
+
export declare function createFarmServerFnBoundaryModule(code: string, id: string): string | null;
|
|
22
|
+
export declare function isFarmServerFnBoundaryId(id: string): boolean;
|
|
6
23
|
//# 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":"AAkBA,MAAM,MAAM,2BAA2B,GAAG;IACxC,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,EAAE,CAAC;CACnB,CAAC;
|
|
1
|
+
{"version":3,"file":"server-fn-transform.d.ts","sourceRoot":"","sources":["../../src/rsc/server-fn-transform.ts"],"names":[],"mappings":"AAkBA;;;;GAIG;AACH,eAAO,MAAM,6BAA6B,qBAAqB,CAAC;AAEhE,MAAM,MAAM,4BAA4B,GAAG;IACzC,0FAA0F;IAC1F,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,2BAA2B,GAAG;IACxC,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,EAAE,CAAC;CACnB,CAAC;AAQF,wBAAgB,sBAAsB,CACpC,IAAI,EAAE,MAAM,EACZ,EAAE,EAAE,MAAM,EACV,OAAO,GAAE,4BAAiC,GACzC,2BAA2B,GAAG,IAAI,CA2CpC;AAED;;;;GAIG;AACH,wBAAgB,gCAAgC,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAkBxF;AAED,wBAAgB,wBAAwB,CAAC,EAAE,EAAE,MAAM,WAElD"}
|
|
@@ -10,7 +10,92 @@ const EXPORT_DEFAULT_ALIAS_RE = /\bexport\s+default\s+([A-Za-z_$][\w$]*)\s*(?:;|
|
|
|
10
10
|
const EXPORT_BINDING_ALIAS_RE = /\bexport\s+(?:const|let|var)\s+([A-Za-z_$][\w$]*)\s*=\s*([A-Za-z_$][\w$]*)\s*(?:;|(?=[,\r\n])|$)/g;
|
|
11
11
|
const MODULE_EXT_RE = /\.[cm]?[jt]sx?$/;
|
|
12
12
|
const DECLARATION_RE = /(^|\n)\s*["']([^"']+)["']\s*;?/g;
|
|
13
|
-
|
|
13
|
+
/**
|
|
14
|
+
* Query that marks the generated "use server" boundary of a module whose server
|
|
15
|
+
* functions were found implicitly. Only the server functions are exported from
|
|
16
|
+
* it, so @vitejs/plugin-rsc registers nothing else from the source module.
|
|
17
|
+
*/
|
|
18
|
+
export const FARM_SERVER_FN_BOUNDARY_QUERY = "?farm-server-fns";
|
|
19
|
+
export function transformFarmServerFns(code, id, options = {}) {
|
|
20
|
+
const analysis = analyzeServerFnModule(code, id);
|
|
21
|
+
if (!analysis)
|
|
22
|
+
return null;
|
|
23
|
+
const { replacements } = analysis;
|
|
24
|
+
const exports = replacements.map((replacement) => replacement.exportName);
|
|
25
|
+
// An explicit module directive is the app saying every export is an action.
|
|
26
|
+
if (hasModuleDirective(code, "use server")) {
|
|
27
|
+
return { code: applyReplacements(code, replacements, "wrapper"), exports };
|
|
28
|
+
}
|
|
29
|
+
// Without one, only the server functions may become actions. A module-level
|
|
30
|
+
// "use server" here would make @vitejs/plugin-rsc register every exported
|
|
31
|
+
// async function, so a plain helper next to a server function would be
|
|
32
|
+
// callable by id, skipping the server function's middleware. Route the
|
|
33
|
+
// server functions through a generated boundary module instead.
|
|
34
|
+
const boundaryId = JSON.stringify(serverFnBoundaryId(id));
|
|
35
|
+
if (options.environment !== undefined && options.environment !== "rsc") {
|
|
36
|
+
// Client and SSR builds only need the action references.
|
|
37
|
+
return {
|
|
38
|
+
code: `export { ${exports.join(", ")} } from ${boundaryId};\n`,
|
|
39
|
+
exports,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
const imports = replacements.map((replacement) => replacement.exportName === "default"
|
|
43
|
+
? "default as $$farm_server_fn_boundary_default"
|
|
44
|
+
: replacement.exportName);
|
|
45
|
+
const reExports = replacements.map((replacement) => replacement.exportName === "default"
|
|
46
|
+
? "$$farm_server_fn_boundary_default as default"
|
|
47
|
+
: replacement.exportName);
|
|
48
|
+
const privateNames = replacements.map((replacement) => replacement.privateName);
|
|
49
|
+
return {
|
|
50
|
+
code: applyReplacements(code, replacements, "private") +
|
|
51
|
+
`\nimport { ${imports.join(", ")} } from ${boundaryId};` +
|
|
52
|
+
`\nexport { ${[...reExports, ...privateNames].join(", ")} };\n`,
|
|
53
|
+
exports,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Source of the generated boundary for a module without an explicit
|
|
58
|
+
* "use server". Its wrappers call the source module's private bindings lazily,
|
|
59
|
+
* so the import cycle between the two modules is safe in any load order.
|
|
60
|
+
*/
|
|
61
|
+
export function createFarmServerFnBoundaryModule(code, id) {
|
|
62
|
+
if (hasModuleDirective(code, "use server"))
|
|
63
|
+
return null;
|
|
64
|
+
const analysis = analyzeServerFnModule(code, id);
|
|
65
|
+
if (!analysis)
|
|
66
|
+
return null;
|
|
67
|
+
const sourceId = JSON.stringify(id.split("?", 1)[0].replace(/\\/g, "/"));
|
|
68
|
+
const privateNames = analysis.replacements.map((replacement) => replacement.privateName);
|
|
69
|
+
let output = `"use server";\nimport { ${privateNames.join(", ")} } from ${sourceId};\n`;
|
|
70
|
+
for (const replacement of analysis.replacements) {
|
|
71
|
+
const declaration = replacement.exportName === "default"
|
|
72
|
+
? "export default async function"
|
|
73
|
+
: "export async function";
|
|
74
|
+
output +=
|
|
75
|
+
`${declaration} ${replacement.wrapperName}(input) {\n return ${replacement.privateName}(input);\n}\n` +
|
|
76
|
+
`${copyServerFunctionDescriptor(replacement.wrapperName, replacement.privateName, true)}\n`;
|
|
77
|
+
}
|
|
78
|
+
return output;
|
|
79
|
+
}
|
|
80
|
+
export function isFarmServerFnBoundaryId(id) {
|
|
81
|
+
return /[?&]farm-server-fns(?:&|$)/.test(id);
|
|
82
|
+
}
|
|
83
|
+
function serverFnBoundaryId(id) {
|
|
84
|
+
return `${id.split("?", 1)[0].replace(/\\/g, "/")}${FARM_SERVER_FN_BOUNDARY_QUERY}`;
|
|
85
|
+
}
|
|
86
|
+
function applyReplacements(code, replacements, mode) {
|
|
87
|
+
let output = code;
|
|
88
|
+
for (const replacement of [...replacements].sort((a, b) => b.start - a.start)) {
|
|
89
|
+
const declaration = `const ${replacement.privateName} = ${replacement.callExpression};\n`;
|
|
90
|
+
const wrapper = mode === "private"
|
|
91
|
+
? ""
|
|
92
|
+
: `${replacement.exportName === "default" ? "export default async function" : "export async function"} ${replacement.wrapperName}(input) {\n return ${replacement.privateName}(input);\n}\n${copyServerFunctionDescriptor(replacement.wrapperName, replacement.privateName, false)}`;
|
|
93
|
+
output =
|
|
94
|
+
output.slice(0, replacement.start) + declaration + wrapper + output.slice(replacement.end);
|
|
95
|
+
}
|
|
96
|
+
return output;
|
|
97
|
+
}
|
|
98
|
+
function analyzeServerFnModule(code, id) {
|
|
14
99
|
if (!isTransformableModule(id) ||
|
|
15
100
|
(!code.includes("createServerFn") && !code.includes("createServerQuery"))) {
|
|
16
101
|
return null;
|
|
@@ -38,17 +123,7 @@ export function transformFarmServerFns(code, id) {
|
|
|
38
123
|
if (hasModuleDirective(code, "use client")) {
|
|
39
124
|
throw new Error("createServerFn and createServerQuery exports must live in a server module. Move them out of the 'use client' file.");
|
|
40
125
|
}
|
|
41
|
-
|
|
42
|
-
for (const replacement of [...replacements].sort((a, b) => b.start - a.start)) {
|
|
43
|
-
output = output.slice(0, replacement.start) + replacement.code + output.slice(replacement.end);
|
|
44
|
-
}
|
|
45
|
-
if (!hasModuleDirective(output, "use server")) {
|
|
46
|
-
output = `"use server";\n${output}`;
|
|
47
|
-
}
|
|
48
|
-
return {
|
|
49
|
-
code: output,
|
|
50
|
-
exports: replacements.map((replacement) => replacement.exportName),
|
|
51
|
-
};
|
|
126
|
+
return { replacements };
|
|
52
127
|
}
|
|
53
128
|
function isTransformableModule(id) {
|
|
54
129
|
const cleanId = id.split("?", 1)[0];
|
|
@@ -221,17 +296,17 @@ function findServerFnExportReplacements(code, factoryNames) {
|
|
|
221
296
|
if (code[declarationEnd] === ";")
|
|
222
297
|
declarationEnd++;
|
|
223
298
|
const calleeIndex = code.indexOf(calleeName, match.index);
|
|
224
|
-
const callExpression = code.slice(calleeIndex, callEnd + 1);
|
|
225
|
-
const privateName = `$$farm_server_${factory}_${exportName}`;
|
|
226
299
|
replacements.push({
|
|
227
300
|
start: match.index ?? 0,
|
|
228
301
|
end: declarationEnd,
|
|
229
302
|
exportName,
|
|
230
|
-
|
|
303
|
+
privateName: `$$farm_server_${factory}_${exportName}`,
|
|
304
|
+
wrapperName: exportName,
|
|
305
|
+
callExpression: code.slice(calleeIndex, callEnd + 1),
|
|
231
306
|
});
|
|
232
307
|
}
|
|
233
308
|
// `export default createServerFn(...)` needs the same treatment. Without it the
|
|
234
|
-
// module gets no
|
|
309
|
+
// module gets no server boundary and its handler ships to the client.
|
|
235
310
|
for (const match of code.matchAll(EXPORT_DEFAULT_SERVER_FUNCTION_FACTORY_RE)) {
|
|
236
311
|
const calleeName = match[1];
|
|
237
312
|
const factory = calleeName ? factoryNames.get(calleeName) : undefined;
|
|
@@ -247,13 +322,14 @@ function findServerFnExportReplacements(code, factoryNames) {
|
|
|
247
322
|
if (code[declarationEnd] === ";")
|
|
248
323
|
declarationEnd++;
|
|
249
324
|
const calleeIndex = code.indexOf(calleeName, match.index);
|
|
250
|
-
const callExpression = code.slice(calleeIndex, callEnd + 1);
|
|
251
325
|
const privateName = `$$farm_server_${factory}_default`;
|
|
252
326
|
replacements.push({
|
|
253
327
|
start: match.index ?? 0,
|
|
254
328
|
end: declarationEnd,
|
|
255
329
|
exportName: "default",
|
|
256
|
-
|
|
330
|
+
privateName,
|
|
331
|
+
wrapperName: `${privateName}_export`,
|
|
332
|
+
callExpression: code.slice(calleeIndex, callEnd + 1),
|
|
257
333
|
});
|
|
258
334
|
}
|
|
259
335
|
return replacements;
|
|
@@ -261,11 +337,14 @@ function findServerFnExportReplacements(code, factoryNames) {
|
|
|
261
337
|
/**
|
|
262
338
|
* The exported wrapper replaces the factory's function, so it would lose the internal descriptor
|
|
263
339
|
* that MCP and other non-browser callers read. Copy only that descriptor: the other server
|
|
264
|
-
* function markers stay on the private binding, as before.
|
|
340
|
+
* function markers stay on the private binding, as before. The boundary module reads it lazily
|
|
341
|
+
* because the private binding may not be initialized yet while the two modules load.
|
|
265
342
|
*/
|
|
266
|
-
function copyServerFunctionDescriptor(target, source) {
|
|
343
|
+
function copyServerFunctionDescriptor(target, source, lazy) {
|
|
267
344
|
const key = 'Symbol.for("farm.server-function.descriptor")';
|
|
268
|
-
return
|
|
345
|
+
return lazy
|
|
346
|
+
? `Object.defineProperty(${target}, ${key}, { get: () => ${source}[${key}] });`
|
|
347
|
+
: `Object.defineProperty(${target}, ${key}, { value: ${source}[${key}] });`;
|
|
269
348
|
}
|
|
270
349
|
function hasModuleDirective(code, directive) {
|
|
271
350
|
for (const match of code.matchAll(DECLARATION_RE)) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@farm.js/plugin",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.38",
|
|
4
4
|
"description": "Official plugins for Farm.js framework",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Farm.js Team",
|
|
@@ -62,7 +62,7 @@
|
|
|
62
62
|
"nitro": "3.0.260903-beta",
|
|
63
63
|
"picocolors": "^1.0.0",
|
|
64
64
|
"rsc-html-stream": "^0.0.4",
|
|
65
|
-
"@farm.js/core": "0.1.
|
|
65
|
+
"@farm.js/core": "0.1.38"
|
|
66
66
|
},
|
|
67
67
|
"devDependencies": {
|
|
68
68
|
"@types/node": "^22.20.5",
|
|
@@ -83,7 +83,7 @@
|
|
|
83
83
|
"react-dom": "19.2.8",
|
|
84
84
|
"react-server-dom-webpack": "19.2.8",
|
|
85
85
|
"vite": ">=6.0.0",
|
|
86
|
-
"@farm.js/core": "0.1.
|
|
86
|
+
"@farm.js/core": "0.1.38"
|
|
87
87
|
},
|
|
88
88
|
"peerDependenciesMeta": {
|
|
89
89
|
"@vitejs/plugin-rsc": {
|