one
Version:
One is a React framework for web, iOS, and Android apps.
140 lines (139 loc) • 5.33 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var warmRoutesPlugin_exports = {};
__export(warmRoutesPlugin_exports, {
autoWarmPlugin: () => autoWarmPlugin,
warmRoutesPlugin: () => warmRoutesPlugin
});
module.exports = __toCommonJS(warmRoutesPlugin_exports);
var import_node_fs = require("node:fs");
var import_node_path = require("node:path");
const WARM_DEPS_FILE = "one-warm-deps.json";
const TRACKING_WINDOW = 5 * 60 * 1e3;
function autoWarmPlugin(persistPath) {
let cacheFile;
let cachedDeps = [];
let excludeSet;
return {
name: "one:auto-warm",
apply: "serve",
config() {
const volatileCache = (0, import_node_path.join)(process.cwd(), "node_modules", ".vite", WARM_DEPS_FILE);
cacheFile = typeof persistPath === "string" ? (0, import_node_path.join)(process.cwd(), persistPath) : volatileCache;
try {
if ((0, import_node_fs.existsSync)(cacheFile)) {
const cached = JSON.parse((0, import_node_fs.readFileSync)(cacheFile, "utf-8"));
if (Array.isArray(cached.deps) && cached.deps.length > 0) {
cachedDeps = cached.deps;
console.info(`[one] loading ${cached.deps.length} cached warm deps`);
return { optimizeDeps: { include: cached.deps } };
}
}
} catch {}
},
async configResolved(config) {
excludeSet = new Set(config.optimizeDeps.exclude || []);
if (cachedDeps.length > 0) {
const resolve = config.createResolver();
const staleDeps = /* @__PURE__ */ new Set();
await Promise.all(cachedDeps.map(async (dep) => {
if (excludeSet.has(dep)) {
staleDeps.add(dep);
return;
}
try {
if (!await resolve(dep)) staleDeps.add(dep);
} catch {
staleDeps.add(dep);
}
}));
if (staleDeps.size > 0) {
cachedDeps = cachedDeps.filter((dep) => !staleDeps.has(dep));
console.info(`[one] pruned ${staleDeps.size} stale deps from warm cache`);
if (config.optimizeDeps.include) {
config.optimizeDeps.include = config.optimizeDeps.include.filter((dep) => !staleDeps.has(dep));
}
try {
const nextRaw = JSON.stringify({ deps: [...cachedDeps].sort() }, null, 2);
if (!(0, import_node_fs.existsSync)(cacheFile) || (0, import_node_fs.readFileSync)(cacheFile, "utf-8") !== nextRaw) {
const dir = (0, import_node_path.dirname)(cacheFile);
if (!(0, import_node_fs.existsSync)(dir)) (0, import_node_fs.mkdirSync)(dir, { recursive: true });
(0, import_node_fs.writeFileSync)(cacheFile, nextRaw);
}
} catch {}
}
}
},
configureServer(server) {
let lastDepsCount = 0;
let timer;
const userInclude = new Set((server.config.optimizeDeps.include || []).filter((d) => !cachedDeps.includes(d)));
function snapshotDeps() {
try {
const optimizer = server.environments?.client?.depsOptimizer ?? server._depsOptimizer;
if (!optimizer?.metadata) return;
const optimized = optimizer.metadata.optimized;
const discovered = optimizer.metadata.discovered;
const currentDeps = [...Object.keys(optimized || {}), ...Object.keys(discovered || {})];
if (currentDeps.length === 0 || currentDeps.length === lastDepsCount) return;
lastDepsCount = currentDeps.length;
const depsToCache = currentDeps.filter((d) => !userInclude.has(d) && !excludeSet.has(d));
const allDeps = new Set(depsToCache);
let existingRaw;
if ((0, import_node_fs.existsSync)(cacheFile)) {
try {
existingRaw = (0, import_node_fs.readFileSync)(cacheFile, "utf-8");
const existing = JSON.parse(existingRaw);
if (Array.isArray(existing.deps)) {
for (const d of existing.deps) {
if (!excludeSet.has(d)) allDeps.add(d);
}
}
} catch {
return;
}
}
const sorted = [...allDeps].sort();
const nextRaw = JSON.stringify({ deps: sorted }, null, 2);
if (existingRaw === nextRaw) return;
const dir = (0, import_node_path.dirname)(cacheFile);
if (!(0, import_node_fs.existsSync)(dir)) (0, import_node_fs.mkdirSync)(dir, { recursive: true });
(0, import_node_fs.writeFileSync)(cacheFile, nextRaw);
console.info(`[one] cached ${sorted.length} deps for next startup`);
} catch {}
}
server.httpServer?.once("listening", () => {
timer = setInterval(snapshotDeps, 5e3);
setTimeout(() => {
clearInterval(timer);
snapshotDeps();
}, TRACKING_WINDOW);
});
const origClose = server.close.bind(server);
server.close = async () => {
clearInterval(timer);
snapshotDeps();
return origClose();
};
}
};
}
const warmRoutesPlugin = autoWarmPlugin;