@farm.js/plugin 0.1.0-beta.99 → 0.1.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 +2 -2
- package/dist/api/index.d.ts +2 -0
- package/dist/api/index.d.ts.map +1 -1
- package/dist/api/index.js +43 -31
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/middleware/index.d.ts +2 -3
- package/dist/middleware/index.d.ts.map +1 -1
- package/dist/middleware/index.js +44 -10
- package/dist/middleware/matching.d.ts +8 -3
- package/dist/middleware/matching.d.ts.map +1 -1
- package/dist/middleware/matching.js +15 -7
- package/dist/observability/index.d.ts.map +1 -1
- package/dist/observability/index.js +7 -2
- package/dist/rsc/entries/client.d.ts.map +1 -1
- package/dist/rsc/entries/client.js +37 -2
- package/dist/rsc/entries/rsc.d.ts.map +1 -1
- package/dist/rsc/entries/rsc.js +77 -22
- package/dist/rsc/index.d.ts +2 -0
- package/dist/rsc/index.d.ts.map +1 -1
- package/dist/rsc/index.js +163 -30
- package/dist/rsc/middleware-config-module.d.ts +19 -0
- package/dist/rsc/middleware-config-module.d.ts.map +1 -0
- package/dist/rsc/middleware-config-module.js +355 -0
- package/dist/rsc/nitro-build.d.ts +2 -0
- package/dist/rsc/nitro-build.d.ts.map +1 -1
- package/dist/rsc/nitro-build.js +64 -39
- package/dist/rsc/server-fn-transform.d.ts.map +1 -1
- package/dist/rsc/server-fn-transform.js +177 -0
- package/dist/rsc/types.d.ts +20 -0
- package/dist/rsc/types.d.ts.map +1 -1
- package/dist/rsc/vite-plugin-nitro.d.ts +20 -7
- package/dist/rsc/vite-plugin-nitro.d.ts.map +1 -1
- package/dist/rsc/vite-plugin-nitro.js +51 -66
- package/package.json +5 -4
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
const SERVER_FUNCTION_FACTORY_IMPORT_RE = /\bimport\s+(?:type\s+)?(?:[A-Za-z_$][\w$]*\s*,\s*)?(\{[^}]*\})\s+from\s*["']@farm.js\/core(?:\/(?:server-fn|server-query))?["']\s*;?/g;
|
|
2
2
|
const EXPORT_SERVER_FUNCTION_FACTORY_RE = /\bexport\s+(?:const|let|var)\s+([A-Za-z_$][\w$]*)\s*=\s*([A-Za-z_$][\w$]*)\s*(?:<[^<>]*(?:<[^<>]*>[^<>]*)*>)?\s*\(/g;
|
|
3
|
+
const EXPORT_DEFAULT_SERVER_FUNCTION_FACTORY_RE = /\bexport\s+default\s+([A-Za-z_$][\w$]*)\s*(?:<[^<>]*(?:<[^<>]*>[^<>]*)*>)?\s*\(/g;
|
|
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;
|
|
3
11
|
const MODULE_EXT_RE = /\.[cm]?[jt]sx?$/;
|
|
4
12
|
const DECLARATION_RE = /(^|\n)\s*["']([^"']+)["']\s*;?/g;
|
|
5
13
|
export function transformFarmServerFns(code, id) {
|
|
@@ -10,6 +18,20 @@ export function transformFarmServerFns(code, id) {
|
|
|
10
18
|
const factoryNames = findServerFunctionFactoryImportNames(code);
|
|
11
19
|
if (factoryNames.size === 0)
|
|
12
20
|
return null;
|
|
21
|
+
const reExport = findServerFnReExport(code, factoryNames);
|
|
22
|
+
if (reExport) {
|
|
23
|
+
const factoryName = reExport.factory === "fn" ? "createServerFn" : "createServerQuery";
|
|
24
|
+
const escape = reExport.form === "clause"
|
|
25
|
+
? "with an export clause"
|
|
26
|
+
: reExport.form === "default"
|
|
27
|
+
? "as the default export"
|
|
28
|
+
: "through another exported binding";
|
|
29
|
+
const inlineForm = reExport.form === "default"
|
|
30
|
+
? `export default ${factoryName}(...)`
|
|
31
|
+
: `export const ${reExport.exportName} = ${factoryName}(...)`;
|
|
32
|
+
throw new Error(`Server function "${reExport.localName}" cannot be re-exported ${escape}. ` +
|
|
33
|
+
`Export it inline instead: ${inlineForm}.`);
|
|
34
|
+
}
|
|
13
35
|
const replacements = findServerFnExportReplacements(code, factoryNames);
|
|
14
36
|
if (replacements.length === 0)
|
|
15
37
|
return null;
|
|
@@ -52,6 +74,135 @@ function findServerFunctionFactoryImportNames(code) {
|
|
|
52
74
|
}
|
|
53
75
|
return names;
|
|
54
76
|
}
|
|
77
|
+
function findServerFnReExport(code, factoryNames) {
|
|
78
|
+
// Comments and string contents must not hide or fake an export: a comment
|
|
79
|
+
// inside `export { /* … */ save }` previously made the specifier unparsable
|
|
80
|
+
// and the module shipped untransformed.
|
|
81
|
+
const scannable = stripCommentsAndStrings(code);
|
|
82
|
+
const declarations = new Map();
|
|
83
|
+
for (const match of scannable.matchAll(SERVER_FUNCTION_DECLARATION_RE)) {
|
|
84
|
+
const localName = match[1];
|
|
85
|
+
const factoryName = match[2];
|
|
86
|
+
const factory = factoryName ? factoryNames.get(factoryName) : undefined;
|
|
87
|
+
if (localName && factory)
|
|
88
|
+
declarations.set(localName, factory);
|
|
89
|
+
}
|
|
90
|
+
if (declarations.size === 0)
|
|
91
|
+
return null;
|
|
92
|
+
for (const match of scannable.matchAll(LOCAL_EXPORT_CLAUSE_RE)) {
|
|
93
|
+
const clause = match[1] ?? "";
|
|
94
|
+
const clauseEnd = (match.index ?? 0) + match[0].length;
|
|
95
|
+
if (scannable.slice(clauseEnd).trimStart().startsWith("from"))
|
|
96
|
+
continue;
|
|
97
|
+
for (const specifier of clause.split(",")) {
|
|
98
|
+
const parts = specifier.trim().split(/\s+as\s+/);
|
|
99
|
+
const localName = parts[0]?.trim();
|
|
100
|
+
if (!localName || !declarations.has(localName))
|
|
101
|
+
continue;
|
|
102
|
+
return {
|
|
103
|
+
localName,
|
|
104
|
+
exportName: parts[1]?.trim() || localName,
|
|
105
|
+
factory: declarations.get(localName),
|
|
106
|
+
form: "clause",
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
// `export default save;` — the export-default replacement only recognizes a
|
|
111
|
+
// direct factory call, so a bare identifier slipped through untransformed.
|
|
112
|
+
for (const match of scannable.matchAll(EXPORT_DEFAULT_ALIAS_RE)) {
|
|
113
|
+
const localName = match[1];
|
|
114
|
+
if (!localName || !declarations.has(localName))
|
|
115
|
+
continue;
|
|
116
|
+
return {
|
|
117
|
+
localName,
|
|
118
|
+
exportName: "default",
|
|
119
|
+
factory: declarations.get(localName),
|
|
120
|
+
form: "default",
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
// `export const persist = save;` — same escape through a renamed binding.
|
|
124
|
+
for (const match of scannable.matchAll(EXPORT_BINDING_ALIAS_RE)) {
|
|
125
|
+
const exportName = match[1];
|
|
126
|
+
const localName = match[2];
|
|
127
|
+
if (!exportName || !localName || !declarations.has(localName))
|
|
128
|
+
continue;
|
|
129
|
+
return {
|
|
130
|
+
localName,
|
|
131
|
+
exportName,
|
|
132
|
+
factory: declarations.get(localName),
|
|
133
|
+
form: "binding",
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Blank out comments and string/template contents, preserving length, so the
|
|
140
|
+
* export scans above see only real syntax. Quotes themselves are kept; the
|
|
141
|
+
* characters between them become spaces.
|
|
142
|
+
*/
|
|
143
|
+
function stripCommentsAndStrings(code) {
|
|
144
|
+
const output = code.split("");
|
|
145
|
+
let quote = null;
|
|
146
|
+
let lineComment = false;
|
|
147
|
+
let blockComment = false;
|
|
148
|
+
let escaped = false;
|
|
149
|
+
for (let index = 0; index < code.length; index++) {
|
|
150
|
+
const char = code[index];
|
|
151
|
+
const next = code[index + 1];
|
|
152
|
+
if (lineComment) {
|
|
153
|
+
if (char === "\n" || char === "\r")
|
|
154
|
+
lineComment = false;
|
|
155
|
+
else
|
|
156
|
+
output[index] = " ";
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
if (blockComment) {
|
|
160
|
+
if (char === "*" && next === "/") {
|
|
161
|
+
blockComment = false;
|
|
162
|
+
output[index] = " ";
|
|
163
|
+
output[index + 1] = " ";
|
|
164
|
+
index++;
|
|
165
|
+
}
|
|
166
|
+
else if (char !== "\n") {
|
|
167
|
+
output[index] = " ";
|
|
168
|
+
}
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
if (quote) {
|
|
172
|
+
if (escaped) {
|
|
173
|
+
escaped = false;
|
|
174
|
+
output[index] = " ";
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
if (char === "\\") {
|
|
178
|
+
escaped = true;
|
|
179
|
+
output[index] = " ";
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
if (char === quote) {
|
|
183
|
+
quote = null;
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
if (char !== "\n")
|
|
187
|
+
output[index] = " ";
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
if (char === "/" && next === "/") {
|
|
191
|
+
lineComment = true;
|
|
192
|
+
output[index] = " ";
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
if (char === "/" && next === "*") {
|
|
196
|
+
blockComment = true;
|
|
197
|
+
output[index] = " ";
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
if (char === "'" || char === '"' || char === "`") {
|
|
201
|
+
quote = char;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return output.join("");
|
|
205
|
+
}
|
|
55
206
|
function findServerFnExportReplacements(code, factoryNames) {
|
|
56
207
|
const replacements = [];
|
|
57
208
|
for (const match of code.matchAll(EXPORT_SERVER_FUNCTION_FACTORY_RE)) {
|
|
@@ -79,6 +230,32 @@ function findServerFnExportReplacements(code, factoryNames) {
|
|
|
79
230
|
code: `const ${privateName} = ${callExpression};\nexport async function ${exportName}(input) {\n return ${privateName}(input);\n}`,
|
|
80
231
|
});
|
|
81
232
|
}
|
|
233
|
+
// `export default createServerFn(...)` needs the same treatment. Without it the
|
|
234
|
+
// module gets no "use server" directive and its handler ships to the client.
|
|
235
|
+
for (const match of code.matchAll(EXPORT_DEFAULT_SERVER_FUNCTION_FACTORY_RE)) {
|
|
236
|
+
const calleeName = match[1];
|
|
237
|
+
const factory = calleeName ? factoryNames.get(calleeName) : undefined;
|
|
238
|
+
if (!calleeName || !factory)
|
|
239
|
+
continue;
|
|
240
|
+
const openParenIndex = (match.index ?? 0) + match[0].length - 1;
|
|
241
|
+
const callEnd = findMatchingParen(code, openParenIndex);
|
|
242
|
+
if (callEnd === -1)
|
|
243
|
+
continue;
|
|
244
|
+
let declarationEnd = callEnd + 1;
|
|
245
|
+
while (/\s/.test(code[declarationEnd] ?? ""))
|
|
246
|
+
declarationEnd++;
|
|
247
|
+
if (code[declarationEnd] === ";")
|
|
248
|
+
declarationEnd++;
|
|
249
|
+
const calleeIndex = code.indexOf(calleeName, match.index);
|
|
250
|
+
const callExpression = code.slice(calleeIndex, callEnd + 1);
|
|
251
|
+
const privateName = `$$farm_server_${factory}_default`;
|
|
252
|
+
replacements.push({
|
|
253
|
+
start: match.index ?? 0,
|
|
254
|
+
end: declarationEnd,
|
|
255
|
+
exportName: "default",
|
|
256
|
+
code: `const ${privateName} = ${callExpression};\nexport default async function (input) {\n return ${privateName}(input);\n}`,
|
|
257
|
+
});
|
|
258
|
+
}
|
|
82
259
|
return replacements;
|
|
83
260
|
}
|
|
84
261
|
function hasModuleDirective(code, directive) {
|
package/dist/rsc/types.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ResolvedFarmI18nConfig } from "@farm.js/core/i18n";
|
|
1
2
|
import type { FarmServerActionsConfig, ResolvedFarmServerActionsConfig } from "@farm.js/core/server-action-security";
|
|
2
3
|
/**
|
|
3
4
|
* Configuration options for the Farm.js RSC plugin.
|
|
@@ -72,6 +73,25 @@ export interface EntryContext {
|
|
|
72
73
|
serverActions: ResolvedFarmServerActionsConfig;
|
|
73
74
|
/** Resolved build identifier embedded into the server and browser entries. */
|
|
74
75
|
deploymentId: string;
|
|
76
|
+
/**
|
|
77
|
+
* Resolved i18n configuration. Middleware path matching runs against the
|
|
78
|
+
* locale-stripped pathname, so without this a `/de/dashboard` request never
|
|
79
|
+
* matches a `/dashboard` middleware.
|
|
80
|
+
*/
|
|
81
|
+
i18n?: ResolvedFarmI18nConfig;
|
|
82
|
+
/**
|
|
83
|
+
* Server settings the request path needs. `trustProxy` decides whether
|
|
84
|
+
* forwarded headers are believed when middleware reads the client address.
|
|
85
|
+
*/
|
|
86
|
+
server?: {
|
|
87
|
+
trustProxy: boolean;
|
|
88
|
+
};
|
|
89
|
+
/**
|
|
90
|
+
* Ordered config modules that can contribute live middleware handlers.
|
|
91
|
+
* Layer configs come first and the application config comes last, matching
|
|
92
|
+
* Farm's normal config merge order.
|
|
93
|
+
*/
|
|
94
|
+
middlewareConfigPaths?: string[];
|
|
75
95
|
/** Whether debug mode is enabled */
|
|
76
96
|
debug: boolean;
|
|
77
97
|
/** True only for the Vite serve command, never for a production build. */
|
package/dist/rsc/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/rsc/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,uBAAuB,EACvB,+BAA+B,EAChC,MAAM,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;KACf,CAAC;IAEF,6CAA6C;IAC7C,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB;AAED;;GAEG;AACH,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,UAAU;QAClB,QAAQ,CAAC,OAAO,EAAE;YAChB,qCAAqC;YACrC,OAAO,IAAI,KAAK,CAAC,YAAY,EAAE,CAAC;YAEhC,6CAA6C;YAC7C,UAAU,CAAC,CAAC,GAAG,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;YAEhE,kDAAkD;YAClD,0BAA0B,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;SAC5D,CAAC;KACH;CACF"}
|
|
@@ -1,15 +1,28 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Vite plugin
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
2
|
+
* Vite plugin for the Nitro RSC deployment pipeline. It forces per-environment
|
|
3
|
+
* output dirs (`Step 1`), captures the server (RSC) Rollup bundle in `writeBundle`
|
|
4
|
+
* (`Step 2`), and exposes `runNitroFromBuildApp()`, which builds Nitro from the
|
|
5
|
+
* captured outputs once all environments have finished writing.
|
|
6
|
+
*
|
|
7
|
+
* Farm's RSC builder invokes `runNitroFromBuildApp()` after every environment
|
|
8
|
+
* has written its output, so a single `vite build` still produces the
|
|
9
|
+
* deployment bundle.
|
|
10
|
+
*
|
|
11
|
+
* The plugin no longer triggers Nitro itself from a Rollup hook: the previous
|
|
12
|
+
* `buildEnd` trigger fired before its own environment's `prepareOutDir`/`writeBundle`,
|
|
13
|
+
* so on a rebuild it observed the previous build's stale outputs and built Nitro
|
|
14
|
+
* against them (and set `__FARM_NITRO_PLUGIN_RAN`, suppressing the proper run).
|
|
15
|
+
* Removing it keeps Nitro from running against stale artifacts while the post-app
|
|
16
|
+
* hook preserves the one-command build contract.
|
|
17
|
+
*
|
|
18
|
+
* - Step 1: Force per-environment output dirs to `.nitro/vite/dist/${name}` so Nitro can locate artifacts.
|
|
19
|
+
* - Step 2: Capture the server (RSC) Rollup bundle in `writeBundle`.
|
|
8
20
|
*
|
|
9
21
|
* @example
|
|
10
22
|
* ```ts
|
|
11
|
-
* import nitro from '@farm.js/plugin/rsc
|
|
23
|
+
* import { defineConfig, nitro } from '@farm.js/plugin/rsc'
|
|
12
24
|
* export default defineConfig({
|
|
25
|
+
* srcDir: 'src',
|
|
13
26
|
* plugins: [rsc(), react(), nitro({ server: { environmentName: 'rsc' }, config: { preset: 'vercel' } })],
|
|
14
27
|
* })
|
|
15
28
|
* ```
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vite-plugin-nitro.d.ts","sourceRoot":"","sources":["../../src/rsc/vite-plugin-nitro.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"vite-plugin-nitro.d.ts","sourceRoot":"","sources":["../../src/rsc/vite-plugin-nitro.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAenC,MAAM,WAAW,kBAAkB;IACjC,MAAM,CAAC,EAAE;QACP,eAAe,CAAC,EAAE,MAAM,CAAC;QACzB,4DAA4D;QAC5D,SAAS,CAAC,EAAE,MAAM,CAAC;KACpB,CAAC;IACF,MAAM,CAAC,EAAE;QACP,MAAM,CAAC,EAAE,MAAM,CAAC;KACjB,CAAC;IACF,kEAAkE;IAClE,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAID,MAAM,CAAC,OAAO,UAAU,KAAK,CAAC,OAAO,GAAE,kBAAuB,GAAG,MAAM,CAqEtE;AAED,kFAAkF;AAClF,wBAAsB,oBAAoB,IAAI,OAAO,CAAC,IAAI,CAAC,CAkC1D"}
|
|
@@ -1,15 +1,28 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Vite plugin
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
2
|
+
* Vite plugin for the Nitro RSC deployment pipeline. It forces per-environment
|
|
3
|
+
* output dirs (`Step 1`), captures the server (RSC) Rollup bundle in `writeBundle`
|
|
4
|
+
* (`Step 2`), and exposes `runNitroFromBuildApp()`, which builds Nitro from the
|
|
5
|
+
* captured outputs once all environments have finished writing.
|
|
6
|
+
*
|
|
7
|
+
* Farm's RSC builder invokes `runNitroFromBuildApp()` after every environment
|
|
8
|
+
* has written its output, so a single `vite build` still produces the
|
|
9
|
+
* deployment bundle.
|
|
10
|
+
*
|
|
11
|
+
* The plugin no longer triggers Nitro itself from a Rollup hook: the previous
|
|
12
|
+
* `buildEnd` trigger fired before its own environment's `prepareOutDir`/`writeBundle`,
|
|
13
|
+
* so on a rebuild it observed the previous build's stale outputs and built Nitro
|
|
14
|
+
* against them (and set `__FARM_NITRO_PLUGIN_RAN`, suppressing the proper run).
|
|
15
|
+
* Removing it keeps Nitro from running against stale artifacts while the post-app
|
|
16
|
+
* hook preserves the one-command build contract.
|
|
17
|
+
*
|
|
18
|
+
* - Step 1: Force per-environment output dirs to `.nitro/vite/dist/${name}` so Nitro can locate artifacts.
|
|
19
|
+
* - Step 2: Capture the server (RSC) Rollup bundle in `writeBundle`.
|
|
8
20
|
*
|
|
9
21
|
* @example
|
|
10
22
|
* ```ts
|
|
11
|
-
* import nitro from '@farm.js/plugin/rsc
|
|
23
|
+
* import { defineConfig, nitro } from '@farm.js/plugin/rsc'
|
|
12
24
|
* export default defineConfig({
|
|
25
|
+
* srcDir: 'src',
|
|
13
26
|
* plugins: [rsc(), react(), nitro({ server: { environmentName: 'rsc' }, config: { preset: 'vercel' } })],
|
|
14
27
|
* })
|
|
15
28
|
* ```
|
|
@@ -18,9 +31,6 @@ import path from "path";
|
|
|
18
31
|
import { buildRscNitro } from "./nitro-build.js";
|
|
19
32
|
import { waitForRscOutputs } from "./nitro-build.js";
|
|
20
33
|
import { resolveRscBuildOutputPath } from "./build-paths.js";
|
|
21
|
-
let nitroRunScheduled = false;
|
|
22
|
-
/** Stored RSC (server) bundle from writeBundle so we can find the entry chunk. */
|
|
23
|
-
let serverBundle = null;
|
|
24
34
|
const NITRO_VITE_DIST = ".nitro/vite/dist";
|
|
25
35
|
export default function nitro(options = {}) {
|
|
26
36
|
const serverEnvName = options.server?.environmentName ?? "rsc";
|
|
@@ -29,12 +39,15 @@ export default function nitro(options = {}) {
|
|
|
29
39
|
let rscOutDir = path.join(NITRO_VITE_DIST, "rsc");
|
|
30
40
|
let ssrOutDir = path.join(NITRO_VITE_DIST, "ssr");
|
|
31
41
|
let clientOutDir = path.join(NITRO_VITE_DIST, "client");
|
|
32
|
-
let
|
|
42
|
+
let clientAssetsDir = "assets";
|
|
33
43
|
return {
|
|
34
44
|
name: "vite-plugin-nitro",
|
|
35
45
|
apply: "build",
|
|
36
46
|
// Step 1: Force per-environment output dirs so Nitro can locate artifacts.
|
|
37
47
|
config() {
|
|
48
|
+
delete globalThis.__FARM_NITRO_SERVER_BUNDLE;
|
|
49
|
+
delete globalThis.__FARM_NITRO_PATHS;
|
|
50
|
+
delete globalThis.__FARM_NITRO_BUILD_PROMISE;
|
|
38
51
|
return {
|
|
39
52
|
environments: {
|
|
40
53
|
rsc: { build: { outDir: path.join(NITRO_VITE_DIST, "rsc") } },
|
|
@@ -50,20 +63,23 @@ export default function nitro(options = {}) {
|
|
|
50
63
|
rscOutDir = envs.rsc.build.outDir;
|
|
51
64
|
ssrOutDir = envs.ssr.build.outDir;
|
|
52
65
|
clientOutDir = envs.client.build.outDir;
|
|
53
|
-
const dir = path.dirname(rscOutDir);
|
|
54
|
-
baseOutDir = path.isAbsolute(dir) ? path.relative(resolvedRoot, dir) || "." : dir;
|
|
55
66
|
}
|
|
56
67
|
else {
|
|
57
|
-
baseOutDir = options.outDir ?? "dist";
|
|
68
|
+
const baseOutDir = options.outDir ?? "dist";
|
|
58
69
|
rscOutDir = path.join(baseOutDir, "rsc");
|
|
59
70
|
ssrOutDir = path.join(baseOutDir, "ssr");
|
|
60
71
|
clientOutDir = path.join(baseOutDir, "client");
|
|
61
72
|
}
|
|
73
|
+
const clientBuildAssetsDir = envs?.client?.build?.assetsDir;
|
|
74
|
+
clientAssetsDir =
|
|
75
|
+
typeof clientBuildAssetsDir === "string" && clientBuildAssetsDir
|
|
76
|
+
? clientBuildAssetsDir
|
|
77
|
+
: "assets";
|
|
62
78
|
},
|
|
63
79
|
// Step 2: Capture the server (RSC) Rollup bundle when that environment writes.
|
|
64
80
|
writeBundle(_options, bundle) {
|
|
65
81
|
if (this.environment?.name === serverEnvName) {
|
|
66
|
-
|
|
82
|
+
delete globalThis.__FARM_NITRO_BUILD_PROMISE;
|
|
67
83
|
globalThis.__FARM_NITRO_SERVER_BUNDLE = bundle;
|
|
68
84
|
globalThis.__FARM_NITRO_PATHS = {
|
|
69
85
|
root: resolvedRoot,
|
|
@@ -72,59 +88,10 @@ export default function nitro(options = {}) {
|
|
|
72
88
|
clientOutDir,
|
|
73
89
|
serverEntryName,
|
|
74
90
|
preset: options.config?.preset ?? process.env.NITRO_PRESET ?? "vercel",
|
|
91
|
+
clientAssetsDir,
|
|
75
92
|
};
|
|
76
93
|
}
|
|
77
94
|
},
|
|
78
|
-
// Run Nitro once all env outputs exist (after step 5). buildEnd runs after each env; only run when all exist.
|
|
79
|
-
async buildEnd() {
|
|
80
|
-
if (nitroRunScheduled)
|
|
81
|
-
return;
|
|
82
|
-
const root = path.resolve(resolvedRoot);
|
|
83
|
-
const { existsSync } = await import("fs");
|
|
84
|
-
await new Promise((r) => setTimeout(r, 800));
|
|
85
|
-
const baseOutDir = rscOutDir.includes(".nitro/vite/dist")
|
|
86
|
-
? NITRO_VITE_DIST
|
|
87
|
-
: path.isAbsolute(path.dirname(rscOutDir))
|
|
88
|
-
? path.relative(root, path.dirname(rscOutDir)) || "."
|
|
89
|
-
: path.dirname(rscOutDir);
|
|
90
|
-
const rscOk = existsSync(resolveRscBuildOutputPath(root, baseOutDir, "rsc", "index.js"));
|
|
91
|
-
const ssrOk = existsSync(resolveRscBuildOutputPath(root, baseOutDir, "ssr", "index.js"));
|
|
92
|
-
const clientOk = existsSync(resolveRscBuildOutputPath(root, baseOutDir, "client", "assets"));
|
|
93
|
-
if (!rscOk || !ssrOk || !clientOk)
|
|
94
|
-
return;
|
|
95
|
-
nitroRunScheduled = true;
|
|
96
|
-
const preset = options.config?.preset ?? process.env.NITRO_PRESET ?? "vercel";
|
|
97
|
-
const run = async () => {
|
|
98
|
-
await waitForRscOutputs(root, baseOutDir, { timeoutMs: 20_000 }).catch(() => {
|
|
99
|
-
// Outputs already verified; proceed (nitro-build stubs manifest if missing)
|
|
100
|
-
});
|
|
101
|
-
let rendererPath = resolveRscBuildOutputPath(root, rscOutDir, "index.js");
|
|
102
|
-
if (serverBundle) {
|
|
103
|
-
const serverEntryChunks = [];
|
|
104
|
-
for (const chunk of Object.values(serverBundle)) {
|
|
105
|
-
if (chunk.type === "chunk" && chunk.isEntry && chunk.fileName) {
|
|
106
|
-
serverEntryChunks.push(chunk);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
const selected = serverEntryName
|
|
110
|
-
? serverEntryChunks.find((c) => (c.name ?? c.fileName) === serverEntryName)
|
|
111
|
-
: serverEntryChunks[0];
|
|
112
|
-
if (selected?.fileName) {
|
|
113
|
-
rendererPath = resolveRscBuildOutputPath(root, rscOutDir, selected.fileName);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
await buildRscNitro({
|
|
117
|
-
root,
|
|
118
|
-
rendererPath,
|
|
119
|
-
publicDir: resolveRscBuildOutputPath(root, clientOutDir),
|
|
120
|
-
ssrPath: resolveRscBuildOutputPath(root, ssrOutDir, "index.js"),
|
|
121
|
-
assetsDir: undefined,
|
|
122
|
-
preset,
|
|
123
|
-
});
|
|
124
|
-
globalThis.__FARM_NITRO_PLUGIN_RAN = true;
|
|
125
|
-
};
|
|
126
|
-
await run();
|
|
127
|
-
},
|
|
128
95
|
};
|
|
129
96
|
}
|
|
130
97
|
/** Called by RSC plugin buildApp (post) to run Nitro with the captured bundle. */
|
|
@@ -133,6 +100,24 @@ export async function runNitroFromBuildApp() {
|
|
|
133
100
|
const bundle = globalThis.__FARM_NITRO_SERVER_BUNDLE;
|
|
134
101
|
if (!paths)
|
|
135
102
|
return;
|
|
103
|
+
const existing = globalThis.__FARM_NITRO_BUILD_PROMISE;
|
|
104
|
+
if (existing) {
|
|
105
|
+
await existing;
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
const run = runCapturedNitroBuild(paths, bundle);
|
|
109
|
+
globalThis.__FARM_NITRO_BUILD_PROMISE = run;
|
|
110
|
+
try {
|
|
111
|
+
await run;
|
|
112
|
+
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
if (globalThis.__FARM_NITRO_BUILD_PROMISE === run) {
|
|
115
|
+
delete globalThis.__FARM_NITRO_BUILD_PROMISE;
|
|
116
|
+
}
|
|
117
|
+
throw error;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
async function runCapturedNitroBuild(paths, bundle) {
|
|
136
121
|
const root = paths.root;
|
|
137
122
|
const preset = paths.preset;
|
|
138
123
|
const baseOutDir = path.dirname(paths.rscOutDir);
|
|
@@ -157,7 +142,7 @@ export async function runNitroFromBuildApp() {
|
|
|
157
142
|
rendererPath,
|
|
158
143
|
publicDir: resolveRscBuildOutputPath(root, paths.clientOutDir),
|
|
159
144
|
ssrPath: resolveRscBuildOutputPath(root, paths.ssrOutDir, "index.js"),
|
|
160
|
-
assetsDir:
|
|
145
|
+
assetsDir: paths.clientAssetsDir ?? "assets",
|
|
161
146
|
preset,
|
|
162
147
|
});
|
|
163
148
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@farm.js/plugin",
|
|
3
|
-
"version": "0.1.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"description": "Official plugins for Farm.js framework",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Farm.js Team",
|
|
@@ -58,10 +58,11 @@
|
|
|
58
58
|
"@farming-labs/strata": "0.1.2",
|
|
59
59
|
"better-call": "^1.0.19",
|
|
60
60
|
"es-module-lexer": "2.0.0",
|
|
61
|
-
"
|
|
61
|
+
"h3": "2.0.1-rc.32",
|
|
62
|
+
"nitro": "3.0.260903-beta",
|
|
62
63
|
"picocolors": "^1.0.0",
|
|
63
64
|
"rsc-html-stream": "^0.0.4",
|
|
64
|
-
"@farm.js/core": "0.1.0
|
|
65
|
+
"@farm.js/core": "0.1.0"
|
|
65
66
|
},
|
|
66
67
|
"devDependencies": {
|
|
67
68
|
"@types/node": "^20.19.21",
|
|
@@ -82,7 +83,7 @@
|
|
|
82
83
|
"react-dom": "19.2.8",
|
|
83
84
|
"react-server-dom-webpack": "19.2.8",
|
|
84
85
|
"vite": ">=6.0.0",
|
|
85
|
-
"@farm.js/core": "0.1.0
|
|
86
|
+
"@farm.js/core": "0.1.0"
|
|
86
87
|
},
|
|
87
88
|
"peerDependenciesMeta": {
|
|
88
89
|
"@vitejs/plugin-rsc": {
|