index.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. "use strict";
  2. var __create = Object.create;
  3. var __defProp = Object.defineProperty;
  4. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  5. var __getOwnPropNames = Object.getOwnPropertyNames;
  6. var __getProtoOf = Object.getPrototypeOf;
  7. var __hasOwnProp = Object.prototype.hasOwnProperty;
  8. var __copyProps = (to, from, except, desc) => {
  9. if (from && typeof from === "object" || typeof from === "function") {
  10. for (let key of __getOwnPropNames(from))
  11. if (!__hasOwnProp.call(to, key) && key !== except)
  12. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  13. }
  14. return to;
  15. };
  16. var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  17. // If the importer is in node compatibility mode or this is not an ESM
  18. // file that has been converted to a CommonJS file using a Babel-
  19. // compatible transform (i.e. "__esModule" has not been set), then set
  20. // "default" to the CommonJS "module.exports" for node compatibility.
  21. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  22. mod
  23. ));
  24. Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
  25. const vite = require("vite");
  26. const node_module = require("node:module");
  27. function resolveViteConfig(options) {
  28. const defaultConfig = {
  29. // 🚧 Avoid recursive build caused by load config file
  30. configFile: false,
  31. publicDir: false,
  32. build: {
  33. // @ts-ignore
  34. lib: options.entry && {
  35. entry: options.entry,
  36. // At present, Electron(20) can only support CommonJs
  37. formats: ["cjs"],
  38. fileName: () => "[name].js"
  39. },
  40. outDir: "dist-electron",
  41. // Avoid multiple entries affecting each other
  42. emptyOutDir: false
  43. },
  44. resolve: {
  45. // #136
  46. // Some libs like `axios` must disable the `browserField`.
  47. // @axios https://github.com/axios/axios/blob/v1.3.5/package.json#L129
  48. // @vite https://github.com/vitejs/vite/blob/v4.2.1/packages/vite/src/node/plugins/resolve.ts#L294
  49. browserField: false,
  50. // #98
  51. // Since we're building for electron (which uses Node.js), we don't want to use the "browser" field in the packages.
  52. // It corrupts bundling packages like `ws` and `isomorphic-ws`, for example.
  53. mainFields: ["module", "jsnext:main", "jsnext"]
  54. }
  55. };
  56. return vite.mergeConfig(defaultConfig, (options == null ? void 0 : options.vite) || {});
  57. }
  58. function withExternalBuiltins(config) {
  59. var _a;
  60. const builtins = node_module.builtinModules.filter((e) => !e.startsWith("_"));
  61. builtins.push("electron", ...builtins.map((m) => `node:${m}`));
  62. config.build ?? (config.build = {});
  63. (_a = config.build).rollupOptions ?? (_a.rollupOptions = {});
  64. let external = config.build.rollupOptions.external;
  65. if (Array.isArray(external) || typeof external === "string" || external instanceof RegExp) {
  66. external = builtins.concat(external);
  67. } else if (typeof external === "function") {
  68. const original = external;
  69. external = function(source, importer, isResolved) {
  70. if (builtins.includes(source)) {
  71. return true;
  72. }
  73. return original(source, importer, isResolved);
  74. };
  75. } else {
  76. external = builtins;
  77. }
  78. config.build.rollupOptions.external = external;
  79. return config;
  80. }
  81. function resolveHostname(hostname) {
  82. const loopbackHosts = /* @__PURE__ */ new Set([
  83. "localhost",
  84. "127.0.0.1",
  85. "::1",
  86. "0000:0000:0000:0000:0000:0000:0000:0001"
  87. ]);
  88. const wildcardHosts = /* @__PURE__ */ new Set([
  89. "0.0.0.0",
  90. "::",
  91. "0000:0000:0000:0000:0000:0000:0000:0000"
  92. ]);
  93. return loopbackHosts.has(hostname) || wildcardHosts.has(hostname) ? "localhost" : hostname;
  94. }
  95. function resolveServerUrl(server) {
  96. const addressInfo = server.httpServer.address();
  97. const isAddressInfo = (x) => x == null ? void 0 : x.address;
  98. if (isAddressInfo(addressInfo)) {
  99. const { address, port } = addressInfo;
  100. const hostname = resolveHostname(address);
  101. const options = server.config.server;
  102. const protocol = options.https ? "https" : "http";
  103. const devBase = server.config.base;
  104. const path = typeof options.open === "string" ? options.open : devBase;
  105. const url = path.startsWith("http") ? path : `${protocol}://${hostname}:${port}${path}`;
  106. return url;
  107. }
  108. }
  109. function build(options) {
  110. return vite.build(withExternalBuiltins(resolveViteConfig(options)));
  111. }
  112. function electron(options) {
  113. const optionsArray = Array.isArray(options) ? options : [options];
  114. let mode;
  115. return [
  116. {
  117. name: "vite-plugin-electron",
  118. apply: "serve",
  119. configureServer(server) {
  120. var _a;
  121. (_a = server.httpServer) == null ? void 0 : _a.once("listening", () => {
  122. var _a2, _b, _c, _d, _e;
  123. Object.assign(process.env, {
  124. VITE_DEV_SERVER_URL: resolveServerUrl(server)
  125. });
  126. for (const options2 of optionsArray) {
  127. options2.vite ?? (options2.vite = {});
  128. (_a2 = options2.vite).mode ?? (_a2.mode = server.config.mode);
  129. (_b = options2.vite).build ?? (_b.build = {});
  130. (_c = options2.vite.build).watch ?? (_c.watch = {});
  131. (_d = options2.vite.build).minify ?? (_d.minify = false);
  132. (_e = options2.vite).plugins ?? (_e.plugins = []);
  133. options2.vite.plugins.push({
  134. name: ":startup",
  135. closeBundle() {
  136. if (options2.onstart) {
  137. options2.onstart.call(this, {
  138. startup,
  139. reload() {
  140. server.ws.send({ type: "full-reload" });
  141. }
  142. });
  143. } else {
  144. startup();
  145. }
  146. }
  147. });
  148. build(options2);
  149. }
  150. });
  151. }
  152. },
  153. {
  154. name: "vite-plugin-electron",
  155. apply: "build",
  156. config(config, env) {
  157. config.base ?? (config.base = "./");
  158. mode = env.mode;
  159. },
  160. async closeBundle() {
  161. var _a;
  162. for (const options2 of optionsArray) {
  163. options2.vite ?? (options2.vite = {});
  164. (_a = options2.vite).mode ?? (_a.mode = mode);
  165. await build(options2);
  166. }
  167. }
  168. }
  169. ];
  170. }
  171. async function startup(argv = [".", "--no-sandbox"]) {
  172. const { spawn } = await import("node:child_process");
  173. const electron2 = await import("electron");
  174. const electronPath = electron2.default ?? electron2;
  175. startup.exit();
  176. process.electronApp = spawn(electronPath, argv, { stdio: "inherit" });
  177. process.electronApp.once("exit", process.exit);
  178. if (!startup.hookProcessExit) {
  179. startup.hookProcessExit = true;
  180. process.once("exit", startup.exit);
  181. }
  182. }
  183. startup.hookProcessExit = false;
  184. startup.exit = () => {
  185. if (process.electronApp) {
  186. process.electronApp.removeAllListeners();
  187. process.electronApp.kill();
  188. }
  189. };
  190. exports.build = build;
  191. exports.default = electron;
  192. exports.resolveViteConfig = resolveViteConfig;
  193. exports.startup = startup;
  194. exports.withExternalBuiltins = withExternalBuiltins;