@tschk/moonshine-deploy-vercel 0.4.4 → 0.4.80

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 (2) hide show
  1. package/package.json +7 -7
  2. package/src/index.ts +83 -4
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tschk/moonshine-deploy-vercel",
3
- "version": "0.4.4",
3
+ "version": "0.4.80",
4
4
  "license": "ISC",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
@@ -15,21 +15,21 @@
15
15
  "test": "bun test"
16
16
  },
17
17
  "dependencies": {
18
- "@tschk/moonshine-framework": "^0.4.4",
19
- "@tschk/moonshine-router": "^0.4.4",
20
- "@tschk/moonshine-server": "^0.4.4",
21
- "@tschk/moonshine-deploy-node": "^0.4.4"
18
+ "@tschk/moonshine-framework": "^0.4.80",
19
+ "@tschk/moonshine-router": "^0.4.80",
20
+ "@tschk/moonshine-server": "^0.4.80",
21
+ "@tschk/moonshine-deploy-node": "^0.4.80"
22
22
  },
23
23
  "devDependencies": {
24
24
  "@types/bun": "1.3.14",
25
25
  "typescript": "7.0.2",
26
- "@tschk/moonshine-adapter-conformance": "^0.4.4"
26
+ "@tschk/moonshine-adapter-conformance": "^0.4.80"
27
27
  },
28
28
  "files": [
29
29
  "src"
30
30
  ],
31
31
  "peerDependencies": {
32
- "@tschk/moonshine-react": "^0.4.4"
32
+ "@tschk/moonshine-react": "^0.4.80"
33
33
  },
