one
Version:
One is a new React Framework that makes Vite serve both native and web.
98 lines (97 loc) • 3.75 kB
JavaScript
import { existsSync, readFileSync, writeFileSync, mkdirSync } from "node:fs";
import { join, dirname } from "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 = join(process.cwd(), "node_modules", ".vite", WARM_DEPS_FILE);
cacheFile = typeof persistPath === "string" ? join(process.cwd(), persistPath) : volatileCache;
try {
if (existsSync(cacheFile)) {
const cached = JSON.parse(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 {}
},
configResolved(config) {
excludeSet = new Set(config.optimizeDeps.exclude || []);
if (cachedDeps.length > 0 && excludeSet.size > 0) {
const conflicts = cachedDeps.filter(d => excludeSet.has(d));
if (conflicts.length > 0) {
console.info(`[one] filtered ${conflicts.length} excluded deps from warm cache`);
if (config.optimizeDeps.include) {
;
config.optimizeDeps.include = config.optimizeDeps.include.filter(d => !excludeSet.has(d));
}
}
}
},
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);
try {
if (existsSync(cacheFile)) {
const existing = JSON.parse(readFileSync(cacheFile, "utf-8"));
if (Array.isArray(existing.deps)) {
for (const d of existing.deps) {
if (!excludeSet.has(d)) allDeps.add(d);
}
}
}
} catch {}
const sorted = [...allDeps].sort();
const dir = dirname(cacheFile);
if (!existsSync(dir)) mkdirSync(dir, {
recursive: true
});
writeFileSync(cacheFile, JSON.stringify({
deps: sorted
}, null, 2));
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;
export { autoWarmPlugin, warmRoutesPlugin };
//# sourceMappingURL=warmRoutesPlugin.mjs.map