@guren/plugin-cloudflare 0.3.1 → 0.4.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/build-B7zQ13ai.js +321 -0
- package/dist/commands.d.ts +13 -14
- package/dist/commands.js +25 -33
- package/dist/index.d.ts +155 -152
- package/dist/index.js +341 -348
- package/package.json +5 -5
- package/dist/chunk-6DV73MBR.js +0 -321
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@guren/plugin-cloudflare",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -36,19 +36,19 @@
|
|
|
36
36
|
}
|
|
37
37
|
},
|
|
38
38
|
"scripts": {
|
|
39
|
-
"build": "
|
|
39
|
+
"build": "bun --bun tsdown",
|
|
40
40
|
"test": "bun test",
|
|
41
41
|
"typecheck": "tsc --noEmit"
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@guren/core": "^1.
|
|
44
|
+
"@guren/core": "^1.10.0",
|
|
45
45
|
"citty": "^0.1.6"
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
48
|
"@cloudflare/workers-types": "^4.20260702.1",
|
|
49
49
|
"@types/node": "^20.14.10",
|
|
50
50
|
"miniflare": "5.20260801.1-alpha",
|
|
51
|
-
"
|
|
52
|
-
"typescript": "^
|
|
51
|
+
"tsdown": "^0.22.14",
|
|
52
|
+
"typescript": "^7.0.2"
|
|
53
53
|
}
|
|
54
54
|
}
|
package/dist/chunk-6DV73MBR.js
DELETED
|
@@ -1,321 +0,0 @@
|
|
|
1
|
-
// src/build.ts
|
|
2
|
-
import { cpSync, existsSync, mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from "fs";
|
|
3
|
-
import { relative, resolve, sep } from "path";
|
|
4
|
-
import { pathToFileURL } from "url";
|
|
5
|
-
import {
|
|
6
|
-
DEV_ONLY_MODULES,
|
|
7
|
-
SQL_CLIENT_MODULES,
|
|
8
|
-
importSpecifier,
|
|
9
|
-
renderDevOnlyStub,
|
|
10
|
-
assertOutputDirOutsideRoot,
|
|
11
|
-
resetOutputDir,
|
|
12
|
-
resolveClientAssetEnv,
|
|
13
|
-
resolvePathLike,
|
|
14
|
-
resolveSsrEntryFile,
|
|
15
|
-
stageStaticAssets
|
|
16
|
-
} from "@guren/core/internal/deploy-build";
|
|
17
|
-
async function buildCloudflareOutput(options = {}) {
|
|
18
|
-
const root = resolvePathLike(options.rootDir ?? process.cwd());
|
|
19
|
-
const out = resolvePathLike(options.outputDir ?? resolve(root, ".cloudflare"));
|
|
20
|
-
const appEntry = resolvePathLike(options.appEntry ?? resolve(root, "src/app.ts"));
|
|
21
|
-
const publicDir = resolvePathLike(options.publicDir ?? resolve(root, "public"));
|
|
22
|
-
const ssrDir = resolvePathLike(options.ssrDir ?? resolve(root, ".guren/ssr"));
|
|
23
|
-
const clientEntryKey = options.clientEntryKey ?? "resources/js/app.tsx";
|
|
24
|
-
const ssrEntryKey = options.ssrEntryKey ?? "resources/js/ssr.tsx";
|
|
25
|
-
assertOutputDirOutsideRoot(out, root, "Cloudflare build");
|
|
26
|
-
const packageJson = readPackageJson(root);
|
|
27
|
-
if (!options.skipAppBuild) {
|
|
28
|
-
runAppBuild(root, packageJson.scripts ?? {});
|
|
29
|
-
}
|
|
30
|
-
if (!existsSync(appEntry)) {
|
|
31
|
-
throw new Error(`Cloudflare build: app entry not found at ${appEntry}. Pass "appEntry" if your Application lives elsewhere.`);
|
|
32
|
-
}
|
|
33
|
-
const ssrImport = await resolveSsrImport(ssrDir, ssrEntryKey);
|
|
34
|
-
const assetEnv = resolveClientAssetEnv(publicDir, clientEntryKey, "Cloudflare build");
|
|
35
|
-
resetOutputDir(out, root, "Cloudflare build");
|
|
36
|
-
stageStaticAssets(publicDir, resolve(out, "assets"));
|
|
37
|
-
writeFileSync(resolve(out, "worker.js"), renderWorkerModule({ out, appEntry, ssrImport, assetEnv }));
|
|
38
|
-
flattenD1Migrations(resolve(root, "db/migrations"), resolve(out, "d1-migrations"));
|
|
39
|
-
writeDevOnlyStubs(out);
|
|
40
|
-
scaffoldWranglerConfig(root, out, packageJson.name);
|
|
41
|
-
}
|
|
42
|
-
var MCP_UNAVAILABLE = "The MCP endpoint is unavailable on Cloudflare Workers \u2014 it generates files on disk.";
|
|
43
|
-
var STUBBED_MODULES = [...DEV_ONLY_MODULES, ...SQL_CLIENT_MODULES];
|
|
44
|
-
var UNAVAILABLE_ON_WORKERS = {
|
|
45
|
-
sqlite: "bun:sqlite is unavailable on Cloudflare Workers \u2014 use createD1Database().",
|
|
46
|
-
vite: "The Vite dev server is unavailable on Cloudflare Workers \u2014 assets are served by Workers Static Assets.",
|
|
47
|
-
mcp: MCP_UNAVAILABLE,
|
|
48
|
-
"sql-driver": "This database client is unavailable on Cloudflare Workers \u2014 use createD1Database(). It is stubbed because @guren/orm names it in a dynamic import that bundlers follow even when the branch cannot be taken."
|
|
49
|
-
};
|
|
50
|
-
var STUB_FILES = {
|
|
51
|
-
"bun:sqlite": "stub-bun-sqlite.js",
|
|
52
|
-
vite: "stub-vite.js",
|
|
53
|
-
"@guren/cli": "stub-guren-cli.js",
|
|
54
|
-
"@modelcontextprotocol/sdk/server/mcp.js": "stub-mcp-server.js",
|
|
55
|
-
"@modelcontextprotocol/sdk/server/webStandardStreamableHttp.js": "stub-mcp-transport.js",
|
|
56
|
-
postgres: "stub-postgres.js",
|
|
57
|
-
mysql2: "stub-mysql2.js",
|
|
58
|
-
"mysql2/promise": "stub-mysql2-promise.js",
|
|
59
|
-
"@aws-sdk/client-rds-data": "stub-rds-data.js"
|
|
60
|
-
};
|
|
61
|
-
function writeDevOnlyStubs(out) {
|
|
62
|
-
for (const module of STUBBED_MODULES) {
|
|
63
|
-
writeFileSync(
|
|
64
|
-
resolve(out, STUB_FILES[module.specifier]),
|
|
65
|
-
renderDevOnlyStub(module, UNAVAILABLE_ON_WORKERS[module.kind])
|
|
66
|
-
);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
function devOnlyAliases(outRelative) {
|
|
70
|
-
return Object.fromEntries(
|
|
71
|
-
STUBBED_MODULES.map((module) => [
|
|
72
|
-
module.specifier,
|
|
73
|
-
`./${outRelative}/${STUB_FILES[module.specifier]}`
|
|
74
|
-
])
|
|
75
|
-
);
|
|
76
|
-
}
|
|
77
|
-
function flattenD1Migrations(migrationsDir, outDir) {
|
|
78
|
-
if (!existsSync(migrationsDir)) {
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
const entries = readdirSync(migrationsDir, { withFileTypes: true });
|
|
82
|
-
const copies = [];
|
|
83
|
-
for (const entry of entries) {
|
|
84
|
-
if (entry.isFile() && entry.name.endsWith(".sql")) {
|
|
85
|
-
copies.push({ from: resolve(migrationsDir, entry.name), to: entry.name });
|
|
86
|
-
continue;
|
|
87
|
-
}
|
|
88
|
-
if (entry.isDirectory() && entry.name !== "meta") {
|
|
89
|
-
const nested = resolve(migrationsDir, entry.name, "migration.sql");
|
|
90
|
-
if (existsSync(nested)) {
|
|
91
|
-
copies.push({ from: nested, to: `${entry.name}.sql` });
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
const seen = /* @__PURE__ */ new Map();
|
|
96
|
-
for (const copy of copies) {
|
|
97
|
-
const clash = seen.get(copy.to);
|
|
98
|
-
if (clash) {
|
|
99
|
-
throw new Error(
|
|
100
|
-
`Cloudflare build: migrations "${clash}" and "${copy.from}" both flatten to "${copy.to}". Rename one so wrangler sees a stable order.`
|
|
101
|
-
);
|
|
102
|
-
}
|
|
103
|
-
seen.set(copy.to, copy.from);
|
|
104
|
-
}
|
|
105
|
-
if (existsSync(outDir)) {
|
|
106
|
-
rmSync(outDir, { recursive: true, force: true });
|
|
107
|
-
}
|
|
108
|
-
if (copies.length === 0) {
|
|
109
|
-
return;
|
|
110
|
-
}
|
|
111
|
-
mkdirSync(outDir, { recursive: true });
|
|
112
|
-
for (const copy of copies) {
|
|
113
|
-
cpSync(copy.from, resolve(outDir, copy.to));
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
function readPackageJson(root) {
|
|
117
|
-
const packageJsonPath = resolve(root, "package.json");
|
|
118
|
-
if (!existsSync(packageJsonPath)) {
|
|
119
|
-
return {};
|
|
120
|
-
}
|
|
121
|
-
try {
|
|
122
|
-
return JSON.parse(readFileSync(packageJsonPath, "utf8"));
|
|
123
|
-
} catch {
|
|
124
|
-
return {};
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
function runAppBuild(root, scripts) {
|
|
128
|
-
if (!scripts.build) {
|
|
129
|
-
throw new Error(
|
|
130
|
-
'Cloudflare build: no "build" script found in package.json. Add one (codegen + vite build + vite build --ssr) or pass --skip-app-build after building manually.'
|
|
131
|
-
);
|
|
132
|
-
}
|
|
133
|
-
const result = Bun.spawnSync({
|
|
134
|
-
cmd: ["bun", "run", "build"],
|
|
135
|
-
cwd: root,
|
|
136
|
-
stdout: "inherit",
|
|
137
|
-
stderr: "inherit"
|
|
138
|
-
});
|
|
139
|
-
if (result.exitCode !== 0) {
|
|
140
|
-
throw new Error('Cloudflare build: the app "build" script failed.');
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
async function resolveSsrImport(ssrDir, ssrEntryKey) {
|
|
144
|
-
const file = resolveSsrEntryFile(ssrDir, ssrEntryKey, "Cloudflare build");
|
|
145
|
-
if (!file) {
|
|
146
|
-
console.warn(
|
|
147
|
-
`Cloudflare build: no SSR manifest entry for "${ssrEntryKey}" under ${ssrDir}; generating a CSR-only worker.`
|
|
148
|
-
);
|
|
149
|
-
return void 0;
|
|
150
|
-
}
|
|
151
|
-
const module = await import(pathToFileURL(file).href);
|
|
152
|
-
const rendererExport = ["render", "default"].find(
|
|
153
|
-
(name) => typeof module[name] === "function"
|
|
154
|
-
);
|
|
155
|
-
if (!rendererExport) {
|
|
156
|
-
throw new Error(
|
|
157
|
-
`Cloudflare build: SSR entry ${file} does not export a renderer (expected a named "render" or default export).`
|
|
158
|
-
);
|
|
159
|
-
}
|
|
160
|
-
return { file, rendererExport };
|
|
161
|
-
}
|
|
162
|
-
function renderWorkerModule(input) {
|
|
163
|
-
const lines = [
|
|
164
|
-
"// Generated by `guren cloudflare:build`. Do not edit \u2014 regenerate instead.",
|
|
165
|
-
"import { createWorkersHandler } from '@guren/plugin-cloudflare'"
|
|
166
|
-
];
|
|
167
|
-
if (input.ssrImport) {
|
|
168
|
-
lines.push(
|
|
169
|
-
"import { setInertiaSsrRenderer } from '@guren/core'",
|
|
170
|
-
`import * as ssrModule from ${JSON.stringify(importSpecifier(input.out, input.ssrImport.file, "Cloudflare build"))}`
|
|
171
|
-
);
|
|
172
|
-
}
|
|
173
|
-
lines.push(`import app from ${JSON.stringify(importSpecifier(input.out, input.appEntry, "Cloudflare build"))}`, "");
|
|
174
|
-
if (input.assetEnv.entry) {
|
|
175
|
-
lines.push(`process.env.GUREN_INERTIA_ENTRY = ${JSON.stringify(input.assetEnv.entry)}`);
|
|
176
|
-
}
|
|
177
|
-
if (input.assetEnv.styles) {
|
|
178
|
-
lines.push(`process.env.GUREN_INERTIA_STYLES = ${JSON.stringify(input.assetEnv.styles)}`);
|
|
179
|
-
}
|
|
180
|
-
if (input.assetEnv.entry || input.assetEnv.styles) {
|
|
181
|
-
lines.push("");
|
|
182
|
-
}
|
|
183
|
-
if (input.ssrImport) {
|
|
184
|
-
lines.push(`setInertiaSsrRenderer(ssrModule.${input.ssrImport.rendererExport})`, "");
|
|
185
|
-
}
|
|
186
|
-
lines.push("export default createWorkersHandler(app)", "");
|
|
187
|
-
return lines.join("\n");
|
|
188
|
-
}
|
|
189
|
-
function scaffoldWranglerConfig(root, out, packageName) {
|
|
190
|
-
const configPath = resolve(root, "wrangler.jsonc");
|
|
191
|
-
const appName = (packageName ?? "guren-app").replace(/^@[^/]+\//, "");
|
|
192
|
-
const outRelative = relative(root, out).split(sep).join("/");
|
|
193
|
-
const config = {
|
|
194
|
-
name: appName,
|
|
195
|
-
main: `${outRelative}/worker.js`,
|
|
196
|
-
compatibility_date: (/* @__PURE__ */ new Date()).toISOString().slice(0, 10),
|
|
197
|
-
compatibility_flags: ["nodejs_compat"],
|
|
198
|
-
alias: devOnlyAliases(outRelative),
|
|
199
|
-
define: {
|
|
200
|
-
// Statements in the generated worker cannot beat ESM import hoisting,
|
|
201
|
-
// and wrangler `vars` are not guaranteed to reach `process.env` before
|
|
202
|
-
// the app's module graph evaluates — framework and app code branch on
|
|
203
|
-
// NODE_ENV at module scope, so it is substituted at build time (the
|
|
204
|
-
// same approach the Vercel plugin takes).
|
|
205
|
-
"process.env.NODE_ENV": '"production"',
|
|
206
|
-
// workerd leaves `import.meta.url` undefined. Two things break on it:
|
|
207
|
-
// Vite's SSR bundle initializes `createRequire(import.meta.url)`, and
|
|
208
|
-
// scaffolded config resolves paths with `new URL(..., import.meta.url)`
|
|
209
|
-
// — both at module scope, so the worker dies before serving anything.
|
|
210
|
-
// Substituting a literal is safe precisely because Workers has no
|
|
211
|
-
// filesystem: every such path is already meaningless there.
|
|
212
|
-
"import.meta.url": '"file:///worker.js"'
|
|
213
|
-
},
|
|
214
|
-
assets: { directory: `${outRelative}/assets` },
|
|
215
|
-
d1_databases: [
|
|
216
|
-
{
|
|
217
|
-
binding: "DB",
|
|
218
|
-
database_name: appName,
|
|
219
|
-
database_id: "TODO: wrangler d1 create",
|
|
220
|
-
migrations_dir: `${outRelative}/d1-migrations`
|
|
221
|
-
}
|
|
222
|
-
],
|
|
223
|
-
vars: { NODE_ENV: "production" }
|
|
224
|
-
};
|
|
225
|
-
try {
|
|
226
|
-
writeFileSync(configPath, `${JSON.stringify(config, null, 2)}
|
|
227
|
-
`, { flag: "wx" });
|
|
228
|
-
} catch (error) {
|
|
229
|
-
if (error.code === "EEXIST") {
|
|
230
|
-
warnMissingBuildOwnedKeys(configPath, outRelative);
|
|
231
|
-
return;
|
|
232
|
-
}
|
|
233
|
-
throw error;
|
|
234
|
-
}
|
|
235
|
-
console.log(`Cloudflare build: scaffolded ${configPath} \u2014 fill in d1_databases[0].database_id before deploying.`);
|
|
236
|
-
}
|
|
237
|
-
function parseJsonc(text) {
|
|
238
|
-
const out = [];
|
|
239
|
-
let index = 0;
|
|
240
|
-
while (index < text.length) {
|
|
241
|
-
const char = text[index];
|
|
242
|
-
if (char === '"') {
|
|
243
|
-
const start = index;
|
|
244
|
-
index += 1;
|
|
245
|
-
while (index < text.length) {
|
|
246
|
-
if (text[index] === "\\") {
|
|
247
|
-
index += 2;
|
|
248
|
-
continue;
|
|
249
|
-
}
|
|
250
|
-
if (text[index] === '"') {
|
|
251
|
-
index += 1;
|
|
252
|
-
break;
|
|
253
|
-
}
|
|
254
|
-
index += 1;
|
|
255
|
-
}
|
|
256
|
-
out.push(text.slice(start, index));
|
|
257
|
-
continue;
|
|
258
|
-
}
|
|
259
|
-
if (char === "/" && text[index + 1] === "/") {
|
|
260
|
-
while (index < text.length && text[index] !== "\n") {
|
|
261
|
-
index += 1;
|
|
262
|
-
}
|
|
263
|
-
continue;
|
|
264
|
-
}
|
|
265
|
-
if (char === "/" && text[index + 1] === "*") {
|
|
266
|
-
const end = text.indexOf("*/", index + 2);
|
|
267
|
-
index = end === -1 ? text.length : end + 2;
|
|
268
|
-
continue;
|
|
269
|
-
}
|
|
270
|
-
if (char === "}" || char === "]") {
|
|
271
|
-
let back = out.length - 1;
|
|
272
|
-
while (back >= 0 && /^\s+$/.test(out[back])) {
|
|
273
|
-
back -= 1;
|
|
274
|
-
}
|
|
275
|
-
if (back >= 0 && out[back] === ",") {
|
|
276
|
-
out.splice(back, 1);
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
out.push(char);
|
|
280
|
-
index += 1;
|
|
281
|
-
}
|
|
282
|
-
return JSON.parse(out.join(""));
|
|
283
|
-
}
|
|
284
|
-
function warnMissingBuildOwnedKeys(configPath, outRelative) {
|
|
285
|
-
let config;
|
|
286
|
-
try {
|
|
287
|
-
config = parseJsonc(readFileSync(configPath, "utf8"));
|
|
288
|
-
} catch {
|
|
289
|
-
console.warn(
|
|
290
|
-
`Cloudflare build: could not parse ${configPath}, so its build-owned keys went unchecked. Fix the file, or compare it against a config scaffolded in an empty directory.`
|
|
291
|
-
);
|
|
292
|
-
return;
|
|
293
|
-
}
|
|
294
|
-
const missing = [];
|
|
295
|
-
const configAlias = config.alias;
|
|
296
|
-
const alias = typeof configAlias === "object" && configAlias !== null ? configAlias : {};
|
|
297
|
-
for (const [specifier, target] of Object.entries(devOnlyAliases(outRelative))) {
|
|
298
|
-
if (!(specifier in alias)) {
|
|
299
|
-
missing.push(`${JSON.stringify(specifier)}: ${JSON.stringify(target)} (inside "alias")`);
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
const define = config.define;
|
|
303
|
-
if (!define?.["process.env.NODE_ENV"]) {
|
|
304
|
-
missing.push('"process.env.NODE_ENV": "\\"production\\"" (inside "define")');
|
|
305
|
-
}
|
|
306
|
-
const d1 = config.d1_databases?.[0];
|
|
307
|
-
if (d1 && d1.migrations_dir !== `${outRelative}/d1-migrations`) {
|
|
308
|
-
missing.push(`"migrations_dir": "${outRelative}/d1-migrations" (inside d1_databases[0])`);
|
|
309
|
-
}
|
|
310
|
-
if (missing.length > 0) {
|
|
311
|
-
console.warn(
|
|
312
|
-
`Cloudflare build: ${configPath} predates this plugin version. Add these entries, alongside whatever the file already has under the same keys, or the worker will fail to start or skip migrations:
|
|
313
|
-
${missing.join("\n ")}`
|
|
314
|
-
);
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
export {
|
|
319
|
-
buildCloudflareOutput,
|
|
320
|
-
flattenD1Migrations
|
|
321
|
-
};
|