@farm.js/plugin 0.1.61 → 0.1.63
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 +567 -47
- package/dist/rsc/entries/ssr.d.ts.map +1 -1
- package/dist/rsc/entries/ssr.js +27 -10
- package/dist/rsc/index.d.ts +38 -3
- package/dist/rsc/index.d.ts.map +1 -1
- package/dist/rsc/index.js +300 -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
|
@@ -14,6 +14,9 @@ type ExtractionResult = {
|
|
|
14
14
|
reason: string;
|
|
15
15
|
};
|
|
16
16
|
export declare function extractRscMiddlewareConfigModule(source: string, program: AstNode): ExtractionResult;
|
|
17
|
+
/** Extracts `key` from the default-exported config, as a module that exports `{ [key] }`. */
|
|
18
|
+
export declare function extractRscConfigPropertyModule(source: string, program: AstNode, key: "middleware" | "mcp"): ExtractionResult;
|
|
17
19
|
export declare function loadRscMiddlewareConfigModule(configPath: string): Promise<ExtractionResult>;
|
|
20
|
+
export declare function loadRscConfigPropertyModule(configPath: string, key: "middleware" | "mcp"): Promise<ExtractionResult>;
|
|
18
21
|
export {};
|
|
19
22
|
//# sourceMappingURL=middleware-config-module.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"middleware-config-module.d.ts","sourceRoot":"","sources":["../../src/rsc/middleware-config-module.ts"],"names":[],"mappings":"AAQA,KAAK,OAAO,GAAG;IACb,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;IACZ,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB,CAAC;AASF,KAAK,gBAAgB,GACjB;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAChC;IAAE,IAAI,EAAE,QAAQ,CAAA;CAAE,GAClB;IAAE,IAAI,EAAE,aAAa,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;
|
|
1
|
+
{"version":3,"file":"middleware-config-module.d.ts","sourceRoot":"","sources":["../../src/rsc/middleware-config-module.ts"],"names":[],"mappings":"AAQA,KAAK,OAAO,GAAG;IACb,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;IACZ,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB,CAAC;AASF,KAAK,gBAAgB,GACjB;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAChC;IAAE,IAAI,EAAE,QAAQ,CAAA;CAAE,GAClB;IAAE,IAAI,EAAE,aAAa,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AAgQ5C,wBAAgB,gCAAgC,CAC9C,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,OAAO,GACf,gBAAgB,CAElB;AAED,6FAA6F;AAC7F,wBAAgB,8BAA8B,CAC5C,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,OAAO,EAChB,GAAG,EAAE,YAAY,GAAG,KAAK,GACxB,gBAAgB,CA+IlB;AAiBD,wBAAsB,6BAA6B,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAEjG;AAED,wBAAsB,2BAA2B,CAC/C,UAAU,EAAE,MAAM,EAClB,GAAG,EAAE,YAAY,GAAG,KAAK,GACxB,OAAO,CAAC,gBAAgB,CAAC,CAuB3B"}
|
|
@@ -202,7 +202,7 @@ function resolveConfigObject(node, variableInitializers, configHelpers, seen = n
|
|
|
202
202
|
* leave the guard with a different matcher than the config it came from. Fail
|
|
203
203
|
* on those instead of producing a module that disagrees with the config.
|
|
204
204
|
*/
|
|
205
|
-
function unsupportedStatementReason(statement, source) {
|
|
205
|
+
function unsupportedStatementReason(statement, source, key) {
|
|
206
206
|
switch (statement.type) {
|
|
207
207
|
case "ImportDeclaration":
|
|
208
208
|
if ((statement.specifiers ?? []).length > 0)
|
|
@@ -224,10 +224,14 @@ function unsupportedStatementReason(statement, source) {
|
|
|
224
224
|
const text = source.slice(statement.start, statement.end).replace(/\s+/g, " ").trim();
|
|
225
225
|
const snippet = text.length > 80 ? `${text.slice(0, 77)}...` : text;
|
|
226
226
|
return statement.type === "ImportDeclaration"
|
|
227
|
-
? `the side-effect import \`${snippet}\` would not run with the extracted
|
|
228
|
-
: `the top-level statement \`${snippet}\` would not run with the extracted
|
|
227
|
+
? `the side-effect import \`${snippet}\` would not run with the extracted ${key}`
|
|
228
|
+
: `the top-level statement \`${snippet}\` would not run with the extracted ${key}`;
|
|
229
229
|
}
|
|
230
230
|
export function extractRscMiddlewareConfigModule(source, program) {
|
|
231
|
+
return extractRscConfigPropertyModule(source, program, "middleware");
|
|
232
|
+
}
|
|
233
|
+
/** Extracts `key` from the default-exported config, as a module that exports `{ [key] }`. */
|
|
234
|
+
export function extractRscConfigPropertyModule(source, program, key) {
|
|
231
235
|
const body = program.body ?? [];
|
|
232
236
|
const bindings = new Map();
|
|
233
237
|
const variableInitializers = new Map();
|
|
@@ -235,7 +239,7 @@ export function extractRscMiddlewareConfigModule(source, program) {
|
|
|
235
239
|
let defaultExport = null;
|
|
236
240
|
let order = 0;
|
|
237
241
|
for (const statement of body) {
|
|
238
|
-
const unsupported = unsupportedStatementReason(statement, source);
|
|
242
|
+
const unsupported = unsupportedStatementReason(statement, source, key);
|
|
239
243
|
if (unsupported)
|
|
240
244
|
return { kind: "unsupported", reason: unsupported };
|
|
241
245
|
if (statement.type === "ImportDeclaration") {
|
|
@@ -313,33 +317,33 @@ export function extractRscMiddlewareConfigModule(source, program) {
|
|
|
313
317
|
};
|
|
314
318
|
}
|
|
315
319
|
const properties = configObject.properties ?? [];
|
|
316
|
-
let
|
|
317
|
-
let
|
|
320
|
+
let keyProperty = null;
|
|
321
|
+
let keyIndex = -1;
|
|
318
322
|
for (const [index, property] of properties.entries()) {
|
|
319
|
-
if (property.type === "Property" && propertyName(property) ===
|
|
320
|
-
|
|
321
|
-
|
|
323
|
+
if (property.type === "Property" && propertyName(property) === key) {
|
|
324
|
+
keyProperty = property;
|
|
325
|
+
keyIndex = index;
|
|
322
326
|
}
|
|
323
327
|
}
|
|
324
|
-
if (!
|
|
328
|
+
if (!keyProperty) {
|
|
325
329
|
if (properties.some((property) => property.type === "SpreadElement")) {
|
|
326
330
|
return {
|
|
327
331
|
kind: "unsupported",
|
|
328
|
-
reason:
|
|
332
|
+
reason: `${key} may be supplied through a config spread`,
|
|
329
333
|
};
|
|
330
334
|
}
|
|
331
335
|
return { kind: "absent" };
|
|
332
336
|
}
|
|
333
|
-
if (properties.slice(
|
|
337
|
+
if (properties.slice(keyIndex + 1).some((property) => property.type === "SpreadElement")) {
|
|
334
338
|
return {
|
|
335
339
|
kind: "unsupported",
|
|
336
|
-
reason:
|
|
340
|
+
reason: `a config spread after ${key} may override it`,
|
|
337
341
|
};
|
|
338
342
|
}
|
|
339
|
-
const
|
|
343
|
+
const keyValue = keyProperty.value;
|
|
340
344
|
const candidateNames = new Set(bindings.keys());
|
|
341
345
|
const selectedBindings = new Set();
|
|
342
|
-
const pending = [...referencedTopLevelNames(
|
|
346
|
+
const pending = [...referencedTopLevelNames(keyValue, candidateNames)];
|
|
343
347
|
while (pending.length > 0) {
|
|
344
348
|
const name = pending.pop();
|
|
345
349
|
const binding = bindings.get(name);
|
|
@@ -355,13 +359,13 @@ export function extractRscMiddlewareConfigModule(source, program) {
|
|
|
355
359
|
const declarations = [...selectedBindings]
|
|
356
360
|
.sort((a, b) => a.order - b.order)
|
|
357
361
|
.map((binding) => binding.render());
|
|
358
|
-
const
|
|
362
|
+
const valueSource = source.slice(keyValue.start, keyValue.end);
|
|
359
363
|
return {
|
|
360
364
|
kind: "module",
|
|
361
|
-
code: `${declarations.join("\n")}${declarations.length ? "\n" : ""}export default {
|
|
365
|
+
code: `${declarations.join("\n")}${declarations.length ? "\n" : ""}export default { ${key}: ${valueSource} };\n`,
|
|
362
366
|
};
|
|
363
367
|
}
|
|
364
|
-
function
|
|
368
|
+
function sourceLang(file) {
|
|
365
369
|
switch (path.extname(file)) {
|
|
366
370
|
case ".ts":
|
|
367
371
|
case ".mts":
|
|
@@ -376,15 +380,24 @@ function esbuildLoader(file) {
|
|
|
376
380
|
}
|
|
377
381
|
}
|
|
378
382
|
export async function loadRscMiddlewareConfigModule(configPath) {
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
]);
|
|
383
|
+
return loadRscConfigPropertyModule(configPath, "middleware");
|
|
384
|
+
}
|
|
385
|
+
export async function loadRscConfigPropertyModule(configPath, key) {
|
|
386
|
+
const [{ readFile }, vite] = await Promise.all([import("node:fs/promises"), import("vite")]);
|
|
383
387
|
const source = await readFile(configPath, "utf8");
|
|
384
|
-
const
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
388
|
+
const lang = sourceLang(configPath);
|
|
389
|
+
// Vite 8 transforms with Oxc and only reaches esbuild when the app installs it.
|
|
390
|
+
// Vite 6 and 7 have no transformWithOxc and always ship esbuild.
|
|
391
|
+
const transformed = typeof vite.transformWithOxc === "function"
|
|
392
|
+
? await vite.transformWithOxc(source, configPath, {
|
|
393
|
+
lang,
|
|
394
|
+
sourceType: "module",
|
|
395
|
+
jsx: { runtime: "automatic" },
|
|
396
|
+
})
|
|
397
|
+
: await vite.transformWithEsbuild(source, configPath, {
|
|
398
|
+
format: "esm",
|
|
399
|
+
jsx: "automatic",
|
|
400
|
+
loader: lang,
|
|
401
|
+
});
|
|
402
|
+
return extractRscConfigPropertyModule(transformed.code, vite.parseAst(transformed.code), key);
|
|
390
403
|
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/** The client build's input name, and so the entry file's name: `assets/farm-mobile-client-<hash>.js`. */
|
|
2
|
+
export declare const FARM_RSC_MOBILE_CLIENT_ENTRY_NAME = "farm-mobile-client";
|
|
3
|
+
/** The server entry's stand-in for the shell client, replaced once the client is built. */
|
|
4
|
+
export declare const FARM_RSC_MOBILE_SHELL_CLIENT_PLACEHOLDER = "__farm_mobile_shell_client__";
|
|
5
|
+
/** What the server needs to point a document at the mobile client (core's `FarmMobileShellClient`). */
|
|
6
|
+
export interface FarmRscMobileShellClient {
|
|
7
|
+
webEntry: string;
|
|
8
|
+
entry: string;
|
|
9
|
+
webOnly: string[];
|
|
10
|
+
}
|
|
11
|
+
/** Core's `FarmMobileClientManifest`, with the directory its files are in. */
|
|
12
|
+
export interface FarmRscMobileClientManifest {
|
|
13
|
+
version: 1;
|
|
14
|
+
webEntry: string;
|
|
15
|
+
entry: string;
|
|
16
|
+
stylesheet: string;
|
|
17
|
+
files: string[];
|
|
18
|
+
/** The client output, relative to the app root: the RSC pipeline builds outside `.farm/client`. */
|
|
19
|
+
clientDir: string;
|
|
20
|
+
}
|
|
21
|
+
export declare function farmRscMobileClientManifestPath(root: string): string;
|
|
22
|
+
/** A build without the mobile entry removes the record an earlier build left. */
|
|
23
|
+
export declare function removeFarmRscMobileClientManifest(root: string): void;
|
|
24
|
+
/**
|
|
25
|
+
* Inside the native shell, whose bridge script runs before the page, hand in-app navigations to
|
|
26
|
+
* the shell's navigator, as core's `farmMobileShellClient` does; in a browser it does nothing. It
|
|
27
|
+
* is written into the entry module rather than imported from a core module the browser entry also
|
|
28
|
+
* imports: the bundler places a shared module's code in the web client's chunks.
|
|
29
|
+
*/
|
|
30
|
+
export declare function generateMobileShellNavigation(): string;
|
|
31
|
+
/** The mobile entry's source: install the shell's navigation delegate, then run the browser entry. */
|
|
32
|
+
export declare function generateMobileClientEntry(browserEntry: string): string;
|
|
33
|
+
/** Client build plugin: records the mobile entry and every file the client build wrote. */
|
|
34
|
+
export declare function createFarmRscMobileClientManifestPlugin(getRoot: () => string | undefined): {
|
|
35
|
+
name: string;
|
|
36
|
+
enforce: "post";
|
|
37
|
+
apply: "build";
|
|
38
|
+
generateBundle(this: {
|
|
39
|
+
environment?: {
|
|
40
|
+
name?: string;
|
|
41
|
+
};
|
|
42
|
+
}, _output: unknown, bundle: Record<string, {
|
|
43
|
+
type: string;
|
|
44
|
+
name?: string;
|
|
45
|
+
isEntry?: boolean;
|
|
46
|
+
}>): void;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Finish `.farm/mobile-client.json` once the web entry and the stylesheet are known, and return
|
|
50
|
+
* what the server needs. Null when the build recorded no mobile entry, or its file is missing.
|
|
51
|
+
*/
|
|
52
|
+
export declare function finalizeFarmRscMobileClient(options: {
|
|
53
|
+
root: string;
|
|
54
|
+
clientDir: string;
|
|
55
|
+
/** The web entry the bootstrap script imports, such as `/assets/index-<hash>.js`. */
|
|
56
|
+
webEntry: string;
|
|
57
|
+
/** The stylesheet documents link, or "". */
|
|
58
|
+
stylesheet: string;
|
|
59
|
+
}): FarmRscMobileShellClient | null;
|
|
60
|
+
/** The shell client as the contents of a JavaScript string literal of any quote. */
|
|
61
|
+
export declare function encodeFarmRscMobileShellClient(client: FarmRscMobileShellClient | null): string;
|
|
62
|
+
/**
|
|
63
|
+
* Fill in the shell client wherever the RSC server bundle has the placeholder, which only a build
|
|
64
|
+
* with the mobile entry has. `resolve` runs only then, once: a patched bundle has none left.
|
|
65
|
+
*/
|
|
66
|
+
export declare function patchFarmRscMobileShellClient(rscDir: string, resolve: () => FarmRscMobileShellClient | null): void;
|
|
67
|
+
//# sourceMappingURL=mobile-client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mobile-client.d.ts","sourceRoot":"","sources":["../../src/rsc/mobile-client.ts"],"names":[],"mappings":"AAqBA,0GAA0G;AAC1G,eAAO,MAAM,iCAAiC,uBAAuB,CAAC;AAEtE,2FAA2F;AAC3F,eAAO,MAAM,wCAAwC,iCAAiC,CAAC;AAKvF,uGAAuG;AACvG,MAAM,WAAW,wBAAwB;IACvC,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,MAAM,EAAE,CAAC;CACnB;AAED,8EAA8E;AAC9E,MAAM,WAAW,2BAA2B;IAC1C,OAAO,EAAE,CAAC,CAAC;IACX,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;IACd,UAAU,EAAE,MAAM,CAAC;IACnB,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,mGAAmG;IACnG,SAAS,EAAE,MAAM,CAAC;CACnB;AAWD,wBAAgB,+BAA+B,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEpE;AAED,iFAAiF;AACjF,wBAAgB,iCAAiC,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAEpE;AAED;;;;;GAKG;AACH,wBAAgB,6BAA6B,IAAI,MAAM,CAQtD;AAED,sGAAsG;AACtG,wBAAgB,yBAAyB,CAAC,YAAY,EAAE,MAAM,GAAG,MAAM,CAItE;AAED,2FAA2F;AAC3F,wBAAgB,uCAAuC,CAAC,OAAO,EAAE,MAAM,MAAM,GAAG,SAAS;;;;yBAM7E;QAAE,WAAW,CAAC,EAAE;YAAE,IAAI,CAAC,EAAE,MAAM,CAAA;SAAE,CAAA;KAAE,WAChC,OAAO,UACR,MAAM,CAAC,MAAM,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,MAAM,CAAC;QAAC,OAAO,CAAC,EAAE,OAAO,CAAA;KAAE,CAAC;EAuB/E;AAaD;;;GAGG;AACH,wBAAgB,2BAA2B,CAAC,OAAO,EAAE;IACnD,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,CAAC;IAClB,qFAAqF;IACrF,QAAQ,EAAE,MAAM,CAAC;IACjB,4CAA4C;IAC5C,UAAU,EAAE,MAAM,CAAC;CACpB,GAAG,wBAAwB,GAAG,IAAI,CAsBlC;AAED,oFAAoF;AACpF,wBAAgB,8BAA8B,CAAC,MAAM,EAAE,wBAAwB,GAAG,IAAI,GAAG,MAAM,CAQ9F;AAED;;;GAGG;AACH,wBAAgB,6BAA6B,CAC3C,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,MAAM,wBAAwB,GAAG,IAAI,GAC7C,IAAI,CAsBN"}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The mobile client of an RSC app with a `mobile` block (RFC 0004, 7.2), the RSC pipeline's
|
|
3
|
+
* counterpart of core's `nitro/mobile-client.ts`.
|
|
4
|
+
*
|
|
5
|
+
* The build adds one client entry, `farm-mobile-client`: it hands in-app navigations to the native
|
|
6
|
+
* shell and then runs the browser entry, so it shares every chunk with the web client. The server
|
|
7
|
+
* points the documents a shell asks for (`X-Farm-Shell`) at it, with core's
|
|
8
|
+
* `_applyFarmMobileShellClient`. `.farm/mobile-client.json` records it in core's format, which
|
|
9
|
+
* `farm build --mobile` reads to bundle the client's files into the app.
|
|
10
|
+
*/
|
|
11
|
+
import { existsSync, mkdirSync, readFileSync, readdirSync, rmSync, statSync, writeFileSync, } from "fs";
|
|
12
|
+
import path from "path";
|
|
13
|
+
/** The client build's input name, and so the entry file's name: `assets/farm-mobile-client-<hash>.js`. */
|
|
14
|
+
export const FARM_RSC_MOBILE_CLIENT_ENTRY_NAME = "farm-mobile-client";
|
|
15
|
+
/** The server entry's stand-in for the shell client, replaced once the client is built. */
|
|
16
|
+
export const FARM_RSC_MOBILE_SHELL_CLIENT_PLACEHOLDER = "__farm_mobile_shell_client__";
|
|
17
|
+
/** Next to the RSC entries, as core writes it to its build directory (`.farm/`); never served. */
|
|
18
|
+
const MANIFEST_FILE = "mobile-client.json";
|
|
19
|
+
/** Vite names every client output file `name-<hash>.ext`, the hash taken from its content. */
|
|
20
|
+
const HASHED_FILE = /-[A-Za-z0-9_-]{8,}\.[A-Za-z0-9]+$/;
|
|
21
|
+
export function farmRscMobileClientManifestPath(root) {
|
|
22
|
+
return path.join(root, ".farm", MANIFEST_FILE);
|
|
23
|
+
}
|
|
24
|
+
/** A build without the mobile entry removes the record an earlier build left. */
|
|
25
|
+
export function removeFarmRscMobileClientManifest(root) {
|
|
26
|
+
rmSync(farmRscMobileClientManifestPath(root), { force: true });
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Inside the native shell, whose bridge script runs before the page, hand in-app navigations to
|
|
30
|
+
* the shell's navigator, as core's `farmMobileShellClient` does; in a browser it does nothing. It
|
|
31
|
+
* is written into the entry module rather than imported from a core module the browser entry also
|
|
32
|
+
* imports: the bundler places a shared module's code in the web client's chunks.
|
|
33
|
+
*/
|
|
34
|
+
export function generateMobileShellNavigation() {
|
|
35
|
+
return `import { delegateNavigation as farmDelegateNavigation } from '@farm.js/core/client';
|
|
36
|
+
const farmMobileShellBridge = globalThis.__farmBridge;
|
|
37
|
+
const farmOfferNavigation = farmMobileShellBridge?.offerNavigation;
|
|
38
|
+
if (typeof farmOfferNavigation === 'function') {
|
|
39
|
+
farmDelegateNavigation((context) => farmOfferNavigation.call(farmMobileShellBridge, context) === true);
|
|
40
|
+
}
|
|
41
|
+
`;
|
|
42
|
+
}
|
|
43
|
+
/** The mobile entry's source: install the shell's navigation delegate, then run the browser entry. */
|
|
44
|
+
export function generateMobileClientEntry(browserEntry) {
|
|
45
|
+
// The browser entry hydrates asynchronously, so the delegate is in place before any click.
|
|
46
|
+
return `import ${JSON.stringify(browserEntry)};
|
|
47
|
+
${generateMobileShellNavigation()}`;
|
|
48
|
+
}
|
|
49
|
+
/** Client build plugin: records the mobile entry and every file the client build wrote. */
|
|
50
|
+
export function createFarmRscMobileClientManifestPlugin(getRoot) {
|
|
51
|
+
return {
|
|
52
|
+
name: "@farm.js/plugin/rsc:mobile-client-manifest",
|
|
53
|
+
enforce: "post",
|
|
54
|
+
apply: "build",
|
|
55
|
+
generateBundle(_output, bundle) {
|
|
56
|
+
const root = getRoot();
|
|
57
|
+
if (!root || this.environment?.name !== "client")
|
|
58
|
+
return;
|
|
59
|
+
const entry = Object.entries(bundle).find(([, output]) => output.type === "chunk" &&
|
|
60
|
+
output.isEntry &&
|
|
61
|
+
output.name === FARM_RSC_MOBILE_CLIENT_ENTRY_NAME)?.[0];
|
|
62
|
+
const record = entry
|
|
63
|
+
? {
|
|
64
|
+
entry,
|
|
65
|
+
files: Object.keys(bundle)
|
|
66
|
+
.filter((file) => !file.endsWith(".map"))
|
|
67
|
+
.sort(),
|
|
68
|
+
}
|
|
69
|
+
: null;
|
|
70
|
+
const manifestPath = farmRscMobileClientManifestPath(root);
|
|
71
|
+
mkdirSync(path.dirname(manifestPath), { recursive: true });
|
|
72
|
+
writeFileSync(manifestPath, JSON.stringify(record));
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
function readRecord(root) {
|
|
77
|
+
try {
|
|
78
|
+
const record = JSON.parse(readFileSync(farmRscMobileClientManifestPath(root), "utf8"));
|
|
79
|
+
return record && typeof record.entry === "string" && Array.isArray(record.files)
|
|
80
|
+
? record
|
|
81
|
+
: null;
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Finish `.farm/mobile-client.json` once the web entry and the stylesheet are known, and return
|
|
89
|
+
* what the server needs. Null when the build recorded no mobile entry, or its file is missing.
|
|
90
|
+
*/
|
|
91
|
+
export function finalizeFarmRscMobileClient(options) {
|
|
92
|
+
const record = readRecord(options.root);
|
|
93
|
+
if (!record || !options.webEntry)
|
|
94
|
+
return null;
|
|
95
|
+
const entryFile = path.join(options.clientDir, record.entry);
|
|
96
|
+
if (!existsSync(entryFile) || statSync(entryFile).size === 0)
|
|
97
|
+
return null;
|
|
98
|
+
const files = new Set(record.files.filter((file) => HASHED_FILE.test(file)).map((file) => `/${file}`));
|
|
99
|
+
const manifest = {
|
|
100
|
+
version: 1,
|
|
101
|
+
webEntry: options.webEntry,
|
|
102
|
+
entry: `/${record.entry}`,
|
|
103
|
+
stylesheet: options.stylesheet,
|
|
104
|
+
files: [...files].sort(),
|
|
105
|
+
clientDir: path.relative(options.root, options.clientDir).split(path.sep).join("/"),
|
|
106
|
+
};
|
|
107
|
+
writeFileSync(farmRscMobileClientManifestPath(options.root), `${JSON.stringify(manifest, null, 2)}\n`);
|
|
108
|
+
// The mobile entry loads the web entry, so every web chunk is the mobile client's too.
|
|
109
|
+
return { webEntry: options.webEntry, entry: manifest.entry, webOnly: [] };
|
|
110
|
+
}
|
|
111
|
+
/** The shell client as the contents of a JavaScript string literal of any quote. */
|
|
112
|
+
export function encodeFarmRscMobileShellClient(client) {
|
|
113
|
+
return JSON.stringify(JSON.stringify(client))
|
|
114
|
+
.slice(1, -1)
|
|
115
|
+
.replace(/'/g, "\\u0027")
|
|
116
|
+
.replace(/`/g, "\\u0060")
|
|
117
|
+
.replace(/\$/g, "\\u0024")
|
|
118
|
+
.replace(/\u2028/g, "\\u2028")
|
|
119
|
+
.replace(/\u2029/g, "\\u2029");
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Fill in the shell client wherever the RSC server bundle has the placeholder, which only a build
|
|
123
|
+
* with the mobile entry has. `resolve` runs only then, once: a patched bundle has none left.
|
|
124
|
+
*/
|
|
125
|
+
export function patchFarmRscMobileShellClient(rscDir, resolve) {
|
|
126
|
+
const files = [];
|
|
127
|
+
const visit = (dir) => {
|
|
128
|
+
if (!existsSync(dir))
|
|
129
|
+
return;
|
|
130
|
+
for (const name of readdirSync(dir)) {
|
|
131
|
+
const file = path.join(dir, name);
|
|
132
|
+
if (statSync(file).isDirectory())
|
|
133
|
+
visit(file);
|
|
134
|
+
else if (/\.(?:m|c)?js$/.test(name) &&
|
|
135
|
+
readFileSync(file, "utf8").includes(FARM_RSC_MOBILE_SHELL_CLIENT_PLACEHOLDER)) {
|
|
136
|
+
files.push(file);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
visit(rscDir);
|
|
141
|
+
if (files.length === 0)
|
|
142
|
+
return;
|
|
143
|
+
const encoded = encodeFarmRscMobileShellClient(resolve());
|
|
144
|
+
for (const file of files) {
|
|
145
|
+
const code = readFileSync(file, "utf8");
|
|
146
|
+
writeFileSync(file, code.split(FARM_RSC_MOBILE_SHELL_CLIENT_PLACEHOLDER).join(encoded));
|
|
147
|
+
}
|
|
148
|
+
}
|
|
@@ -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;AAgCH;;;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;AAuGD;;;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;AA0NF,wBAAsB,aAAa,CAAC,OAAO,EAAE,oBAAoB,GAAG,OAAO,CAAC,IAAI,CAAC,CAwIhF"}
|