one
Version:
One is a new React Framework that makes Vite serve both native and web.
247 lines • 8.4 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all) __defProp(target, name, {
get: all[name],
enumerable: true
});
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: () => from[key],
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
value: mod,
enumerable: true
}) : target, mod));
var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
value: true
}), mod);
var cloudflareWranglerConfig_exports = {};
__export(cloudflareWranglerConfig_exports, {
GENERATED_CLOUDFLARE_WRANGLER_RULES: () => GENERATED_CLOUDFLARE_WRANGLER_RULES,
createCloudflareWranglerConfig: () => createCloudflareWranglerConfig,
getCloudflareProjectName: () => getCloudflareProjectName,
getCloudflareProjectNameSync: () => getCloudflareProjectNameSync,
hasUserWranglerConfig: () => hasUserWranglerConfig,
isExperimentalWorkerDevEnabled: () => isExperimentalWorkerDevEnabled,
isPlainObject: () => isPlainObject,
loadUserWranglerConfig: () => loadUserWranglerConfig,
loadUserWranglerConfigSync: () => loadUserWranglerConfigSync,
shouldEnableWorkerdDev: () => shouldEnableWorkerdDev
});
module.exports = __toCommonJS(cloudflareWranglerConfig_exports);
var import_node_fs = require("node:fs");
var import_node_path = require("node:path");
var import_fs_extra = __toESM(require("fs-extra"), 1);
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 ((0, import_node_fs.existsSync)((0, import_node_path.join)(candidateRoot, fileName))) return true;
}
}
return false;
}
function parseUserWranglerFile(configPath) {
const contents = (0, import_node_fs.readFileSync)(configPath, "utf-8");
let parsed;
try {
parsed = parseJsonc(contents);
} catch (err) {
throw new Error(`Failed to parse ${(0, import_node_path.relative)(process.cwd(), configPath)}: ${err.message}`);
}
if (!isPlainObject(parsed)) {
throw new Error(`Expected ${(0, import_node_path.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 = (0, import_node_path.join)(candidateRoot, fileName);
if (!(0, import_node_fs.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 = (0, import_node_path.join)(candidateRoot, fileName);
if (!(await import_fs_extra.default.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((0, import_node_fs.readFileSync)((0, import_node_path.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 import_fs_extra.default.readFile((0, import_node_path.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);
}