one
Version:
One is a new React Framework that makes Vite serve both native and web.
123 lines • 4.84 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 {}
},
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 ((0, import_node_fs.existsSync)(cacheFile)) {
const existing = JSON.parse((0, import_node_fs.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 = (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, 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;