one
Version:
One is a new React Framework that makes Vite serve both native and web.
117 lines (116 loc) • 3.19 kB
JavaScript
import * as path from "node:path";
import colors from "picocolors";
import { setServerGlobals } from "../server/setServerGlobals.mjs";
import { virtualEntryIdNative } from "../vite/plugins/virtualEntryConstants.mjs";
import { checkNodeVersion } from "./checkNodeVersion.mjs";
import { labelProcess } from "./label-process.mjs";
const DAEMON_PORT = 8081;
async function dev(args) {
labelProcess("dev");
checkNodeVersion();
setServerGlobals();
const root = process.cwd();
let daemonServerId;
let useDaemon = false;
const forcePort = process.env.ONE_FORCE_PORT ? +process.env.ONE_FORCE_PORT : void 0;
const envPort = process.env.ONE_PORT ? +process.env.ONE_PORT : void 0;
let effectivePort = forcePort ?? (args.port ? +args.port : void 0) ?? envPort;
const {
isDaemonRunning,
registerWithDaemon,
unregisterFromDaemon,
writeServerFile,
removeServerFile
} = await import("../daemon/ipc.mjs");
const {
getBundleIdFromConfig,
getAvailablePort
} = await import("../daemon/utils.mjs");
const daemonRunning = await isDaemonRunning();
const bundleId = getBundleIdFromConfig(root) || path.basename(root);
if (daemonRunning) {
if (!effectivePort || effectivePort === DAEMON_PORT) {
effectivePort = await getAvailablePort(8082, DAEMON_PORT);
console.log(colors.cyan(`[daemon] Detected running daemon on :${DAEMON_PORT}`));
console.log(colors.cyan(`[daemon] Using port :${effectivePort} for this server`));
useDaemon = true;
}
}
const {
dev: dev2
} = await import("vxrn/dev");
const {
start,
stop
} = await dev2({
mode: args.mode,
clean: args.clean,
root,
debugBundle: args.debugBundle,
debug: args.debug,
extraConfig: args.extraConfig,
server: {
host: args.host,
port: effectivePort
},
entries: {
native: virtualEntryIdNative
}
});
const {
closePromise
} = await start();
if (useDaemon && bundleId) {
try {
daemonServerId = await registerWithDaemon({
port: effectivePort,
bundleId,
root
});
console.log(colors.green(`[daemon] Registered as ${bundleId} (${daemonServerId}) \u2192 accessible via :${DAEMON_PORT}`));
writeServerFile({
port: effectivePort,
bundleId,
root,
pid: process.pid
});
} catch (err) {
console.log(colors.yellow(`[daemon] Failed to register: ${err}`));
}
}
const cleanup = async () => {
if (daemonServerId) {
try {
await unregisterFromDaemon(daemonServerId);
removeServerFile(root);
} catch {}
}
await stop();
};
process.on("beforeExit", () => {
cleanup();
});
process.on("SIGINT", async () => {
try {
await cleanup();
} finally {
process.exit(2);
}
});
process.on("SIGTERM", async () => {
try {
await cleanup();
} finally {
process.exit(0);
}
});
process.on("uncaughtException", err => {
console.error(err?.message || err);
});
process.on("unhandledRejection", err => {
console.error(err);
});
await closePromise;
}
export { dev };
//# sourceMappingURL=dev.mjs.map