@farm.js/plugin 0.1.0-beta.10 → 0.1.0-beta.13

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 (52) hide show
  1. package/dist/api/index.d.ts +42 -0
  2. package/dist/api/index.d.ts.map +1 -0
  3. package/dist/api/index.js +493 -0
  4. package/dist/context/index.d.ts +61 -0
  5. package/dist/context/index.d.ts.map +1 -0
  6. package/dist/context/index.js +75 -0
  7. package/dist/index.d.ts +43 -0
  8. package/dist/index.d.ts.map +1 -0
  9. package/dist/index.js +49 -0
  10. package/dist/middleware/index.d.ts +97 -0
  11. package/dist/middleware/index.d.ts.map +1 -0
  12. package/dist/middleware/index.js +469 -0
  13. package/dist/observability/index.d.ts +190 -0
  14. package/dist/observability/index.d.ts.map +1 -0
  15. package/dist/observability/index.js +399 -0
  16. package/dist/rsc/automatic-optimized-boundary.d.ts +11 -0
  17. package/dist/rsc/automatic-optimized-boundary.d.ts.map +1 -0
  18. package/dist/rsc/automatic-optimized-boundary.js +162 -0
  19. package/dist/rsc/build-paths.d.ts +3 -0
  20. package/dist/rsc/build-paths.d.ts.map +1 -0
  21. package/dist/rsc/build-paths.js +8 -0
  22. package/dist/rsc/compatibility.d.ts +8 -0
  23. package/dist/rsc/compatibility.d.ts.map +1 -0
  24. package/dist/rsc/compatibility.js +46 -0
  25. package/dist/rsc/entries/client.d.ts +14 -0
  26. package/dist/rsc/entries/client.d.ts.map +1 -0
  27. package/dist/rsc/entries/client.js +283 -0
  28. package/dist/rsc/entries/rsc.d.ts +13 -0
  29. package/dist/rsc/entries/rsc.d.ts.map +1 -0
  30. package/dist/rsc/entries/rsc.js +932 -0
  31. package/dist/rsc/entries/ssr.d.ts +13 -0
  32. package/dist/rsc/entries/ssr.d.ts.map +1 -0
  33. package/dist/rsc/entries/ssr.js +245 -0
  34. package/dist/rsc/index.d.ts +87 -0
  35. package/dist/rsc/index.d.ts.map +1 -0
  36. package/dist/rsc/index.js +1389 -0
  37. package/dist/rsc/nitro-build.d.ts +36 -0
  38. package/dist/rsc/nitro-build.d.ts.map +1 -0
  39. package/dist/rsc/nitro-build.js +396 -0
  40. package/dist/rsc/optimized-boundary.d.ts +29 -0
  41. package/dist/rsc/optimized-boundary.d.ts.map +1 -0
  42. package/dist/rsc/optimized-boundary.js +243 -0
  43. package/dist/rsc/server-fn-transform.d.ts +6 -0
  44. package/dist/rsc/server-fn-transform.d.ts.map +1 -0
  45. package/dist/rsc/server-fn-transform.js +152 -0
  46. package/dist/rsc/types.d.ts +123 -0
  47. package/dist/rsc/types.d.ts.map +1 -0
  48. package/dist/rsc/types.js +1 -0
  49. package/dist/rsc/vite-plugin-nitro.d.ts +33 -0
  50. package/dist/rsc/vite-plugin-nitro.d.ts.map +1 -0
  51. package/dist/rsc/vite-plugin-nitro.js +163 -0
  52. package/package.json +3 -3
