@farm.js/plugin 0.1.60 → 0.1.61

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.
Files changed (40) hide show
  1. package/dist/index.d.ts +1 -1
  2. package/dist/index.js +1 -1
  3. package/dist/middleware/index.d.ts +1 -8
  4. package/dist/middleware/index.d.ts.map +1 -1
  5. package/dist/middleware/index.js +10 -16
  6. package/dist/middleware/matching.d.ts +2 -4
  7. package/dist/middleware/matching.d.ts.map +1 -1
  8. package/dist/middleware/matching.js +47 -7
  9. package/dist/rsc/entries/client.d.ts.map +1 -1
  10. package/dist/rsc/entries/client.js +15 -85
  11. package/dist/rsc/entries/rsc.d.ts.map +1 -1
  12. package/dist/rsc/entries/rsc.js +45 -563
  13. package/dist/rsc/entries/ssr.d.ts.map +1 -1
  14. package/dist/rsc/entries/ssr.js +6 -7
  15. package/dist/rsc/index.d.ts +3 -38
  16. package/dist/rsc/index.d.ts.map +1 -1
  17. package/dist/rsc/index.js +292 -279
  18. package/dist/rsc/middleware-config-module.d.ts +0 -3
  19. package/dist/rsc/middleware-config-module.d.ts.map +1 -1
  20. package/dist/rsc/middleware-config-module.js +28 -41
  21. package/dist/rsc/nitro-build.d.ts.map +1 -1
  22. package/dist/rsc/nitro-build.js +81 -268
  23. package/dist/rsc/server-fn-transform.d.ts +5 -1
  24. package/dist/rsc/server-fn-transform.d.ts.map +1 -1
  25. package/dist/rsc/server-fn-transform.js +338 -3
  26. package/dist/rsc/types.d.ts +0 -39
  27. package/dist/rsc/types.d.ts.map +1 -1
  28. package/dist/rsc/vite-plugin-nitro.d.ts +0 -2
  29. package/dist/rsc/vite-plugin-nitro.d.ts.map +1 -1
  30. package/dist/rsc/vite-plugin-nitro.js +2 -12
  31. package/package.json +3 -3
  32. package/dist/rsc/client-sourcemaps.d.ts +0 -13
  33. package/dist/rsc/client-sourcemaps.d.ts.map +0 -1
  34. package/dist/rsc/client-sourcemaps.js +0 -52
  35. package/dist/rsc/document-layouts.d.ts +0 -16
  36. package/dist/rsc/document-layouts.d.ts.map +0 -1
  37. package/dist/rsc/document-layouts.js +0 -82
  38. package/dist/rsc/mobile-client.d.ts +0 -67
  39. package/dist/rsc/mobile-client.d.ts.map +0 -1
  40. package/dist/rsc/mobile-client.js +0 -148
@@ -1,82 +0,0 @@
1
- import { promises as fs } from "node:fs";
2
- import path from "node:path";
3
- const LAYOUT_FILE = /^layout\.(tsx|jsx|ts|js)$/;
4
- const SOURCE_EXTENSIONS = [".tsx", ".ts", ".jsx", ".js"];
5
- const RENDERS_HTML = /<html[\s>]/;
6
- // `export { default } from "./x"` and `export { Layout as default } from "./x"`.
7
- const DEFAULT_REEXPORT = /export\s*\{[^}]*\b(?:default|[\w$]+\s+as\s+default)\b[^}]*\}\s*from\s*["'](\.{1,2}\/[^"']+)["']/;
8
- async function resolveRelativeModule(fromFile, specifier) {
9
- const base = path.resolve(path.dirname(fromFile), specifier);
10
- const candidates = [
11
- base,
12
- ...SOURCE_EXTENSIONS.map((extension) => base + extension),
13
- ...SOURCE_EXTENSIONS.map((extension) => path.join(base, "index" + extension)),
14
- ];
15
- for (const candidate of candidates) {
16
- try {
17
- if ((await fs.stat(candidate)).isFile())
18
- return candidate;
19
- }
20
- catch {
21
- // try the next candidate
22
- }
23
- }
24
- return null;
25
- }
26
- /**
27
- * Whether a layout module renders the whole `<html>` document, following re-exports of its default
28
- * export a few modules deep (`export { default } from "../site/app/layout"`).
29
- */
30
- export async function layoutRendersDocument(file, depth = 0) {
31
- let source;
32
- try {
33
- source = await fs.readFile(file, "utf8");
34
- }
35
- catch {
36
- return false;
37
- }
38
- if (RENDERS_HTML.test(source))
39
- return true;
40
- const reexport = DEFAULT_REEXPORT.exec(source);
41
- if (!reexport || depth >= 4)
42
- return false;
43
- const target = await resolveRelativeModule(file, reexport[1]);
44
- return target ? layoutRendersDocument(target, depth + 1) : false;
45
- }
46
- /**
47
- * Files below `routesDir` whose name passes `test`, as route-relative keys (`/layout.tsx`,
48
- * `/(docs)/layout.tsx`): the keys the RSC entry gives its merged route module maps.
49
- */
50
- export async function findRouteFiles(routesDir, test) {
51
- const found = [];
52
- const walk = async (dir) => {
53
- let entries;
54
- try {
55
- entries = await fs.readdir(dir, { withFileTypes: true });
56
- }
57
- catch {
58
- return;
59
- }
60
- for (const entry of entries) {
61
- const full = path.join(dir, entry.name);
62
- if (entry.isDirectory()) {
63
- if (entry.name !== "node_modules" && !entry.name.startsWith("."))
64
- await walk(full);
65
- }
66
- else if (test(entry.name)) {
67
- found.push({
68
- key: "/" + path.relative(routesDir, full).split(path.sep).join("/"),
69
- file: full,
70
- });
71
- }
72
- }
73
- };
74
- await walk(routesDir);
75
- return found.sort((left, right) => (left.key < right.key ? -1 : left.key > right.key ? 1 : 0));
76
- }
77
- /** The layouts below `routesDir` that render the `<html>` document themselves. */
78
- export async function findDocumentLayouts(routesDir) {
79
- const layouts = await findRouteFiles(routesDir, (name) => LAYOUT_FILE.test(name));
80
- const owners = await Promise.all(layouts.map(({ file }) => layoutRendersDocument(file)));
81
- return layouts.filter((_, index) => owners[index]).map(({ key }) => key);
82
- }
@@ -1,67 +0,0 @@
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
@@ -1 +0,0 @@
1
- {"version":3,"file":"mobile-client.d.ts","sour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AI,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"}
@@ -1,148 +0,0 @@
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
- }