34
34
  "peerDependenciesMeta": {
35
35
  "@tschk/moonshine-react": {
package/src/index.ts CHANGED
@@ -1,12 +1,18 @@
1
1
  import { copyFile, mkdir, realpath, rm, writeFile } from "node:fs/promises";
2
2
  import { dirname, isAbsolute, relative, resolve, sep } from "node:path";
3
3
  import { fileURLToPath } from "node:url";
4
- import { tryServeStatic } from "@tschk/moonshine-server";
4
+ import { existsSync } from "node:fs";
5
+ import {
6
+ createRequestHandler,
7
+ tryServeStatic,
8
+ type RouteModule,
9
+ } from "@tschk/moonshine-server";
5
10
 
6
11
  import {
7
12
  type DeploymentAdapter,
8
13
  type MoonshineManifest,
9
14
  type RenderContext,
15
+ type Renderer,
10
16
  type RouteArtifact,
11
17
  type Harness,
12
18
  type HarnessFactory,
@@ -169,7 +175,14 @@ async function copyPublicAssets(
169
175
  );
170
176
  }
171
177
 
172
- async function buildNativeFunction(
178
+ /**
179
+ * Writes `outDir/.vercel/output` around one Node function bundled from the
180
+ * server bundle `moonshine build` emitted: its route modules and renderer,
181
+ * with every package inlined, so the function runs on Vercel without the
182
+ * project's sources or node_modules. React static routes are prerendered
183
+ * through the same bundle and served from the filesystem first.
184
+ */
185
+ async function buildBundledFunction(
173
186
  manifest: MoonshineManifest,
174
187
  outDir: string,
175
188
  ) {
@@ -188,18 +201,23 @@ async function buildNativeFunction(
188
201
  entryPath,
189
202
  `import { createRequestHandler } from "@tschk/moonshine-server";
190
203
  import { createNodeHandler } from ${JSON.stringify(fileURLToPath(import.meta.resolve("@tschk/moonshine-deploy-node")))};
191
- import { modules, renderer } from "./dist/server.js";
204
+ import { modules, renderer } from ${JSON.stringify(`./${manifest.entries.server}`)};
192
205
  import manifest from "./manifest.json" with { type: "json" };
193
206
  const fetch = createRequestHandler({ manifest, modules, renderer });
194
207
  export default createNodeHandler({ fetch });
195
208
  `,
196
209
  );
210
+ const overrides =
211
+ manifest.renderer === "native"
212
+ ? {}
213
+ : await prerenderStatic(manifest, outDir, staticDir);
197
214
  const result = await Bun.build({
198
215
  entrypoints: [entryPath],
199
216
  outdir: funcDir,
200
217
  naming: "index.mjs",
201
218
  target: "node",
202
219
  packages: "bundle",
220
+ define: { "process.env.NODE_ENV": '"production"' },
203
221
  });
204
222
  if (!result.success)
205
223
  throw new AggregateError(
@@ -225,6 +243,7 @@ export default createNodeHandler({ fetch });
225
243
  {
226
244
  version: 3,
227
245
  routes: [{ handle: "filesystem" }, { src: "/.*", dest: "/moonshine" }],
246
+ ...(Object.keys(overrides).length && { overrides }),
228
247
  },
229
248
  null,
230
249
  2,
@@ -232,6 +251,58 @@ export default createNodeHandler({ fetch });
232
251
  );
233
252
  }
234
253
 
254
+ /**
255
+ * Renders each static or island route without params through the server bundle and
256
+ * writes it under `static/`, with the override that serves `about.html` at
257
+ * `/about`.
258
+ */
259
+ async function prerenderStatic(
260
+ manifest: MoonshineManifest,
261
+ outDir: string,
262
+ staticDir: string,
263
+ ): Promise<Record<string, { path: string }>> {
264
+ const bundle = (await import(resolve(outDir, manifest.entries.server!))) as {
265
+ modules: Record<string, RouteModule>;
266
+ renderer: Renderer;
267
+ };
268
+ const fetch = createRequestHandler({
269
+ manifest,
270
+ modules: bundle.modules,
271
+ renderer: bundle.renderer,
272
+ });
273
+ const overrides: Record<string, { path: string }> = {};
274
+ for (const route of manifest.routes) {
275
+ // An island page is build-time HTML too; only its islands run later.
276
+ if (
277
+ (route.mode !== "static" && route.mode !== "island") ||
278
+ /[:*]/.test(route.path)
279
+ )
280
+ continue;
281
+ const response = await fetch(new Request(`http://localhost${route.path}`));
282
+ if (
283
+ response.status !== 200 ||
284
+ !response.headers.get("content-type")?.startsWith("text/html")
285
+ ) {
286
+ await response.body?.cancel();
287
+ continue;
288
+ }
289
+ const path = route.path.replace(/^\/+|\/+$/g, "");
290
+ const file = `${path || "index"}.html`;
291
+ await mkdir(dirname(resolve(staticDir, file)), { recursive: true });
292
+ await writeFile(resolve(staticDir, file), await response.text());
293
+ if (path) overrides[file] = { path };
294
+ }
295
+ return overrides;
296
+ }
297
+
298
+ /** A React build whose server bundle `moonshine build` actually wrote. */
299
+ function hasServerBundle(manifest: MoonshineManifest, outDir: string): boolean {
300
+ return Boolean(
301
+ manifest.entries.server &&
302
+ existsSync(resolve(outDir, manifest.entries.server)),
303
+ );
304
+ }
305
+
235
306
  export const vercelAdapter: DeploymentAdapter = {
236
307
  name: "vercel",
237
308
  runtimes: ["node", "vercel-edge"],
@@ -261,7 +332,15 @@ export const vercelAdapter: DeploymentAdapter = {
261
332
  force: true,
262
333
  });
263
334
  if (manifest.renderer === "native") {
264
- await buildNativeFunction(manifest, outDir);
335
+ await buildBundledFunction(manifest, outDir);
336
+ return;
337
+ }
338
+ if (hasServerBundle(manifest, outDir)) {
339
+ if (manifest.routes.some((route) => route.runtime === "vercel-edge"))
340
+ throw new Error(
341
+ "React Vercel output runs one Node function; vercel-edge routes are not supported",
342
+ );
343
+ await buildBundledFunction(manifest, outDir);
265
344
  return;
266
345
  }
267
346
  for (const asset of manifest.assets) validateAssetPath(asset.path);