easysql/node_modules/vite-plugin-electron/dist/index.js
2025-12-29 18:35:04 +08:00

223 lines
7.9 KiB
JavaScript

"use strict";
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 __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
));
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const vite = require("vite");
const node_module = require("node:module");
function resolveViteConfig(options) {
const defaultConfig = {
// 🚧 Avoid recursive build caused by load config file
configFile: false,
publicDir: false,
build: {
// @ts-ignore
lib: options.entry && {
entry: options.entry,
// At present, Electron(20) can only support CommonJs
formats: ["cjs"],
fileName: () => "[name].js"
},
outDir: "dist-electron",
// Avoid multiple entries affecting each other
emptyOutDir: false
},
resolve: {
// #136
// Some libs like `axios` must disable the `browserField`.
// @axios https://github.com/axios/axios/blob/v1.3.5/package.json#L129
// @vite https://github.com/vitejs/vite/blob/v4.2.1/packages/vite/src/node/plugins/resolve.ts#L294
browserField: false,
// #98
// Since we're building for electron (which uses Node.js), we don't want to use the "browser" field in the packages.
// It corrupts bundling packages like `ws` and `isomorphic-ws`, for example.
mainFields: ["module", "jsnext:main", "jsnext"]
}
};
return vite.mergeConfig(defaultConfig, (options == null ? void 0 : options.vite) || {});
}
function withExternalBuiltins(config) {
var _a;
const builtins = node_module.builtinModules.filter((e) => !e.startsWith("_"));
builtins.push("electron", ...builtins.map((m) => `node:${m}`));
config.build ?? (config.build = {});
(_a = config.build).rollupOptions ?? (_a.rollupOptions = {});
let external = config.build.rollupOptions.external;
if (Array.isArray(external) || typeof external === "string" || external instanceof RegExp) {
external = builtins.concat(external);
} else if (typeof external === "function") {
const original = external;
external = function(source, importer, isResolved) {
if (builtins.includes(source)) {
return true;
}
return original(source, importer, isResolved);
};
} else {
external = builtins;
}
config.build.rollupOptions.external = external;
return config;
}
function resolveHostname(hostname) {
const loopbackHosts = /* @__PURE__ */ new Set([
"localhost",
"127.0.0.1",
"::1",
"0000:0000:0000:0000:0000:0000:0000:0001"
]);
const wildcardHosts = /* @__PURE__ */ new Set([
"0.0.0.0",
"::",
"0000:0000:0000:0000:0000:0000:0000:0000"
]);
return loopbackHosts.has(hostname) || wildcardHosts.has(hostname) ? "localhost" : hostname;
}
function resolveServerUrl(server) {
const addressInfo = server.httpServer.address();
const isAddressInfo = (x) => x == null ? void 0 : x.address;
if (isAddressInfo(addressInfo)) {
const { address, port } = addressInfo;
const hostname = resolveHostname(address);
const options = server.config.server;
const protocol = options.https ? "https" : "http";
const devBase = server.config.base;
const path = typeof options.open === "string" ? options.open : devBase;
const url = path.startsWith("http") ? path : `${protocol}://${hostname}:${port}${path}`;
return url;
}
}
function calcEntryCount(optionsArray) {
return optionsArray.length;
}
function build(options) {
return vite.build(withExternalBuiltins(resolveViteConfig(options)));
}
function electron(options) {
const optionsArray = Array.isArray(options) ? options : [options];
let mode;
return [
{
name: "vite-plugin-electron",
apply: "serve",
configureServer(server) {
var _a;
(_a = server.httpServer) == null ? void 0 : _a.once("listening", () => {
var _a2, _b, _c, _d, _e;
Object.assign(process.env, {
VITE_DEV_SERVER_URL: resolveServerUrl(server)
});
const entryCount = calcEntryCount(optionsArray);
let closeBundleCount = 0;
for (const options2 of optionsArray) {
options2.vite ?? (options2.vite = {});
(_a2 = options2.vite).mode ?? (_a2.mode = server.config.mode);
(_b = options2.vite).build ?? (_b.build = {});
(_c = options2.vite.build).watch ?? (_c.watch = {});
(_d = options2.vite.build).minify ?? (_d.minify = false);
(_e = options2.vite).plugins ?? (_e.plugins = []);
options2.vite.plugins.push(
{
name: ":startup",
closeBundle() {
if (++closeBundleCount < entryCount)
return;
if (options2.onstart) {
options2.onstart.call(this, {
startup,
reload: process.electronApp ? () => server.ws.send({ type: "full-reload" }) : startup
});
} else {
startup();
}
}
}
);
build(options2);
}
});
}
},
{
name: "vite-plugin-electron",
apply: "build",
config(config, env) {
config.base ?? (config.base = "./");
mode = env.mode;
},
async closeBundle() {
var _a;
for (const options2 of optionsArray) {
options2.vite ?? (options2.vite = {});
(_a = options2.vite).mode ?? (_a.mode = mode);
await build(options2);
}
}
}
];
}
async function startup(argv = [".", "--no-sandbox"]) {
const { spawn } = await import("node:child_process");
const electron2 = await import("electron");
const electronPath = electron2.default ?? electron2;
await startup.exit();
process.electronApp = spawn(electronPath, argv, { stdio: "inherit" });
process.electronApp.once("exit", process.exit);
if (!startup.hookedProcessExit) {
startup.hookedProcessExit = true;
process.once("exit", async () => {
await startup.exit();
process.electronApp.kill();
});
}
}
startup.hookedProcessExit = false;
startup.exit = () => {
if (!process.electronApp) {
return Promise.resolve(null);
}
process.electronApp.removeAllListeners();
return new Promise(async (resolve) => {
await import("tree-kill").then((m) => m.default(
process.electronApp.pid,
"SIGKILL",
resolve
)).catch((e) => {
if (e.code === "ERR_MODULE_NOT_FOUND") {
console.log(
"[vite-plugin-electron]",
'Please install tree-kill to exit all associated processes, run "npm i tree-kill -D".'
);
} else {
console.error(e);
}
process.electronApp.kill();
resolve(e);
});
});
};
exports.build = build;
exports.default = electron;
exports.resolveViteConfig = resolveViteConfig;
exports.startup = startup;
exports.withExternalBuiltins = withExternalBuiltins;