@guren/plugin-vercel 0.4.0 → 0.5.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/dist/index.d.ts CHANGED
@@ -29,14 +29,7 @@ interface BuildVercelOutputOptions {
29
29
  * fields never force another registration-shape change.
30
30
  */
31
31
  interface VercelPluginConfig {}
32
- /**
33
- * Register the Vercel plugin.
34
- *
35
- * @example
36
- * ```typescript
37
- * createApp({ providers: [vercelPlugin()] })
38
- * ```
39
- */
32
+ /** Register the Vercel plugin. */
40
33
  declare function vercelPlugin(config?: VercelPluginConfig): ServiceProviderConstructor;
41
34
  declare function createVercelHandler(app: VercelAppLike): Promise<VercelHandler>;
42
35
  declare function buildVercelOutput(options?: BuildVercelOutputOptions): Promise<void>;
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { cpSync, existsSync, mkdirSync, writeFileSync } from "node:fs";
2
2
  import { basename, extname, resolve } from "node:path";
3
3
  import { definePlugin } from "@guren/core";
4
- import { DEV_ONLY_MODULES, MCP_SDK_SUBPATH_PREFIX, assertOutputDirOutsideRoot, clientManifestJson, renderDevOnlyStub, resetOutputDir, resolveClientAssetEnv, resolvePathLike, resolveSsrEntryFile, ssrRuntimePaths, unusedSqlClients } from "@guren/core/internal/deploy-build";
4
+ import { DOCUMENT_ASSET_EXTENSIONS, DOCUMENT_ASSET_HEADERS, MCP_SDK_SUBPATH_PREFIX, appUsesMcpPlugin, assertOutputDirOutsideRoot, clientManifestJson, removeShadowingIndex, renderDevOnlyStub, resetOutputDir, resolveClientAssetEnv, resolvePathLike, resolveSsrEntryFile, ssrRuntimePaths, stubbableDevOnlyModules, unusedSqlClients } from "@guren/core/internal/deploy-build";
5
5
  //#region src/index.ts
6
6
  /** Prefixes every diagnostic this build emits. */
7
7
  const LABEL = "Vercel build";
@@ -9,14 +9,7 @@ const factory = definePlugin({
9
9
  name: "vercel",
10
10
  register() {}
11
11
  });
12
- /**
13
- * Register the Vercel plugin.
14
- *
15
- * @example
16
- * ```typescript
17
- * createApp({ providers: [vercelPlugin()] })
18
- * ```
19
- */
12
+ /** Register the Vercel plugin. */
20
13
  function vercelPlugin(config = {}) {
21
14
  return factory(config);
22
15
  }
@@ -53,6 +46,12 @@ async function buildVercelOutput(options = {}) {
53
46
  {
54
47
  src: "/(.*)",
55
48
  dest: "/index"
49
+ },
50
+ { handle: "hit" },
51
+ {
52
+ src: documentAssetPattern(),
53
+ headers: { ...DOCUMENT_ASSET_HEADERS },
54
+ continue: true
56
55
  }
57
56
  ]
58
57
  }, null, 2));
@@ -73,22 +72,28 @@ async function buildVercelOutput(options = {}) {
73
72
  if (existsSync(ssrDir)) cpSync(ssrDir, resolve(funcDir, ".guren/ssr"), { recursive: true });
74
73
  if (existsSync(migrationsDir)) cpSync(migrationsDir, resolve(funcDir, "db/migrations"), { recursive: true });
75
74
  if (existsSync(docsDir)) cpSync(docsDir, resolve(funcDir, "docs"), { recursive: true });
76
- if (existsSync(publicDir)) cpSync(publicDir, resolve(out, "static"), { recursive: true });
75
+ if (existsSync(publicDir)) {
76
+ cpSync(publicDir, resolve(out, "static"), { recursive: true });
77
+ removeShadowingIndex(resolve(out, "static"));
78
+ }
79
+ }
80
+ /**
81
+ * Matches a staged path whose extension a browser would render as a document;
82
+ * the CDN serves `.vercel/output/static` ahead of the function, so the
83
+ * framework's own guard never sees these files. A character class per letter
84
+ * because Vercel compiles `src` case-sensitively (a plain extension leaves
85
+ * logo.SVG inline) and validates it as a JavaScript `RegExp`, so no inline flag.
86
+ */
87
+ function documentAssetPattern() {
88
+ return `^/.*\\.(?:${DOCUMENT_ASSET_EXTENSIONS.map((extension) => [...extension].map((character) => `[${character}${character.toUpperCase()}]`).join("")).join("|")})$`;
77
89
  }
78
90
  const MCP_UNAVAILABLE = "The MCP endpoint is unavailable on Vercel — it generates files on disk, and the function filesystem is read-only.";
79
91
  /**
80
- * Why each dev-only module cannot run here, or `null` for one that can and is
81
- * therefore left alone.
82
- *
83
- * `sqlite` is the `null`, and it is written as an entry rather than filtered
84
- * out somewhere else so the decision sits where the reason does: the function
85
- * runs on Vercel's Bun runtime, so `bun:sqlite` is a working database here —
86
- * unlike on Workers and Lambda, whose tables name a replacement for it.
87
- * Stubbing it would break an app that ships a read-only sqlite file beside
88
- * its function.
89
- *
90
- * Keyed on every kind `DEV_ONLY_MODULES` contains, so a kind added there is a
91
- * compile error here rather than a module that silently stubs to `undefined`.
92
+ * Why each dev-only module cannot run here, or `null` for one that can. `sqlite`
93
+ * is the `null`: the function runs on Vercel's Bun runtime, so `bun:sqlite` works
94
+ * and stubbing it would break an app shipping a read-only sqlite file beside its
95
+ * function. Keyed on every kind `DEV_ONLY_MODULES` contains, so a kind added
96
+ * there is a compile error here.
92
97
  */
