@tomjs/vite-plugin-electron 1.6.0 → 1.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -96,7 +96,7 @@ import { app, BrowserWindow } from 'electron';
96
96
  global.__dirname = dirname(fileURLToPath(import.meta.url));
97
97
 
98
98
  const preload = join(__dirname, '../preload/index.mjs');
99
- const url = process.env.APP_DEV_SERVER_URL;
99
+ const url = process.env.VITE_DEV_SERVER_URL;
100
100
 
101
101
  async function createWindow() {
102
102
  win = new BrowserWindow({
@@ -314,9 +314,9 @@ const config = {
314
314
 
315
315
  Electron `main process` and `renderer process` use.
316
316
 
317
- | Variable | Description |
318
- | ------------------ | -------------------------- |
319
- | APP_DEV_SERVER_URL | The url of the dev server. |
317
+ | Variable | Description |
318
+ | --------------------- | -------------------------- |
319
+ | `VITE_DEV_SERVER_URL` | The url of the dev server. |
320
320
 
321
321
  ## Debug
322
322
 
package/README.zh_CN.md CHANGED
@@ -96,7 +96,7 @@ import { app, BrowserWindow } from 'electron';
96
96
  global.__dirname = dirname(fileURLToPath(import.meta.url));
97
97
 
98
98
  const preload = join(__dirname, '../preload/index.mjs');
99
- const url = process.env.APP_DEV_SERVER_URL;
99
+ const url = process.env.VITE_DEV_SERVER_URL;
100
100
 
101
101
  async function createWindow() {
102
102
  win = new BrowserWindow({
@@ -313,9 +313,9 @@ const config = {
313
313
 
314
314
  Electron `main process` 和 `renderer process` 使用。
315
315
 
316
- | 变量 | 描述 |
317
- | ------------------ | --------------------- |
318
- | APP_DEV_SERVER_URL | Vite 开发服务器的 URL |
316
+ | 变量 | 描述 |
317
+ | --------------------- | --------------------- |
318
+ | `VITE_DEV_SERVER_URL` | Vite 开发服务器的 URL |
319
319
 
320
320
  ## 调试
321
321
 
package/dist/index.js CHANGED
@@ -1,6 +1,5 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }// src/index.ts
2
2
  var _fs = require('fs'); var _fs2 = _interopRequireDefault(_fs);
3
- var _process = require('process');
4
3
  var _lodashclonedeep = require('lodash.clonedeep'); var _lodashclonedeep2 = _interopRequireDefault(_lodashclonedeep);
5
4
  var _lodashmerge = require('lodash.merge'); var _lodashmerge2 = _interopRequireDefault(_lodashmerge);
6
5
  var _path = require('path'); var _path2 = _interopRequireDefault(_path);
@@ -8,7 +7,7 @@ var _path = require('path'); var _path2 = _interopRequireDefault(_path);
8
7
  // src/builder.ts
9
8
  var _os = require('os'); var _os2 = _interopRequireDefault(_os);
10
9
 
11
-
10
+ var _process = require('process');
12
11
 
13
12
  var _shelljs = require('shelljs'); var _shelljs2 = _interopRequireDefault(_shelljs);
14
13
 
@@ -18,7 +17,6 @@ var _kolorist = require('kolorist');
18
17
 
19
18
  // src/constants.ts
20
19
  var PLUGIN_NAME = "@tomjs:electron";
21
- var PACKAGE_NAME = "@tomjs/vite-plugin-electron";
22
20
 
23
21
  // src/logger.ts
24
22
  var Logger = class {
@@ -71,6 +69,30 @@ function readJson(path3) {
71
69
  function writeJson(path3, data) {
72
70
  _fs2.default.writeFileSync(path3, JSON.stringify(data, null, 2), "utf8");
73
71
  }
72
+ function resolveHostname(hostname) {
73
+ const loopbackHosts = /* @__PURE__ */ new Set([
74
+ "localhost",
75
+ "127.0.0.1",
76
+ "::1",
77
+ "0000:0000:0000:0000:0000:0000:0000:0001"
78
+ ]);
79
+ const wildcardHosts = /* @__PURE__ */ new Set(["0.0.0.0", "::", "0000:0000:0000:0000:0000:0000:0000:0000"]);
80
+ return loopbackHosts.has(hostname) || wildcardHosts.has(hostname) ? "localhost" : hostname;
81
+ }
82
+ function resolveServerUrl(server) {
83
+ const addressInfo = server.httpServer.address();
84
+ const isAddressInfo = (x) => x == null ? void 0 : x.address;
85
+ if (isAddressInfo(addressInfo)) {
86
+ const { address, port } = addressInfo;
87
+ const hostname = resolveHostname(address);
88
+ const options = server.config.server;
89
+ const protocol = options.https ? "https" : "http";
90
+ const devBase = server.config.base;
91
+ const path3 = typeof options.open === "string" ? options.open : devBase;
92
+ const url = path3.startsWith("http") ? path3 : `${protocol}://${hostname}:${port}${path3}`;
93
+ return url;
94
+ }
95
+ }
74
96
 
75
97
  // src/builder.ts
76
98
  var logger = createLogger();
@@ -373,6 +395,11 @@ function preMergeOptions(options) {
373
395
  } else if (typeof entry === "string") {
374
396
  opt.entry = [entry];
375
397
  }
398
+ if (isDev) {
399
+ opt.sourcemap ??= true;
400
+ } else {
401
+ opt.minify ??= true;
402
+ }
376
403
  const external = opt.external || opts.external || ["electron"];
377
404
  opt.external = [...new Set(["electron"].concat(external))];
378
405
  });
@@ -420,24 +447,7 @@ function useElectronPlugin(options) {
420
447
  opts.main.outDir ||= _path2.default.join("dist-electron", "main");
421
448
  opts.preload.outDir ||= _path2.default.join("dist-electron", "preload");
422
449
  }
423
- if (isDev) {
424
- opts.main.sourcemap ??= true;
425
- opts.preload.sourcemap ??= true;
426
- } else {
427
- opts.main.minify ??= true;
428
- opts.preload.minify ??= true;
429
- }
430
- let envPrefix = config.envPrefix;
431
- if (!envPrefix) {
432
- envPrefix = ["VITE_"];
433
- } else if (typeof envPrefix === "string") {
434
- envPrefix = [envPrefix];
435
- }
436
- if (!envPrefix.includes("APP_")) {
437
- envPrefix.push("APP_");
438
- }
439
450
  return {
440
- envPrefix: [...new Set(envPrefix)],
441
451
  build: {
442
452
  outDir
443
453
  }
@@ -454,19 +464,13 @@ function useElectronPlugin(options) {
454
464
  return;
455
465
  }
456
466
  server.httpServer.on("listening", async () => {
457
- var _a;
458
- const serve = (_a = server.httpServer) == null ? void 0 : _a.address();
459
- const { address, port, family } = serve;
460
- const hostname = family === "IPv6" ? `[${address}]` : address;
461
- const protocol = server.config.server.https ? "https" : "http";
462
- process.env.APP_DEV_SERVER_URL = `${protocol}://${hostname}:${port}`;
463
- const DEBUG_PATH = _path2.default.resolve(_process.cwd.call(void 0, ), "node_modules", PACKAGE_NAME, "debug");
464
- if (!_fs2.default.existsSync(DEBUG_PATH)) {
465
- _fs.mkdirSync.call(void 0, DEBUG_PATH, { recursive: true });
466
- }
467
- const env = Object.keys(process.env).filter((s) => s.startsWith("APP_") || s.startsWith("VITE_")).map((s) => `${s}=${process.env[s]}`).join("\n");
468
- _fs.writeFileSync.call(void 0, _path2.default.join(DEBUG_PATH, ".env"), `NODE_ENV=development
469
- ${env}`);
467
+ const env = {
468
+ NODE_ENV: server.config.mode || "development",
469
+ VITE_DEV_SERVER_URL: resolveServerUrl(server)
470
+ };
471
+ ["main", "preload"].forEach((prop) => {
472
+ opts[prop].env = env;
473
+ });
470
474
  await runServe(opts, server);
471
475
  });
472
476
  },
package/dist/index.mjs CHANGED
@@ -1,6 +1,5 @@
1
1
  // src/index.ts
2
- import fs2, { mkdirSync, writeFileSync } from "fs";
3
- import { cwd as cwd2 } from "process";
2
+ import fs2 from "fs";
4
3
  import cloneDeep from "lodash.clonedeep";
5
4
  import merge2 from "lodash.merge";
6
5
  import path2 from "path";
@@ -18,7 +17,6 @@ import { blue, gray, green, red, yellow } from "kolorist";
18
17
 
19
18
  // src/constants.ts
20
19
  var PLUGIN_NAME = "@tomjs:electron";
21
- var PACKAGE_NAME = "@tomjs/vite-plugin-electron";
22
20
 
23
21
  // src/logger.ts
24
22
  var Logger = class {
@@ -70,6 +68,30 @@ function readJson(path3) {
70
68
  function writeJson(path3, data) {
71
69
  fs.writeFileSync(path3, JSON.stringify(data, null, 2), "utf8");
72
70
  }
71
+ function resolveHostname(hostname) {
72
+ const loopbackHosts = /* @__PURE__ */ new Set([
73
+ "localhost",
74
+ "127.0.0.1",
75
+ "::1",
76
+ "0000:0000:0000:0000:0000:0000:0000:0001"
77
+ ]);
78
+ const wildcardHosts = /* @__PURE__ */ new Set(["0.0.0.0", "::", "0000:0000:0000:0000:0000:0000:0000:0000"]);
79
+ return loopbackHosts.has(hostname) || wildcardHosts.has(hostname) ? "localhost" : hostname;
80
+ }
81
+ function resolveServerUrl(server) {
82
+ const addressInfo = server.httpServer.address();
83
+ const isAddressInfo = (x) => x == null ? void 0 : x.address;
84
+ if (isAddressInfo(addressInfo)) {
85
+ const { address, port } = addressInfo;
86
+ const hostname = resolveHostname(address);
87
+ const options = server.config.server;
88
+ const protocol = options.https ? "https" : "http";
89
+ const devBase = server.config.base;
90
+ const path3 = typeof options.open === "string" ? options.open : devBase;
91
+ const url = path3.startsWith("http") ? path3 : `${protocol}://${hostname}:${port}${path3}`;
92
+ return url;
93
+ }
94
+ }
73
95
 
74
96
  // src/builder.ts
75
97
  var logger = createLogger();
@@ -372,6 +394,11 @@ function preMergeOptions(options) {
372
394
  } else if (typeof entry === "string") {
373
395
  opt.entry = [entry];
374
396
  }
397
+ if (isDev) {
398
+ opt.sourcemap ??= true;
399
+ } else {
400
+ opt.minify ??= true;
401
+ }
375
402
  const external = opt.external || opts.external || ["electron"];
376
403
  opt.external = [...new Set(["electron"].concat(external))];
377
404
  });
@@ -419,24 +446,7 @@ function useElectronPlugin(options) {
419
446
  opts.main.outDir ||= path2.join("dist-electron", "main");
420
447
  opts.preload.outDir ||= path2.join("dist-electron", "preload");
421
448
  }
422
- if (isDev) {
423
- opts.main.sourcemap ??= true;
424
- opts.preload.sourcemap ??= true;
425
- } else {
426
- opts.main.minify ??= true;
427
- opts.preload.minify ??= true;
428
- }
429
- let envPrefix = config.envPrefix;
430
- if (!envPrefix) {
431
- envPrefix = ["VITE_"];
432
- } else if (typeof envPrefix === "string") {
433
- envPrefix = [envPrefix];
434
- }
435
- if (!envPrefix.includes("APP_")) {
436
- envPrefix.push("APP_");
437
- }
438
449
  return {
439
- envPrefix: [...new Set(envPrefix)],
440
450
  build: {
441
451
  outDir
442
452
  }
@@ -453,19 +463,13 @@ function useElectronPlugin(options) {
453
463
  return;
454
464
  }
455
465
  server.httpServer.on("listening", async () => {
456
- var _a;
457
- const serve = (_a = server.httpServer) == null ? void 0 : _a.address();
458
- const { address, port, family } = serve;
459
- const hostname = family === "IPv6" ? `[${address}]` : address;
460
- const protocol = server.config.server.https ? "https" : "http";
461
- process.env.APP_DEV_SERVER_URL = `${protocol}://${hostname}:${port}`;
462
- const DEBUG_PATH = path2.resolve(cwd2(), "node_modules", PACKAGE_NAME, "debug");
463
- if (!fs2.existsSync(DEBUG_PATH)) {
464
- mkdirSync(DEBUG_PATH, { recursive: true });
465
- }
466
- const env = Object.keys(process.env).filter((s) => s.startsWith("APP_") || s.startsWith("VITE_")).map((s) => `${s}=${process.env[s]}`).join("\n");
467
- writeFileSync(path2.join(DEBUG_PATH, ".env"), `NODE_ENV=development
468
- ${env}`);
466
+ const env = {
467
+ NODE_ENV: server.config.mode || "development",
468
+ VITE_DEV_SERVER_URL: resolveServerUrl(server)
469
+ };
470
+ ["main", "preload"].forEach((prop) => {
471
+ opts[prop].env = env;
472
+ });
469
473
  await runServe(opts, server);
470
474
  });
471
475
  },
package/env.d.ts CHANGED
@@ -12,6 +12,6 @@ declare namespace NodeJS {
12
12
  /**
13
13
  * The url of the dev server.
14
14
  */
15
- APP_DEV_SERVER_URL?: string;
15
+ VITE_DEV_SERVER_URL?: string;
16
16
  }
17
17
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tomjs/vite-plugin-electron",
3
- "version": "1.6.0",
3
+ "version": "1.7.1",
4
4
  "description": "A simple vite plugin for electron, supports esm/cjs, support esm in electron v28+",
5
5
  "keywords": [
6
6
  "vite",
@@ -38,7 +38,7 @@
38
38
  },
39
39
  "repository": {
40
40
  "type": "git",
41
- "url": "git+https://github.com/tomgao365/vite-plugin-electron.git"
41
+ "url": "git+https://github.com/tomjs/vite-plugin-electron.git"
42
42
  },
43
43
  "dependencies": {
44
44
  "dayjs": "^1.11.10",
@@ -51,25 +51,25 @@
51
51
  },
52
52
  "devDependencies": {
53
53
  "@commitlint/cli": "^18.4.3",
54
- "@tomjs/commitlint": "^2.0.5",
55
- "@tomjs/eslint": "^1.1.1",
56
- "@tomjs/prettier": "^1.0.6",
57
- "@tomjs/stylelint": "^1.1.1",
58
- "@tomjs/tsconfig": "^1.1.2",
54
+ "@tomjs/commitlint": "^2.1.1",
55
+ "@tomjs/eslint": "^1.2.1",
56
+ "@tomjs/prettier": "^1.1.1",
57
+ "@tomjs/stylelint": "^2.1.1",
58
+ "@tomjs/tsconfig": "^1.2.1",
59
59
  "@types/lodash.clonedeep": "^4.5.9",
60
60
  "@types/lodash.merge": "^4.6.9",
61
61
  "@types/node": "^18.19.3",
62
62
  "@types/shelljs": "^0.8.15",
63
- "eslint": "^8.55.0",
63
+ "eslint": "^8.56.0",
64
64
  "husky": "^8.0.3",
65
65
  "lint-staged": "^15.2.0",
66
66
  "np": "^9.2.0",
67
67
  "npm-run-all": "^4.1.5",
68
68
  "prettier": "^3.1.1",
69
69
  "rimraf": "^5.0.5",
70
- "stylelint": "^15.11.0",
71
- "tsx": "^4.6.2",
72
- "typescript": "~5.2.2"
70
+ "stylelint": "^16.0.2",
71
+ "tsx": "^4.7.0",
72
+ "typescript": "~5.3.3"
73
73
  },
74
74
  "peerDependencies": {
75
75
  "electron": ">=12.0.0",