UNPKG

one

Version:

One is a new React Framework that makes Vite serve both native and web.

203 lines (202 loc) 6.3 kB
import { existsSync, readFileSync } from "node:fs"; import { join, relative } from "node:path"; import FSExtra from "fs-extra"; const GENERATED_CLOUDFLARE_WRANGLER_RULES = [{ type: "ESModule", globs: ["./server/**/*.js"], fallthrough: true }, { type: "ESModule", globs: ["./api/**/*.js"], fallthrough: true }, { type: "ESModule", globs: ["./middlewares/**/*.js"], fallthrough: true }, { type: "ESModule", globs: ["./assets/**/*.js"], fallthrough: true }]; function isPlainObject(value) { return !!value && typeof value === "object" && !Array.isArray(value); } function mergeJsonObjects(base, overrides) { const merged = { ...base }; for (const [key, value] of Object.entries(overrides)) { const baseValue = merged[key]; if (isPlainObject(baseValue) && isPlainObject(value)) { merged[key] = mergeJsonObjects(baseValue, value); } else { merged[key] = value; } } return merged; } function dedupeJsonValues(values) { const seen = /* @__PURE__ */new Set(); return values.filter(value => { const key = JSON.stringify(value); if (seen.has(key)) return false; seen.add(key); return true; }); } function mergeCloudflareCompatibilityFlags(flags) { const userFlags = Array.isArray(flags) ? flags.filter(flag => typeof flag === "string") : []; return dedupeJsonValues(["nodejs_compat", ...userFlags]); } function mergeCloudflareRules(rules) { const userRules = Array.isArray(rules) ? rules.filter(rule => isPlainObject(rule)) : []; return dedupeJsonValues([...GENERATED_CLOUDFLARE_WRANGLER_RULES, ...userRules]); } function parseJsonc(text) { let out = ""; let i = 0; let inString = false; let quote = ""; while (i < text.length) { const ch = text[i]; const next = text[i + 1]; if (inString) { if (ch === "\\") { out += ch + (next ?? ""); i += 2; continue; } if (ch === quote) inString = false; out += ch; i++; continue; } if (ch === '"' || ch === "'") { inString = true; quote = ch; out += ch; i++; continue; } if (ch === "/" && next === "/") { while (i < text.length && text[i] !== "\n") i++; continue; } if (ch === "/" && next === "*") { i += 2; while (i < text.length - 1 && !(text[i] === "*" && text[i + 1] === "/")) i++; i += 2; continue; } out += ch; i++; } return JSON.parse(out.replace(/,(\s*[}\]])/g, "$1")); } const WRANGLER_FILE_NAMES = ["wrangler.jsonc", "wrangler.json"]; function wranglerCandidateRoots(root) { return [... /* @__PURE__ */new Set([root, process.cwd()])]; } function hasUserWranglerConfig(root) { for (const candidateRoot of wranglerCandidateRoots(root)) { for (const fileName of WRANGLER_FILE_NAMES) { if (existsSync(join(candidateRoot, fileName))) return true; } } return false; } function parseUserWranglerFile(configPath) { const contents = readFileSync(configPath, "utf-8"); let parsed; try { parsed = parseJsonc(contents); } catch (err) { throw new Error(`Failed to parse ${relative(process.cwd(), configPath)}: ${err.message}`); } if (!isPlainObject(parsed)) { throw new Error(`Expected ${relative(process.cwd(), configPath)} to contain a top-level JSON object`); } return { path: configPath, config: parsed }; } function loadUserWranglerConfigSync(root) { for (const candidateRoot of wranglerCandidateRoots(root)) { for (const fileName of WRANGLER_FILE_NAMES) { const configPath = join(candidateRoot, fileName); if (!existsSync(configPath)) continue; return parseUserWranglerFile(configPath); } } return null; } async function loadUserWranglerConfig(root) { const candidateRoots = wranglerCandidateRoots(root); for (const candidateRoot of candidateRoots) { for (const fileName of WRANGLER_FILE_NAMES) { const configPath = join(candidateRoot, fileName); if (!(await FSExtra.pathExists(configPath))) { continue; } return parseUserWranglerFile(configPath); } } return null; } function createCloudflareWranglerConfig(projectName, userConfig) { const generatedConfig = { name: projectName, main: "worker.js", compatibility_date: "2024-12-05", compatibility_flags: ["nodejs_compat"], find_additional_modules: true, rules: GENERATED_CLOUDFLARE_WRANGLER_RULES, assets: { directory: "client", binding: "ASSETS", run_worker_first: true } }; const mergedConfig = userConfig ? mergeJsonObjects(generatedConfig, userConfig) : generatedConfig; mergedConfig.main = "worker.js"; mergedConfig.find_additional_modules = true; mergedConfig.compatibility_flags = mergeCloudflareCompatibilityFlags(mergedConfig.compatibility_flags); mergedConfig.rules = mergeCloudflareRules(mergedConfig.rules); mergedConfig.assets = { ...(isPlainObject(mergedConfig.assets) ? mergedConfig.assets : {}), directory: "client", binding: "ASSETS", run_worker_first: true }; return mergedConfig; } function getCloudflareProjectNameSync(root) { try { const pkg = JSON.parse(readFileSync(join(root, "package.json"), "utf-8")); if (pkg.name) { return String(pkg.name).replace(/^@[^/]+\//, ""); } } catch {} return "one-app"; } async function getCloudflareProjectName(root) { try { const pkg = JSON.parse(await FSExtra.readFile(join(root, "package.json"), "utf-8")); if (pkg.name) { return pkg.name.replace(/^@[^/]+\//, ""); } } catch {} return "one-app"; } function isExperimentalWorkerDevEnabled() { return process.env.ONE_EXPERIMENTAL_WORKER_DEV === "1"; } function shouldEnableWorkerdDev(deploy, root) { if (!isExperimentalWorkerDevEnabled()) return false; const target = typeof deploy === "string" ? deploy : deploy?.target; if (target === "cloudflare") return true; return hasUserWranglerConfig(root); } export { GENERATED_CLOUDFLARE_WRANGLER_RULES, createCloudflareWranglerConfig, getCloudflareProjectName, getCloudflareProjectNameSync, hasUserWranglerConfig, isExperimentalWorkerDevEnabled, isPlainObject, loadUserWranglerConfig, loadUserWranglerConfigSync, shouldEnableWorkerdDev }; //# sourceMappingURL=cloudflareWranglerConfig.mjs.map