93
98
  const UNAVAILABLE_ON_VERCEL = {
94
99
  sqlite: null,
@@ -96,22 +101,14 @@ const UNAVAILABLE_ON_VERCEL = {
96
101
  mcp: MCP_UNAVAILABLE
97
102
  };
98
103
  /**
99
- * Modules replaced with throwing stubs, in the order they are matched.
100
- *
101
- * Two separate reasons, both of them the same defect shape — a *literal*
102
- * dynamic import of a package the app never installed, which a bundler
103
- * follows whether or not the branch can be taken:
104
- *
105
- * - The dev-only ones. A scaffolded app could not be bundled for Vercel at
106
- * all: the disabled MCP endpoint's `import("@guren/cli")` resolves, and the
107
- * CLI's own `import("@guren/openapi")` behind it does not. Lambda never saw
108
- * this because it has stubbed these since it shipped.
109
- * - The database clients for dialects the app does not use, decided per app
110
- * rather than per platform — the ones it *does* use are load-bearing here,
111
- * unlike on Workers where D1 is the only database.
104
+ * Modules replaced with throwing stubs, in match order. One defect shape — a
105
+ * *literal* dynamic import of a package the app never installed, followed by a
106
+ * bundler regardless of branch: dev-only modules, and SQL clients of unused
107
+ * dialects. An app declaring `@guren/plugin-mcp` serves the App MCP endpoint here,
108
+ * so its transport must reach the bundle (RFC 0016 §7); the *Dev* MCP stays stubbed.
112
109
  */
113
- function stubbedModules(root, dialects) {
114
- const devOnly = DEV_ONLY_MODULES.flatMap((module) => {
110
+ function stubbedModules(root, dialects, mcpPlugin) {
111
+ const devOnly = stubbableDevOnlyModules({ mcpPlugin }).flatMap((module) => {
115
112
  const message = UNAVAILABLE_ON_VERCEL[module.kind];
116
113
  return message === null ? [] : [[module.specifier, renderDevOnlyStub(module, message)]];
117
114
  });
@@ -124,12 +121,20 @@ function stubbedModules(root, dialects) {
124
121
  }
125
122
  /**
126
123
  * Fallback for an MCP SDK subpath `DEV_ONLY_MODULES` does not name. It cannot
127
- * know which names the importer destructures, so it throws on evaluation
128
- * rather than resolving to an empty module: a subpath reached from app code
129
- * (not Guren's disabled MCP endpoint) must fail loudly at build or cold start,
130
- * never silently hand back missing exports.
124
+ * know which names the importer destructures, so it throws on evaluation rather
125
+ * than resolving to an empty module. Reachable only for an app that does *not*
126
+ * declare `@guren/plugin-mcp`, whose statically imported `server/index.js` and
127
+ * `types.js` this would otherwise leave compiled shut.
131
128
  */
132
129
  const unlistedMcpStub = `throw new Error(${JSON.stringify(MCP_UNAVAILABLE)})\n`;
130
+ function escapeRegExp(value) {
131
+ return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
132
+ }
133
+ function stubFilter(stubs, mcpPlugin) {
134
+ const terms = Object.keys(stubs).map(escapeRegExp);
135
+ if (!mcpPlugin) terms.push(`${escapeRegExp(MCP_SDK_SUBPATH_PREFIX)}.+`);
136
+ return new RegExp(`^(?:${terms.join("|")})$`);
137
+ }
133
138
  /**
134
139
  * Bundle the function with Bun's JS API rather than by spawning `bun build`.
135
140
  *
@@ -138,8 +143,9 @@ const unlistedMcpStub = `throw new Error(${JSON.stringify(MCP_UNAVAILABLE)})\n`;
138
143
  * all while the other two did. The API takes plugins.
139
144
  */
140
145
  async function bundleFunction(input) {
141
- const stubs = stubbedModules(input.root, input.dialects);
142
- const filter = new RegExp(`^(?:${[...Object.keys(stubs).map(escapeRegExp), `${escapeRegExp(MCP_SDK_SUBPATH_PREFIX)}.+`].join("|")})$`);
146
+ const mcpPlugin = appUsesMcpPlugin(input.root);
147
+ const stubs = stubbedModules(input.root, input.dialects, mcpPlugin);
148
+ const filter = stubFilter(stubs, mcpPlugin);
143
149
  const result = await Bun.build({
144
150
  entrypoints: [input.entrypoint],
145
151
  throw: false,
@@ -173,9 +179,6 @@ async function bundleFunction(input) {
173
179
  });
174
180
  if (!result.success) throw new Error(`${LABEL}: bun build failed.\n${result.logs.map((log) => String(log)).join("\n")}`);
175
181
  }
176
- function escapeRegExp(value) {
177
- return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
178
- }
179
182
  function buildVercelEnvironment(publicDir, ssrDir) {
180
183
  const env = {
181
184
  NODE_ENV: "production",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@guren/plugin-vercel",
3
- "version": "0.4.0",
3
+ "version": "0.5.1",
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.10.0"
38
+ "@guren/core": "^1.13.1"
39
39
  },
40
40
  "devDependencies": {
41
41
  "@types/node": "^20.14.10",