@farm.js/plugin 0.1.49 → 0.1.51-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +157 -3
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/rsc/server-fn-transform.d.ts +1 -22
- package/dist/rsc/server-fn-transform.d.ts.map +1 -1
- package/dist/rsc/server-fn-transform.js +3 -417
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,11 +1,165 @@
|
|
|
1
1
|
# @farm.js/plugin
|
|
2
2
|
|
|
3
|
-
Official plugins for Farm.js
|
|
3
|
+
Official plugins for Farm.js. Its main entry, `@farm.js/plugin/rsc`, is the React Server Components
|
|
4
|
+
pipeline Farm runs on the official `@vitejs/plugin-rsc`: it generates the RSC, SSR, and browser
|
|
5
|
+
entries from your routes, applies the server function transform for client imports, and builds the
|
|
6
|
+
output through Nitro. The package also ships standalone Vite plugins for middleware and API routes,
|
|
7
|
+
and two Farm plugins for observability signals and request context.
|
|
4
8
|
|
|
5
9
|
See [Stability and Support](https://farmjs.dev/docs/stability) for what this package guarantees.
|
|
10
|
+
React Server Components themselves are an experimental feature outside that promise.
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
Most apps never install this package by hand. With `experimental.serverComponents` set in
|
|
15
|
+
`farm.config.ts`, `farm dev` and `farm build` install `@farm.js/plugin` at the version of the
|
|
16
|
+
installed `@farm.js/core`, and its RSC peers at the versions it pins. CI never changes dependencies,
|
|
17
|
+
and `FARM_SKIP_RSC_INSTALL=1` turns the install off anywhere else. To install it yourself:
|
|
6
18
|
|
|
7
19
|
```bash
|
|
8
|
-
|
|
20
|
+
pnpm add @farm.js/plugin @vitejs/plugin-rsc react-server-dom-webpack
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
`@farm.js/core`, `react`, `react-dom`, and `vite` are required peers. `@vitejs/plugin-rsc` and
|
|
24
|
+
`react-server-dom-webpack` are optional peers, needed only for `@farm.js/plugin/rsc`. The RSC
|
|
25
|
+
pipeline accepts one tested combination of `react`, `react-dom`, `react-server-dom-webpack`, and
|
|
26
|
+
`@vitejs/plugin-rsc`, pinned exactly in this package's `peerDependencies`, and stops at startup
|
|
27
|
+
with the expected and found versions when the app resolves anything else.
|
|
28
|
+
|
|
29
|
+
## Configure
|
|
30
|
+
|
|
31
|
+
The usual setup is one flag in `farm.config.ts`. Farm maps the config onto this plugin:
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
import { defineConfig } from "@farm.js/core";
|
|
35
|
+
|
|
36
|
+
export default defineConfig({
|
|
37
|
+
experimental: {
|
|
38
|
+
serverComponents: true,
|
|
39
|
+
// Opt in separately to client-callable server actions and server functions.
|
|
40
|
+
serverActions: true,
|
|
41
|
+
},
|
|
42
|
+
});
|
|
9
43
|
```
|
|
10
44
|
|
|
11
|
-
|
|
45
|
+
An app driven by the `vite` CLI uses `defineConfig` from `@farm.js/plugin/rsc` in
|
|
46
|
+
`vite.config.ts` instead, as the `examples/rsc-demo` app does. `@vitejs/plugin-react` is installed
|
|
47
|
+
separately here:
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
// vite.config.ts
|
|
51
|
+
import rsc from "@vitejs/plugin-rsc";
|
|
52
|
+
import react from "@vitejs/plugin-react";
|
|
53
|
+
import { defineConfig, nitro } from "@farm.js/plugin/rsc";
|
|
54
|
+
|
|
55
|
+
export default defineConfig({
|
|
56
|
+
srcDir: "src",
|
|
57
|
+
experimental: { serverActions: true },
|
|
58
|
+
plugins: [
|
|
59
|
+
rsc({
|
|
60
|
+
serverHandler: false,
|
|
61
|
+
entries: {
|
|
62
|
+
rsc: "./.farm/rsc-entries/entry.rsc.tsx",
|
|
63
|
+
ssr: "./.farm/rsc-entries/entry.ssr.tsx",
|
|
64
|
+
client: "./.farm/rsc-entries/entry.browser.tsx",
|
|
65
|
+
},
|
|
66
|
+
}),
|
|
67
|
+
react(),
|
|
68
|
+
nitro({ server: { environmentName: "rsc" }, config: { preset: "node-server" } }),
|
|
69
|
+
],
|
|
70
|
+
});
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
The plugin writes those entry files under `.farm/rsc-entries/`. Useful `defineConfig` options:
|
|
74
|
+
|
|
75
|
+
| Option | Default | Notes |
|
|
76
|
+
| -------------------------------- | ------- | -------------------------------------------------------------- |
|
|
77
|
+
| `srcDir` | `"src"` | |
|
|
78
|
+
| `routesDir` | `"app"` | Route folder inside `srcDir`. `""` uses `srcDir` itself. |
|
|
79
|
+
| `port` | `3000` | Development server port. |
|
|
80
|
+
| `experimental.serverActions` | `false` | React Server Actions and their POST decoder. |
|
|
81
|
+
| `experimental.optimizedBoundary` | `false` | Native Strata rendering for eligible host-only trees. |
|
|
82
|
+
| `middleware`, `api`, `images` | | Same shapes as in `farm.config.ts`. |
|
|
83
|
+
| `md`, `agent.llmsTxt`, `mcp` | | Markdown mirrors, `/llms.txt`, and MCP (needs `@farm.js/mcp`). |
|
|
84
|
+
| `deploymentId` | | Rejects requests from stale deployments. |
|
|
85
|
+
| `plugins` | | Extra Vite plugins, such as `rsc()`, `react()`, and `nitro()`. |
|
|
86
|
+
|
|
87
|
+
`nitro()` builds the deployment after `vite build`. `config.preset` falls back to `NITRO_PRESET`,
|
|
88
|
+
then `vercel`, and `config.outputDir` defaults to `.output`.
|
|
89
|
+
|
|
90
|
+
## Entry points
|
|
91
|
+
|
|
92
|
+
- `@farm.js/plugin`: only `version`. Import plugins from a subpath.
|
|
93
|
+
- `@farm.js/plugin/rsc`: `defineConfig`, the underlying RSC Vite plugins as the default export,
|
|
94
|
+
`nitro`, and `buildRscNitro` for post-build scripts.
|
|
95
|
+
- `@farm.js/plugin/rsc/vite-plugin-nitro`: the same `nitro()` Vite plugin as a default export.
|
|
96
|
+
- `@farm.js/plugin/rsc/optimized-boundary`: `OptimizedBoundary` and the Strata document types, for
|
|
97
|
+
explicit native boundaries. With `experimental.optimizedBoundary` Farm selects eligible regions
|
|
98
|
+
automatically, so most apps need no import. Strata uses a native Node binding; keep the experiment
|
|
99
|
+
on Node deployments.
|
|
100
|
+
- `@farm.js/plugin/middleware` and `@farm.js/plugin/api`: `farmMiddleware` and `farmApi`, Vite
|
|
101
|
+
plugins for apps without the RSC pipeline.
|
|
102
|
+
- `@farm.js/plugin/observability` and `@farm.js/plugin/context`: `observabilityPlugin` and
|
|
103
|
+
`contextPlugin`, Farm plugins.
|
|
104
|
+
|
|
105
|
+
## Standalone Vite plugins
|
|
106
|
+
|
|
107
|
+
`farmMiddleware` runs `middleware.ts` files found under `srcDir`, and `farmApi` serves
|
|
108
|
+
`route.ts` files under `<srcDir>/api` at `/api/...` from their exported method handlers. Both hook
|
|
109
|
+
into the Vite development server only.
|
|
110
|
+
|
|
111
|
+
```ts
|
|
112
|
+
// vite.config.ts
|
|
113
|
+
import { defineConfig } from "vite";
|
|
114
|
+
import { farmMiddleware } from "@farm.js/plugin/middleware";
|
|
115
|
+
import { farmApi } from "@farm.js/plugin/api";
|
|
116
|
+
|
|
117
|
+
export default defineConfig({
|
|
118
|
+
plugins: [farmMiddleware({ srcDir: "src" }), farmApi({ srcDir: "src" })],
|
|
119
|
+
});
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Farm plugins
|
|
123
|
+
|
|
124
|
+
`observabilityPlugin` records signals for completed requests, API responses, renders, errors, and
|
|
125
|
+
builds, and turns 5xx responses, runtime errors, and requests slower than `telemetry.slowRequestMs`
|
|
126
|
+
(default `300`) into deduplicated incidents. Its default `workflow.pipeline` is `["notify"]`, which
|
|
127
|
+
calls `workflow.callbacks.onIncident` or logs a warning. `telemetry.annotateHtml` (default `true`)
|
|
128
|
+
appends an HTML comment with the path and route to rendered pages.
|
|
129
|
+
|
|
130
|
+
```ts
|
|
131
|
+
import { defineConfig } from "@farm.js/core";
|
|
132
|
+
import { observabilityPlugin } from "@farm.js/plugin/observability";
|
|
133
|
+
|
|
134
|
+
export default defineConfig({
|
|
135
|
+
plugins: [
|
|
136
|
+
observabilityPlugin({
|
|
137
|
+
service: "storefront",
|
|
138
|
+
telemetry: { slowRequestMs: 500 },
|
|
139
|
+
workflow: {
|
|
140
|
+
callbacks: {
|
|
141
|
+
onIncident: ({ incident }) => console.warn(incident.severity, incident.title),
|
|
142
|
+
},
|
|
143
|
+
},
|
|
144
|
+
}),
|
|
145
|
+
],
|
|
146
|
+
});
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
`contextPlugin` is a small example plugin. It sets `requestId` (from `x-request-id`, or a random
|
|
150
|
+
UUID), `locale` (from `x-locale`, default `en`), and `pathname` on the request context and exposes
|
|
151
|
+
them to server page props as `props.context`.
|
|
152
|
+
|
|
153
|
+
Both are Farm plugins, so they run in ordinary Farm apps. The RSC pipeline ignores Farm `plugins`
|
|
154
|
+
with a warning.
|
|
155
|
+
|
|
156
|
+
## Documentation
|
|
157
|
+
|
|
158
|
+
- [Server Rendering](https://farmjs.dev/docs/server-rendering): React Server Components setup and
|
|
159
|
+
the optimized boundary experiment.
|
|
160
|
+
- [Server Queries](https://farmjs.dev/docs/server-queries): the server function transform.
|
|
161
|
+
- [Middleware](https://farmjs.dev/docs/middleware), [Markdown](https://farmjs.dev/docs/markdown),
|
|
162
|
+
and [MCP](https://farmjs.dev/docs/plugins/mcp) in RSC apps.
|
|
163
|
+
- [Configuration](https://farmjs.dev/docs/configuration)
|
|
164
|
+
|
|
165
|
+
Report issues and find examples in the [Farm.js repository](https://github.com/farming-labs/farm.js).
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AAEH,eAAO,MAAM,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AAEH,eAAO,MAAM,OAAO,kBAAkB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,23 +1,2 @@
|
|
|
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
|
-
};
|
|
11
|
-
export type FarmServerFnTransformResult = {
|
|
12
|
-
code: string;
|
|
13
|
-
exports: string[];
|
|
14
|
-
};
|
|
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;
|
|
1
|
+
export * from "@farm.js/core/internal/server-fn-transform";
|
|
23
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"}
|
|
@@ -1,417 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
// Any assignment counts as a declaration, not only `const x = factory(...)`:
|
|
5
|
-
// splitting `let save; save = createServerFn(...)` must not hide the binding
|
|
6
|
-
// from the fail-closed export checks below.
|
|
7
|
-
const SERVER_FUNCTION_DECLARATION_RE = /(?<![.\w$])([A-Za-z_$][\w$]*)\s*=\s*([A-Za-z_$][\w$]*)\s*(?:<[^<>]*(?:<[^<>]*>[^<>]*)*>)?\s*\(/g;
|
|
8
|
-
const LOCAL_EXPORT_CLAUSE_RE = /\bexport\s*\{([^}]*)\}/g;
|
|
9
|
-
const EXPORT_DEFAULT_ALIAS_RE = /\bexport\s+default\s+([A-Za-z_$][\w$]*)\s*(?:;|(?=[\r\n])|$)/g;
|
|
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
|
-
const MODULE_EXT_RE = /\.[cm]?[jt]sx?$/;
|
|
12
|
-
const DECLARATION_RE = /(^|\n)\s*["']([^"']+)["']\s*;?/g;
|
|
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) {
|
|
99
|
-
if (!isTransformableModule(id) ||
|
|
100
|
-
(!code.includes("createServerFn") && !code.includes("createServerQuery"))) {
|
|
101
|
-
return null;
|
|
102
|
-
}
|
|
103
|
-
const factoryNames = findServerFunctionFactoryImportNames(code);
|
|
104
|
-
if (factoryNames.size === 0)
|
|
105
|
-
return null;
|
|
106
|
-
const reExport = findServerFnReExport(code, factoryNames);
|
|
107
|
-
if (reExport) {
|
|
108
|
-
const factoryName = reExport.factory === "fn" ? "createServerFn" : "createServerQuery";
|
|
109
|
-
const escape = reExport.form === "clause"
|
|
110
|
-
? "with an export clause"
|
|
111
|
-
: reExport.form === "default"
|
|
112
|
-
? "as the default export"
|
|
113
|
-
: "through another exported binding";
|
|
114
|
-
const inlineForm = reExport.form === "default"
|
|
115
|
-
? `export default ${factoryName}(...)`
|
|
116
|
-
: `export const ${reExport.exportName} = ${factoryName}(...)`;
|
|
117
|
-
throw new Error(`Server function "${reExport.localName}" cannot be re-exported ${escape}. ` +
|
|
118
|
-
`Export it inline instead: ${inlineForm}.`);
|
|
119
|
-
}
|
|
120
|
-
const replacements = findServerFnExportReplacements(code, factoryNames);
|
|
121
|
-
if (replacements.length === 0)
|
|
122
|
-
return null;
|
|
123
|
-
if (hasModuleDirective(code, "use client")) {
|
|
124
|
-
throw new Error("createServerFn and createServerQuery exports must live in a server module. Move them out of the 'use client' file.");
|
|
125
|
-
}
|
|
126
|
-
return { replacements };
|
|
127
|
-
}
|
|
128
|
-
function isTransformableModule(id) {
|
|
129
|
-
const cleanId = id.split("?", 1)[0];
|
|
130
|
-
return MODULE_EXT_RE.test(cleanId) && !/\.d\.[cm]?ts$/.test(cleanId);
|
|
131
|
-
}
|
|
132
|
-
function findServerFunctionFactoryImportNames(code) {
|
|
133
|
-
const names = new Map();
|
|
134
|
-
for (const match of code.matchAll(SERVER_FUNCTION_FACTORY_IMPORT_RE)) {
|
|
135
|
-
const importClause = match[1] ?? "";
|
|
136
|
-
const namedStart = importClause.indexOf("{");
|
|
137
|
-
const namedEnd = importClause.lastIndexOf("}");
|
|
138
|
-
if (namedStart === -1 || namedEnd === -1 || namedEnd <= namedStart)
|
|
139
|
-
continue;
|
|
140
|
-
const namedImports = importClause.slice(namedStart + 1, namedEnd).split(",");
|
|
141
|
-
for (const specifier of namedImports) {
|
|
142
|
-
const normalized = specifier.trim().replace(/^type\s+/, "");
|
|
143
|
-
const specifierMatch = normalized.match(/^(createServerFn|createServerQuery)(?:\s+as\s+([A-Za-z_$][\w$]*))?$/);
|
|
144
|
-
if (specifierMatch) {
|
|
145
|
-
const factory = specifierMatch[1];
|
|
146
|
-
names.set(specifierMatch[2] ?? factory, factory === "createServerFn" ? "fn" : "query");
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
return names;
|
|
151
|
-
}
|
|
152
|
-
function findServerFnReExport(code, factoryNames) {
|
|
153
|
-
// Comments and string contents must not hide or fake an export: a comment
|
|
154
|
-
// inside `export { /* … */ save }` previously made the specifier unparsable
|
|
155
|
-
// and the module shipped untransformed.
|
|
156
|
-
const scannable = stripCommentsAndStrings(code);
|
|
157
|
-
const declarations = new Map();
|
|
158
|
-
for (const match of scannable.matchAll(SERVER_FUNCTION_DECLARATION_RE)) {
|
|
159
|
-
const localName = match[1];
|
|
160
|
-
const factoryName = match[2];
|
|
161
|
-
const factory = factoryName ? factoryNames.get(factoryName) : undefined;
|
|
162
|
-
if (localName && factory)
|
|
163
|
-
declarations.set(localName, factory);
|
|
164
|
-
}
|
|
165
|
-
if (declarations.size === 0)
|
|
166
|
-
return null;
|
|
167
|
-
for (const match of scannable.matchAll(LOCAL_EXPORT_CLAUSE_RE)) {
|
|
168
|
-
const clause = match[1] ?? "";
|
|
169
|
-
const clauseEnd = (match.index ?? 0) + match[0].length;
|
|
170
|
-
if (scannable.slice(clauseEnd).trimStart().startsWith("from"))
|
|
171
|
-
continue;
|
|
172
|
-
for (const specifier of clause.split(",")) {
|
|
173
|
-
const parts = specifier.trim().split(/\s+as\s+/);
|
|
174
|
-
const localName = parts[0]?.trim();
|
|
175
|
-
if (!localName || !declarations.has(localName))
|
|
176
|
-
continue;
|
|
177
|
-
return {
|
|
178
|
-
localName,
|
|
179
|
-
exportName: parts[1]?.trim() || localName,
|
|
180
|
-
factory: declarations.get(localName),
|
|
181
|
-
form: "clause",
|
|
182
|
-
};
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
// `export default save;` — the export-default replacement only recognizes a
|
|
186
|
-
// direct factory call, so a bare identifier slipped through untransformed.
|
|
187
|
-
for (const match of scannable.matchAll(EXPORT_DEFAULT_ALIAS_RE)) {
|
|
188
|
-
const localName = match[1];
|
|
189
|
-
if (!localName || !declarations.has(localName))
|
|
190
|
-
continue;
|
|
191
|
-
return {
|
|
192
|
-
localName,
|
|
193
|
-
exportName: "default",
|
|
194
|
-
factory: declarations.get(localName),
|
|
195
|
-
form: "default",
|
|
196
|
-
};
|
|
197
|
-
}
|
|
198
|
-
// `export const persist = save;` — same escape through a renamed binding.
|
|
199
|
-
for (const match of scannable.matchAll(EXPORT_BINDING_ALIAS_RE)) {
|
|
200
|
-
const exportName = match[1];
|
|
201
|
-
const localName = match[2];
|
|
202
|
-
if (!exportName || !localName || !declarations.has(localName))
|
|
203
|
-
continue;
|
|
204
|
-
return {
|
|
205
|
-
localName,
|
|
206
|
-
exportName,
|
|
207
|
-
factory: declarations.get(localName),
|
|
208
|
-
form: "binding",
|
|
209
|
-
};
|
|
210
|
-
}
|
|
211
|
-
return null;
|
|
212
|
-
}
|
|
213
|
-
/**
|
|
214
|
-
* Blank out comments and string/template contents, preserving length, so the
|
|
215
|
-
* export scans above see only real syntax. Quotes themselves are kept; the
|
|
216
|
-
* characters between them become spaces.
|
|
217
|
-
*/
|
|
218
|
-
function stripCommentsAndStrings(code) {
|
|
219
|
-
const output = code.split("");
|
|
220
|
-
let quote = null;
|
|
221
|
-
let lineComment = false;
|
|
222
|
-
let blockComment = false;
|
|
223
|
-
let escaped = false;
|
|
224
|
-
for (let index = 0; index < code.length; index++) {
|
|
225
|
-
const char = code[index];
|
|
226
|
-
const next = code[index + 1];
|
|
227
|
-
if (lineComment) {
|
|
228
|
-
if (char === "\n" || char === "\r")
|
|
229
|
-
lineComment = false;
|
|
230
|
-
else
|
|
231
|
-
output[index] = " ";
|
|
232
|
-
continue;
|
|
233
|
-
}
|
|
234
|
-
if (blockComment) {
|
|
235
|
-
if (char === "*" && next === "/") {
|
|
236
|
-
blockComment = false;
|
|
237
|
-
output[index] = " ";
|
|
238
|
-
output[index + 1] = " ";
|
|
239
|
-
index++;
|
|
240
|
-
}
|
|
241
|
-
else if (char !== "\n") {
|
|
242
|
-
output[index] = " ";
|
|
243
|
-
}
|
|
244
|
-
continue;
|
|
245
|
-
}
|
|
246
|
-
if (quote) {
|
|
247
|
-
if (escaped) {
|
|
248
|
-
escaped = false;
|
|
249
|
-
output[index] = " ";
|
|
250
|
-
continue;
|
|
251
|
-
}
|
|
252
|
-
if (char === "\\") {
|
|
253
|
-
escaped = true;
|
|
254
|
-
output[index] = " ";
|
|
255
|
-
continue;
|
|
256
|
-
}
|
|
257
|
-
if (char === quote) {
|
|
258
|
-
quote = null;
|
|
259
|
-
continue;
|
|
260
|
-
}
|
|
261
|
-
if (char !== "\n")
|
|
262
|
-
output[index] = " ";
|
|
263
|
-
continue;
|
|
264
|
-
}
|
|
265
|
-
if (char === "/" && next === "/") {
|
|
266
|
-
lineComment = true;
|
|
267
|
-
output[index] = " ";
|
|
268
|
-
continue;
|
|
269
|
-
}
|
|
270
|
-
if (char === "/" && next === "*") {
|
|
271
|
-
blockComment = true;
|
|
272
|
-
output[index] = " ";
|
|
273
|
-
continue;
|
|
274
|
-
}
|
|
275
|
-
if (char === "'" || char === '"' || char === "`") {
|
|
276
|
-
quote = char;
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
return output.join("");
|
|
280
|
-
}
|
|
281
|
-
function findServerFnExportReplacements(code, factoryNames) {
|
|
282
|
-
const replacements = [];
|
|
283
|
-
for (const match of code.matchAll(EXPORT_SERVER_FUNCTION_FACTORY_RE)) {
|
|
284
|
-
const exportName = match[1];
|
|
285
|
-
const calleeName = match[2];
|
|
286
|
-
const factory = calleeName ? factoryNames.get(calleeName) : undefined;
|
|
287
|
-
if (!exportName || !calleeName || !factory)
|
|
288
|
-
continue;
|
|
289
|
-
const openParenIndex = (match.index ?? 0) + match[0].length - 1;
|
|
290
|
-
const callEnd = findMatchingParen(code, openParenIndex);
|
|
291
|
-
if (callEnd === -1)
|
|
292
|
-
continue;
|
|
293
|
-
let declarationEnd = callEnd + 1;
|
|
294
|
-
while (/\s/.test(code[declarationEnd] ?? ""))
|
|
295
|
-
declarationEnd++;
|
|
296
|
-
if (code[declarationEnd] === ";")
|
|
297
|
-
declarationEnd++;
|
|
298
|
-
const calleeIndex = code.indexOf(calleeName, match.index);
|
|
299
|
-
replacements.push({
|
|
300
|
-
start: match.index ?? 0,
|
|
301
|
-
end: declarationEnd,
|
|
302
|
-
exportName,
|
|
303
|
-
privateName: `$$farm_server_${factory}_${exportName}`,
|
|
304
|
-
wrapperName: exportName,
|
|
305
|
-
callExpression: code.slice(calleeIndex, callEnd + 1),
|
|
306
|
-
});
|
|
307
|
-
}
|
|
308
|
-
// `export default createServerFn(...)` needs the same treatment. Without it the
|
|
309
|
-
// module gets no server boundary and its handler ships to the client.
|
|
310
|
-
for (const match of code.matchAll(EXPORT_DEFAULT_SERVER_FUNCTION_FACTORY_RE)) {
|
|
311
|
-
const calleeName = match[1];
|
|
312
|
-
const factory = calleeName ? factoryNames.get(calleeName) : undefined;
|
|
313
|
-
if (!calleeName || !factory)
|
|
314
|
-
continue;
|
|
315
|
-
const openParenIndex = (match.index ?? 0) + match[0].length - 1;
|
|
316
|
-
const callEnd = findMatchingParen(code, openParenIndex);
|
|
317
|
-
if (callEnd === -1)
|
|
318
|
-
continue;
|
|
319
|
-
let declarationEnd = callEnd + 1;
|
|
320
|
-
while (/\s/.test(code[declarationEnd] ?? ""))
|
|
321
|
-
declarationEnd++;
|
|
322
|
-
if (code[declarationEnd] === ";")
|
|
323
|
-
declarationEnd++;
|
|
324
|
-
const calleeIndex = code.indexOf(calleeName, match.index);
|
|
325
|
-
const privateName = `$$farm_server_${factory}_default`;
|
|
326
|
-
replacements.push({
|
|
327
|
-
start: match.index ?? 0,
|
|
328
|
-
end: declarationEnd,
|
|
329
|
-
exportName: "default",
|
|
330
|
-
privateName,
|
|
331
|
-
wrapperName: `${privateName}_export`,
|
|
332
|
-
callExpression: code.slice(calleeIndex, callEnd + 1),
|
|
333
|
-
});
|
|
334
|
-
}
|
|
335
|
-
return replacements;
|
|
336
|
-
}
|
|
337
|
-
/**
|
|
338
|
-
* The exported wrapper replaces the factory's function, so it would lose the internal descriptor
|
|
339
|
-
* that MCP and other non-browser callers read. Copy only that descriptor: the other server
|
|
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.
|
|
342
|
-
*/
|
|
343
|
-
function copyServerFunctionDescriptor(target, source, lazy) {
|
|
344
|
-
const key = 'Symbol.for("farm.server-function.descriptor")';
|
|
345
|
-
return lazy
|
|
346
|
-
? `Object.defineProperty(${target}, ${key}, { get: () => ${source}[${key}] });`
|
|
347
|
-
: `Object.defineProperty(${target}, ${key}, { value: ${source}[${key}] });`;
|
|
348
|
-
}
|
|
349
|
-
function hasModuleDirective(code, directive) {
|
|
350
|
-
for (const match of code.matchAll(DECLARATION_RE)) {
|
|
351
|
-
const value = match[2];
|
|
352
|
-
if (value === directive)
|
|
353
|
-
return true;
|
|
354
|
-
if (value !== "use strict" && value !== "use client" && value !== "use server")
|
|
355
|
-
return false;
|
|
356
|
-
}
|
|
357
|
-
return false;
|
|
358
|
-
}
|
|
359
|
-
function findMatchingParen(code, openParenIndex) {
|
|
360
|
-
let depth = 0;
|
|
361
|
-
let quote = null;
|
|
362
|
-
let lineComment = false;
|
|
363
|
-
let blockComment = false;
|
|
364
|
-
let escaped = false;
|
|
365
|
-
for (let index = openParenIndex; index < code.length; index++) {
|
|
366
|
-
const char = code[index];
|
|
367
|
-
const next = code[index + 1];
|
|
368
|
-
if (lineComment) {
|
|
369
|
-
if (char === "\n" || char === "\r")
|
|
370
|
-
lineComment = false;
|
|
371
|
-
continue;
|
|
372
|
-
}
|
|
373
|
-
if (blockComment) {
|
|
374
|
-
if (char === "*" && next === "/") {
|
|
375
|
-
blockComment = false;
|
|
376
|
-
index++;
|
|
377
|
-
}
|
|
378
|
-
continue;
|
|
379
|
-
}
|
|
380
|
-
if (quote) {
|
|
381
|
-
if (escaped) {
|
|
382
|
-
escaped = false;
|
|
383
|
-
continue;
|
|
384
|
-
}
|
|
385
|
-
if (char === "\\") {
|
|
386
|
-
escaped = true;
|
|
387
|
-
continue;
|
|
388
|
-
}
|
|
389
|
-
if (char === quote) {
|
|
390
|
-
quote = null;
|
|
391
|
-
}
|
|
392
|
-
continue;
|
|
393
|
-
}
|
|
394
|
-
if (char === "/" && next === "/") {
|
|
395
|
-
lineComment = true;
|
|
396
|
-
index++;
|
|
397
|
-
continue;
|
|
398
|
-
}
|
|
399
|
-
if (char === "/" && next === "*") {
|
|
400
|
-
blockComment = true;
|
|
401
|
-
index++;
|
|
402
|
-
continue;
|
|
403
|
-
}
|
|
404
|
-
if (char === "'" || char === '"' || char === "`") {
|
|
405
|
-
quote = char;
|
|
406
|
-
continue;
|
|
407
|
-
}
|
|
408
|
-
if (char === "(")
|
|
409
|
-
depth++;
|
|
410
|
-
if (char === ")") {
|
|
411
|
-
depth--;
|
|
412
|
-
if (depth === 0)
|
|
413
|
-
return index;
|
|
414
|
-
}
|
|
415
|
-
}
|
|
416
|
-
return -1;
|
|
417
|
-
}
|
|
1
|
+
// Server functions become "use server" boundaries in both engines: the transform lives in core,
|
|
2
|
+
// which islands mode runs without this plugin.
|
|
3
|
+
export * from "@farm.js/core/internal/server-fn-transform";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@farm.js/plugin",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.51-beta.0",
|
|
4
4
|
"description": "Official plugins for Farm.js framework",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Farm.js Team",
|
|
@@ -63,7 +63,7 @@
|
|
|
63
63
|
"nitro": "3.0.260903-beta",
|
|
64
64
|
"picocolors": "^1.0.0",
|
|
65
65
|
"rsc-html-stream": "^0.0.4",
|
|
66
|
-
"@farm.js/core": "0.1.
|
|
66
|
+
"@farm.js/core": "0.1.51-beta.0"
|
|
67
67
|
},
|
|
68
68
|
"devDependencies": {
|
|
69
69
|
"@types/node": "^22.20.5",
|
|
@@ -84,7 +84,7 @@
|
|
|
84
84
|
"react-dom": "19.2.8",
|
|
85
85
|
"react-server-dom-webpack": "19.2.8",
|
|
86
86
|
"vite": ">=6.0.0",
|
|
87
|
-
"@farm.js/core": "0.1.
|
|
87
|
+
"@farm.js/core": "0.1.51-beta.0"
|
|
88
88
|
},
|
|
89
89
|
"peerDependenciesMeta": {
|
|
90
90
|
"@vitejs/plugin-rsc": {
|