@@ -0,0 +1,163 @@
1
+ /**
2
+ * Vite plugin to run Nitro after RSC build (single "vite build", no post script).
3
+ * Implements the Nitro RSC deployment pipeline:
4
+ * - Step 1: Force per-environment output dirs to .nitro/vite/dist/${name} so Nitro can locate artifacts.
5
+ * - Step 2: Capture the server (RSC) Rollup bundle in writeBundle.
6
+ * - Step 3: Find the SSR entry chunk from the bundle.
7
+ * - Step 4: Compute renderer path and pass to Nitro with client publicDir.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * import nitro from '@farm.js/plugin/rsc/vite-plugin-nitro'
12
+ * export default defineConfig({
13
+ * plugins: [rsc(), react(), nitro({ server: { environmentName: 'rsc' }, config: { preset: 'vercel' } })],
14
+ * })
15
+ * ```
16
+ */
17
+ import path from "path";
18
+ import { buildRscNitro } from "./nitro-build.js";
19
+ import { waitForRscOutputs } from "./nitro-build.js";
20
+ 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
+ const NITRO_VITE_DIST = ".nitro/vite/dist";
25
+ export default function nitro(options = {}) {
26
+ const serverEnvName = options.server?.environmentName ?? "rsc";
27
+ const serverEntryName = options.server?.entryName ?? "index";
28
+ let resolvedRoot = process.cwd();
29
+ let rscOutDir = path.join(NITRO_VITE_DIST, "rsc");
30
+ let ssrOutDir = path.join(NITRO_VITE_DIST, "ssr");
31
+ let clientOutDir = path.join(NITRO_VITE_DIST, "client");
32
+ let baseOutDir = NITRO_VITE_DIST;
33
+ return {
34
+ name: "vite-plugin-nitro",
35
+ apply: "build",
36
+ // Step 1: Force per-environment output dirs so Nitro can locate artifacts.
37
+ config() {
38
+ return {
39
+ environments: {
40
+ rsc: { build: { outDir: path.join(NITRO_VITE_DIST, "rsc") } },
41
+ ssr: { build: { outDir: path.join(NITRO_VITE_DIST, "ssr") } },
42
+ client: { build: { outDir: path.join(NITRO_VITE_DIST, "client") } },
43
+ },
44
+ };
45
+ },
46
+ configResolved(config) {
47
+ resolvedRoot = path.resolve(config.root ?? process.cwd());
48
+ const envs = config.environments;
49
+ if (envs?.rsc?.build?.outDir && envs?.ssr?.build?.outDir && envs?.client?.build?.outDir) {
50
+ rscOutDir = envs.rsc.build.outDir;
51
+ ssrOutDir = envs.ssr.build.outDir;
52
+ clientOutDir = envs.client.build.outDir;
53
+ const dir = path.dirname(rscOutDir);
54
+ baseOutDir = path.isAbsolute(dir) ? path.relative(resolvedRoot, dir) || "." : dir;
55
+ }
56
+ else {
57
+ baseOutDir = options.outDir ?? "dist";
58
+ rscOutDir = path.join(baseOutDir, "rsc");
59
+ ssrOutDir = path.join(baseOutDir, "ssr");
60
+ clientOutDir = path.join(baseOutDir, "client");
61
+ }
62
+ },
63
+ // Step 2: Capture the server (RSC) Rollup bundle when that environment writes.
64
+ writeBundle(_options, bundle) {
65
+ if (this.environment?.name === serverEnvName) {
66
+ serverBundle = bundle;
67
+ globalThis.__FARM_NITRO_SERVER_BUNDLE = bundle;
68
+ globalThis.__FARM_NITRO_PATHS = {
69
+ root: resolvedRoot,
70
+ rscOutDir,
71
+ ssrOutDir,
72
+ clientOutDir,
73
+ serverEntryName,
74
+ preset: options.config?.preset ?? process.env.NITRO_PRESET ?? "vercel",
75
+ };
76
+ }
77
+ },
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
+ };
129
+ }
130
+ /** Called by RSC plugin buildApp (post) to run Nitro with the captured bundle. */
131
+ export async function runNitroFromBuildApp() {
132
+ const paths = globalThis.__FARM_NITRO_PATHS;
133
+ const bundle = globalThis.__FARM_NITRO_SERVER_BUNDLE;
134
+ if (!paths)
135
+ return;
136
+ const root = paths.root;
137
+ const preset = paths.preset;
138
+ const baseOutDir = path.dirname(paths.rscOutDir);
139
+ await waitForRscOutputs(root, baseOutDir, { timeoutMs: 25_000 });
140
+ let rendererPath = resolveRscBuildOutputPath(root, paths.rscOutDir, "index.js");
141
+ if (bundle) {
142
+ const serverEntryChunks = [];
143
+ for (const chunk of Object.values(bundle)) {
144
+ if (chunk.type === "chunk" && chunk.isEntry && chunk.fileName) {
145
+ serverEntryChunks.push(chunk);
146
+ }
147
+ }
148
+ const selected = paths.serverEntryName
149
+ ? serverEntryChunks.find((c) => (c.name ?? c.fileName) === paths.serverEntryName)
150
+ : serverEntryChunks[0];
151
+ if (selected?.fileName) {
152
+ rendererPath = resolveRscBuildOutputPath(root, paths.rscOutDir, selected.fileName);
153
+ }
154
+ }
155
+ await buildRscNitro({
156
+ root,
157
+ rendererPath,
158
+ publicDir: resolveRscBuildOutputPath(root, paths.clientOutDir),
159
+ ssrPath: resolveRscBuildOutputPath(root, paths.ssrOutDir, "index.js"),
160
+ assetsDir: undefined,
161
+ preset,
162
+ });
163
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@farm.js/plugin",
3
- "version": "0.1.0-beta.10",
3
+ "version": "0.1.0-beta.13",
4
4
  "description": "Official plugins for Farm.js framework",
5
5
  "license": "MIT",
6
6
  "author": "Farm.js Team",
@@ -61,7 +61,7 @@
61
61
  "nitro": "3.0.1-alpha.0",
62
62
  "picocolors": "^1.0.0",
63
63
  "rsc-html-stream": "^0.0.4",
64
- "@farm.js/core": "0.1.0-beta.10"
64
+ "@farm.js/core": "0.1.0-beta.13"
65
65
  },
66
66
  "devDependencies": {
67
67
  "@types/node": "^20.19.21",
@@ -82,7 +82,7 @@
82
82
  "react-dom": "19.2.8",
83
83
  "react-server-dom-webpack": "19.2.8",
84
84
  "vite": ">=6.0.0",
85
- "@farm.js/core": "0.1.0-beta.10"
85
+ "@farm.js/core": "0.1.0-beta.13"
86
86
  },
87
87
  "peerDependenciesMeta": {
88
88
  "@vitejs/plugin-rsc": {