@guren/plugin-vercel 0.2.1 → 0.3.0

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 CHANGED
@@ -1,5 +1,5 @@
1
1
  import { ServiceProviderConstructor } from '@guren/core';
2
- import { PathLike } from '@guren/core/internal/deploy-build';
2
+ import { PathLike, DatabaseDialect } from '@guren/core/internal/deploy-build';
3
3
 
4
4
  interface VercelAppLike {
5
5
  boot(): Promise<void>;
@@ -16,6 +16,13 @@ interface BuildVercelOutputOptions {
16
16
  docsDir?: PathLike;
17
17
  ssrDir?: PathLike;
18
18
  migrationsDir?: PathLike;
19
+ /**
20
+ * Databases this app connects to, overriding what is read from
21
+ * `config/database.ts`. Every other dialect's client is stubbed out of the
22
+ * bundle. Pass this when the config reaches a factory without naming it and
23
+ * the build reports that it could not tell.
24
+ */
25
+ databaseDialects?: readonly DatabaseDialect[];
19
26
  }
20
27
  /**
21
28
  * Configuration for the Vercel plugin. Currently empty — reserved so future
@@ -33,6 +40,6 @@ interface VercelPluginConfig {
33
40
  */
34
41
  declare function vercelPlugin(config?: VercelPluginConfig): ServiceProviderConstructor;
35
42
  declare function createVercelHandler(app: VercelAppLike): Promise<VercelHandler>;
36
- declare function buildVercelOutput(options?: BuildVercelOutputOptions): void;
43
+ declare function buildVercelOutput(options?: BuildVercelOutputOptions): Promise<void>;
37
44
 
38
45
  export { type BuildVercelOutputOptions, type VercelAppLike, type VercelHandler, type VercelPluginConfig, buildVercelOutput, createVercelHandler, vercelPlugin };
package/dist/index.js CHANGED
@@ -4,11 +4,15 @@ import { basename, extname, resolve } from "path";
4
4
  import { definePlugin } from "@guren/core";
5
5
  import {
6
6
  assertOutputDirOutsideRoot,
7
+ DEV_ONLY_MODULES,
8
+ MCP_SDK_SUBPATH_PREFIX,
9
+ renderDevOnlyStub,
7
10
  resetOutputDir,
8
11
  resolveClientAssetEnv,
9
12
  resolvePathLike,
10
13
  resolveSsrEntryFile,
11
- ssrRuntimePaths
14
+ ssrRuntimePaths,
15
+ unusedSqlClients
12
16
  } from "@guren/core/internal/deploy-build";
13
17
  var LABEL = "Vercel build";
14
18
  var factory = definePlugin({
@@ -27,7 +31,7 @@ async function createVercelHandler(app) {
27
31
  }
28
32
  };
29
33
  }
30
- function buildVercelOutput(options = {}) {
34
+ async function buildVercelOutput(options = {}) {
31
35
  const root = resolvePathLike(options.rootDir ?? new URL("..", import.meta.url));
32
36
  const out = resolvePathLike(options.outputDir ?? resolve(root, ".vercel/output"));
33
37
  const funcDir = resolve(out, "functions/index.func");
@@ -85,39 +89,7 @@ function buildVercelOutput(options = {}) {
85
89
  2
86
90
  )
87
91
  );
88
- const result = Bun.spawnSync({
89
- // `bun build` inlines `process.env.NODE_ENV` at bundle time (defaulting to
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.
101
- cmd: [
102
- "bun",
103
- "build",
104
- entrypoint,
105
- "--outdir",
106
- funcDir,
107
- "--target",
108
- "bun",
109
- "--minify-whitespace",
110
- "--minify-syntax",
111
- "--define",
112
- 'process.env.NODE_ENV="production"'
113
- ],
114
- cwd: root,
115
- stdout: "inherit",
116
- stderr: "inherit"
117
- });
118
- if (result.exitCode !== 0) {
119
- throw new Error("bun build failed");
120
- }
92
+ await bundleFunction({ entrypoint, funcDir, root, dialects: options.databaseDialects });
121
93
  if (existsSync(ssrDir)) {
122
94
  cpSync(ssrDir, resolve(funcDir, ".guren/ssr"), { recursive: true });
123
95
  }
@@ -131,6 +103,75 @@ function buildVercelOutput(options = {}) {
131
103
  cpSync(publicDir, resolve(out, "static"), { recursive: true });
132
104
  }
133
105
  }
106
+ var MCP_UNAVAILABLE = "The MCP endpoint is unavailable on Vercel \u2014 it generates files on disk, and the function filesystem is read-only.";
107
+ var UNAVAILABLE_ON_VERCEL = {
108
+ sqlite: null,
109
+ vite: "The Vite dev server is unavailable on Vercel \u2014 assets are served from the static output directory.",
110
+ mcp: MCP_UNAVAILABLE
111
+ };
112
+ function stubbedModules(root, dialects) {
113
+ const devOnly = DEV_ONLY_MODULES.flatMap((module) => {
114
+ const message = UNAVAILABLE_ON_VERCEL[module.kind];
115
+ return message === null ? [] : [[module.specifier, renderDevOnlyStub(module, message)]];
116
+ });
117
+ const unused = unusedSqlClients({ root, label: LABEL, dialects }).map(({ module, message }) => [
118
+ module.specifier,
119
+ renderDevOnlyStub(module, message)
120
+ ]);
121
+ return Object.fromEntries([...devOnly, ...unused]);
122
+ }
123
+ var unlistedMcpStub = `throw new Error(${JSON.stringify(MCP_UNAVAILABLE)})
124
+ `;
125
+ async function bundleFunction(input) {
126
+ const stubs = stubbedModules(input.root, input.dialects);
127
+ const filter = new RegExp(
128
+ `^(?:${[...Object.keys(stubs).map(escapeRegExp), `${escapeRegExp(MCP_SDK_SUBPATH_PREFIX)}.+`].join("|")})$`
129
+ );
130
+ const result = await Bun.build({
131
+ entrypoints: [input.entrypoint],
132
+ // `Bun.build` rejects with a bare "Bundle failed" AggregateError by
133
+ // default (Bun >= 1.2), which discards the one line that matters — the
134
+ // module it could not resolve. Opting out of that is what makes the
135
+ // `result.logs` report below reachable at all.
136
+ throw: false,
137
+ outdir: input.funcDir,
138
+ target: "bun",
139
+ // Whitespace and syntax only — never plain `minify: true`, which also
140
+ // mangles identifiers. Guren keys durable records on class names: the
141
+ // queue registry stores each job's wire name (its class name unless it
142
+ // declares a jobName) in every queued message, and notifications persist
143
+ // `constructor.name` as their `type`. Mangled, a job dispatched by one
144
+ // deploy resolves to nothing after the next.
145
+ //
146
+ // Not `keepNames`: as of Bun 1.3.14 it is accepted and silently leaves
147
+ // class names mangled, so it cannot replace this.
148
+ minify: { whitespace: true, syntax: true, identifiers: false },
149
+ define: {
150
+ // `bun build` inlines `process.env.NODE_ENV` at bundle time (defaulting
151
+ // to "development"), so pin it to "production" for the deployed function.
152
+ "process.env.NODE_ENV": '"production"'
153
+ },
154
+ plugins: [
155
+ {
156
+ name: "guren-vercel-stubs",
157
+ setup(build) {
158
+ build.onResolve({ filter }, (args) => ({ path: args.path, namespace: "guren-vercel-stub" }));
159
+ build.onLoad({ filter: /.*/, namespace: "guren-vercel-stub" }, (args) => ({
160
+ contents: stubs[args.path] ?? unlistedMcpStub,
161
+ loader: "js"
162
+ }));
163
+ }
164
+ }
165
+ ]
166
+ });
167
+ if (!result.success) {
168
+ throw new Error(`${LABEL}: bun build failed.
169
+ ${result.logs.map((log) => String(log)).join("\n")}`);
170
+ }
171
+ }
172
+ function escapeRegExp(value) {
173
+ return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
174
+ }
134
175
  function buildVercelEnvironment(publicDir, ssrDir) {
135
176
  const env = {
136
177
  NODE_ENV: "production",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@guren/plugin-vercel",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -35,7 +35,7 @@
35
35
  "typecheck": "tsc --noEmit"
36
36
  },
37
37
  "dependencies": {
38
- "@guren/core": "^1.4.0"
38
+ "@guren/core": "^1.6.2"
39
39
  },
40
40
  "devDependencies": {
41
41
  "@types/node": "^20.14.10",