@guren/plugin-vercel 0.1.2 → 0.2.1

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 ADDED
@@ -0,0 +1,44 @@
1
+ # @guren/plugin-vercel
2
+
3
+ Deploy a [Guren](https://guren.dev/) SSR application to Vercel. The plugin assembles a [Build Output API v3](https://vercel.com/docs/build-output-api/v3) directory that runs on Vercel's Bun runtime.
4
+
5
+ ```bash
6
+ bunx guren plugin @guren/plugin-vercel
7
+ bun add @guren/plugin-vercel
8
+ ```
9
+
10
+ Installing scaffolds `src/vercel.ts`, `scripts/vercel-build.ts`, and `vercel.json`.
11
+
12
+ ## Build and deploy
13
+
14
+ ```bash
15
+ bun run vercel:build
16
+ vercel deploy --prebuilt
17
+ ```
18
+
19
+ ## API
20
+
21
+ - **`createVercelHandler(app)`** — boots a Guren `Application` and returns a `fetch` handler for the serverless function.
22
+
23
+ ```typescript
24
+ // src/vercel.ts
25
+ import app from './app.js'
26
+ import { createVercelHandler } from '@guren/plugin-vercel'
27
+
28
+ export default await createVercelHandler(app)
29
+ ```
30
+
31
+ - **`buildVercelOutput(options)`** — assembles `.vercel/output`: the bundled function, static assets, and the routing config. Reads the Vite manifests to inject the correct `GUREN_INERTIA_*` environment variables into the function.
32
+ - **`vercelPlugin()`** — the service provider factory, registered automatically by `guren plugin`.
33
+
34
+ ## Notes
35
+
36
+ - **SSR apps only.** The plugin reads Vite manifests to wire the Inertia SSR bundle. API-only apps should use Docker or Lambda instead.
37
+ - **SSR bundles must be self-contained.** Function directories ship without `node_modules`, so externalized `react`/`@inertiajs/react` imports fail at runtime and Inertia silently falls back to client-side rendering. The Guren Vite plugin defaults `ssr.noExternal: true` for SSR builds — leave it alone unless you know the target has those packages installed.
38
+ - **`NODE_ENV` is inlined at bundle time.** `bun build` bakes in `"development"` unless told otherwise, and a runtime `NODE_ENV=production` cannot override it. This plugin passes the right `--define` for you.
39
+
40
+ See the [deployment guide](https://guren.dev/docs/guides/deployment) for the full setup.
41
+
42
+ ## License
43
+
44
+ MIT
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { ServiceProvider } from '@guren/core';
1
+ import { ServiceProviderConstructor } from '@guren/core';
2
+ import { PathLike } from '@guren/core/internal/deploy-build';
2
3
 
3
- type PathLike = string | URL;
4
4
  interface VercelAppLike {
5
5
  boot(): Promise<void>;
6
6
  fetch(request: Request): Response | Promise<Response>;
@@ -17,10 +17,22 @@ interface BuildVercelOutputOptions {
17
17
  ssrDir?: PathLike;
18
18
  migrationsDir?: PathLike;
19
19
  }
20
- declare class GurenPluginVercelProvider extends ServiceProvider {
21
- register(): void;
20
+ /**
21
+ * Configuration for the Vercel plugin. Currently empty — reserved so future
22
+ * fields never force another registration-shape change.
23
+ */
24
+ interface VercelPluginConfig {
22
25
  }
26
+ /**
27
+ * Register the Vercel plugin.
28
+ *
29
+ * @example
30
+ * ```typescript
31
+ * createApp({ providers: [vercelPlugin()] })
32
+ * ```
33
+ */
34
+ declare function vercelPlugin(config?: VercelPluginConfig): ServiceProviderConstructor;
23
35
  declare function createVercelHandler(app: VercelAppLike): Promise<VercelHandler>;
24
36
  declare function buildVercelOutput(options?: BuildVercelOutputOptions): void;
25
37
 
26
- export { type BuildVercelOutputOptions, GurenPluginVercelProvider, type VercelAppLike, type VercelHandler, buildVercelOutput, createVercelHandler };
38
+ export { type BuildVercelOutputOptions, type VercelAppLike, type VercelHandler, type VercelPluginConfig, buildVercelOutput, createVercelHandler, vercelPlugin };
package/dist/index.js CHANGED
@@ -1,12 +1,24 @@
1
1
  // src/index.ts
2
- import { cpSync, existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "fs";
2
+ import { cpSync, existsSync, mkdirSync, writeFileSync } from "fs";
3
3
  import { basename, extname, resolve } from "path";
4
- import { fileURLToPath } from "url";
5
- import { ServiceProvider } from "@guren/core";
6
- var GurenPluginVercelProvider = class extends ServiceProvider {
4
+ import { definePlugin } from "@guren/core";
5
+ import {
6
+ assertOutputDirOutsideRoot,
7
+ resetOutputDir,
8
+ resolveClientAssetEnv,
9
+ resolvePathLike,
10
+ resolveSsrEntryFile,
11
+ ssrRuntimePaths
12
+ } from "@guren/core/internal/deploy-build";
13
+ var LABEL = "Vercel build";
14
+ var factory = definePlugin({
15
+ name: "vercel",
7
16
  register() {
8
17
  }
9
- };
18
+ });
19
+ function vercelPlugin(config = {}) {
20
+ return factory(config);
21
+ }
10
22
  async function createVercelHandler(app) {
11
23
  await app.boot();
12
24
  return {
@@ -25,18 +37,32 @@ function buildVercelOutput(options = {}) {
25
37
  const docsDir = resolvePathLike(options.docsDir ?? resolveNearestDocsDir(root) ?? resolve(root, "docs"));
26
38
  const ssrDir = resolvePathLike(options.ssrDir ?? resolve(root, ".guren/ssr"));
27
39
  const migrationsDir = resolvePathLike(options.migrationsDir ?? resolve(root, "db/migrations"));
28
- if (existsSync(out)) {
29
- rmSync(out, { recursive: true, force: true });
40
+ assertOutputDirOutsideRoot(out, root, LABEL);
41
+ if (!existsSync(entrypoint)) {
42
+ throw new Error(
43
+ `${LABEL}: entrypoint not found at ${entrypoint}. Run \`bunx guren plugin @guren/plugin-vercel\` to scaffold src/vercel.ts, or pass "entrypoint".`
44
+ );
30
45
  }
46
+ const env = buildVercelEnvironment(publicDir, ssrDir);
47
+ resetOutputDir(out, root, LABEL);
31
48
  mkdirSync(funcDir, { recursive: true });
32
49
  mkdirSync(resolve(out, "static"), { recursive: true });
33
- const env = buildVercelEnvironment(root);
34
50
  writeFileSync(
35
51
  resolve(out, "config.json"),
36
52
  JSON.stringify(
37
53
  {
38
54
  version: 3,
39
55
  routes: [
56
+ // Built assets self-reference the Vite plugin's derived base,
57
+ // `/public/assets/`, while the files themselves are copied to the
58
+ // output root. Without this the entry script still loads — its path
59
+ // is injected directly — but every chunk it imports falls through
60
+ // to the function and comes back as HTML.
61
+ //
62
+ // A `rewrites` entry in vercel.json only covers builds Vercel runs
63
+ // itself; a `--prebuilt` upload is routed by this file alone, which
64
+ // is the flow the deployment guide documents.
65
+ { src: "/public/(.*)", dest: "/$1" },
40
66
  { handle: "filesystem" },
41
67
  { src: "/(.*)", dest: "/index" }
42
68
  ]
@@ -62,6 +88,16 @@ function buildVercelOutput(options = {}) {
62
88
  const result = Bun.spawnSync({
63
89
  // `bun build` inlines `process.env.NODE_ENV` at bundle time (defaulting to
64
90
  // "development"), so pin it to "production" for the deployed function.
91
+ //
92
+ // Whitespace and syntax only — never plain `--minify`, which also mangles
93
+ // identifiers. Guren keys durable records on class names: the queue
94
+ // registry stores each job's wire name (its class name unless it declares a
95
+ // jobName) in every queued message, and notifications persist
96
+ // `constructor.name` as their `type`. Mangled, a job dispatched by one
97
+ // deploy resolves to nothing after the next.
98
+ //
99
+ // Not `--keep-names`: as of Bun 1.3.14 it is accepted and silently leaves
100
+ // class names mangled, so it cannot replace this.
65
101
  cmd: [
66
102
  "bun",
67
103
  "build",
@@ -70,7 +106,8 @@ function buildVercelOutput(options = {}) {
70
106
  funcDir,
71
107
  "--target",
72
108
  "bun",
73
- "--minify",
109
+ "--minify-whitespace",
110
+ "--minify-syntax",
74
111
  "--define",
75
112
  'process.env.NODE_ENV="production"'
76
113
  ],
@@ -94,45 +131,33 @@ function buildVercelOutput(options = {}) {
94
131
  cpSync(publicDir, resolve(out, "static"), { recursive: true });
95
132
  }
96
133
  }
97
- function buildVercelEnvironment(root) {
134
+ function buildVercelEnvironment(publicDir, ssrDir) {
98
135
  const env = {
99
136
  NODE_ENV: "production",
100
137
  BUN_RUNTIME_TRANSPILER_CACHE_PATH: "/tmp/.bun-cache",
101
138
  BUN_INSTALL_CACHE_DIR: "/tmp/.bun-install-cache",
102
139
  TMPDIR: "/tmp"
103
140
  };
104
- const clientManifest = loadManifest(
105
- resolve(root, "public/assets/.vite/manifest.json"),
106
- resolve(root, "public/assets/manifest.json")
107
- );
108
- if (clientManifest) {
109
- const entry = clientManifest["resources/js/app.tsx"];
110
- if (entry?.file) {
111
- env.GUREN_INERTIA_ENTRY = `/assets/${entry.file}`;
112
- }
113
- if (entry?.css?.length) {
114
- env.GUREN_INERTIA_STYLES = entry.css.map((file) => `/assets/${file}`).join(",");
115
- }
141
+ const assetEnv = resolveClientAssetEnv(publicDir, "resources/js/app.tsx", LABEL);
142
+ if (assetEnv.entry) {
143
+ env.GUREN_INERTIA_ENTRY = assetEnv.entry;
116
144
  }
117
- const ssrManifest = loadManifest(
118
- resolve(root, ".guren/ssr/.vite/manifest.json"),
119
- resolve(root, ".guren/ssr/manifest.json")
120
- );
121
- if (ssrManifest) {
122
- const ssrEntry = ssrManifest["resources/js/ssr.tsx"];
123
- if (ssrEntry?.file) {
124
- env.GUREN_INERTIA_SSR_ENTRY = `./.guren/ssr/${ssrEntry.file}`;
145
+ if (assetEnv.styles) {
146
+ env.GUREN_INERTIA_STYLES = assetEnv.styles;
147
+ }
148
+ const ssrFile = resolveSsrEntryFile(ssrDir, "resources/js/ssr.tsx", LABEL);
149
+ if (ssrFile) {
150
+ const ssrPaths = ssrRuntimePaths(ssrDir, ssrFile, "./.guren/ssr");
151
+ env.GUREN_INERTIA_SSR_ENTRY = ssrPaths.entry;
152
+ if (ssrPaths.manifest) {
153
+ env.GUREN_INERTIA_SSR_MANIFEST = ssrPaths.manifest;
125
154
  }
126
- env.GUREN_INERTIA_SSR_MANIFEST = "./.guren/ssr/.vite/manifest.json";
127
155
  }
128
156
  env.GUREN_INERTIA_IMPORT_MAP = JSON.stringify({
129
157
  "@guren/inertia-client": "/vendor/inertia-client.tsx"
130
158
  });
131
159
  return env;
132
160
  }
133
- function resolvePathLike(value) {
134
- return value instanceof URL ? fileURLToPath(value) : resolve(String(value));
135
- }
136
161
  function resolveNearestDocsDir(startDir, maxDepth = 6) {
137
162
  let currentDir = startDir;
138
163
  for (let depth = 0; depth < maxDepth; depth += 1) {
@@ -148,21 +173,8 @@ function resolveNearestDocsDir(startDir, maxDepth = 6) {
148
173
  }
149
174
  return void 0;
150
175
  }
151
- function loadManifest(...paths) {
152
- for (const path of paths) {
153
- if (!existsSync(path)) {
154
- continue;
155
- }
156
- try {
157
- return JSON.parse(readFileSync(path, "utf8"));
158
- } catch {
159
- continue;
160
- }
161
- }
162
- return void 0;
163
- }
164
176
  export {
165
- GurenPluginVercelProvider,
166
177
  buildVercelOutput,
167
- createVercelHandler
178
+ createVercelHandler,
179
+ vercelPlugin
168
180
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@guren/plugin-vercel",
3
- "version": "0.1.2",
3
+ "version": "0.2.1",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -16,8 +16,7 @@
16
16
  "access": "public"
17
17
  },
18
18
  "gurenPlugin": {
19
- "compatibility": ">=1.0.0 <2.0.0",
20
- "provider": "GurenPluginVercelProvider"
19
+ "compatibility": ">=1.0.0 <2.0.0"
21
20
  },
22
21
  "files": [
23
22
  "dist"
@@ -36,7 +35,7 @@
36
35
  "typecheck": "tsc --noEmit"
37
36
  },
38
37
  "dependencies": {
39
- "@guren/core": "^1.1.0"
38
+ "@guren/core": "^1.4.0"
40
39
  },
41
40
  "devDependencies": {
42
41
  "@types/node": "^20.14.10",