unplugin-https-reverse-proxy 0.0.24 → 1.0.0-beta.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/dist/rollup.cjs CHANGED
@@ -104,7 +104,7 @@ async function syncHost() {
104
104
  import_node_process2.default.platform === "win32" ? "ipconfig /flushdns" : "sudo -E killall -HUP mDNSResponder",
105
105
  (error, stdout, stderr) => {
106
106
  if (error) {
107
- console.error(`exec error: ${error}`);
107
+ consola.error(`exec error: ${error}`);
108
108
  return resolve(false);
109
109
  }
110
110
  return resolve(true);
@@ -117,6 +117,8 @@ async function addHost(ip, host) {
117
117
  (0, import_node_fs.chmodSync)(getPathOfSystemHostsPath(), 511);
118
118
  hostInstance.add("127.0.0.1", "localhost");
119
119
  hostInstance.add(ip, host);
120
+ if (ip === "localhost")
121
+ hostInstance.add("127.0.0.1", host);
120
122
  await hostInstance.updateFinish();
121
123
  return await syncHost();
122
124
  }
@@ -413,7 +415,7 @@ var CaddyInstant = class {
413
415
  });
414
416
  }
415
417
  });
416
- const originalExit = import_node_process4.default.exit;
418
+ const originalExit = import_node_process4.default.exit.bind(import_node_process4.default);
417
419
  import_node_process4.default.exit = async (code) => {
418
420
  if (!restore || this.stoped)
419
421
  return originalExit(code);
@@ -453,7 +455,31 @@ var CaddyInstant = class {
453
455
  // src/index.ts
454
456
  var config;
455
457
  var caddy;
456
- var _servce;
458
+ function vitePrintUrls(server, options, source, target, base = "/", _printUrls) {
459
+ _printUrls == null ? void 0 : _printUrls();
460
+ const pwd = import_node_process5.default.cwd();
461
+ const viteCacheDir = `${pwd}/node_modules/.vite`;
462
+ if ((0, import_node_fs3.existsSync)(viteCacheDir))
463
+ (0, import_node_fs3.chmodSync)(viteCacheDir, 511);
464
+ const nuxtCacheDir = `${pwd}/.nuxt`;
465
+ if ((0, import_node_fs3.existsSync)(nuxtCacheDir))
466
+ (0, import_node_fs3.chmodSync)(nuxtCacheDir, 511);
467
+ try {
468
+ if (caddy)
469
+ return;
470
+ caddy = new CaddyInstant();
471
+ caddy.run(source, target, __spreadValues({
472
+ base
473
+ }, options)).then(() => {
474
+ const colorUrl = (url) => import_picocolors.default.green(url.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
475
+ consola.success(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}${base}`)}`);
476
+ }).catch((e) => {
477
+ throw e;
478
+ });
479
+ } catch (e) {
480
+ consola.error(e);
481
+ }
482
+ }
457
483
  var unpluginFactory = (options) => ({
458
484
  name: "unplugin-https-reverse-proxy",
459
485
  enforce: "pre",
@@ -462,6 +488,7 @@ var unpluginFactory = (options) => ({
462
488
  config = _config;
463
489
  },
464
490
  configureServer(server) {
491
+ var _a;
465
492
  if (!isAdmin()) {
466
493
  consola.warn("please run as administrator");
467
494
  return;
@@ -478,38 +505,15 @@ var unpluginFactory = (options) => ({
478
505
  consola.fail("please provide target");
479
506
  return;
480
507
  }
481
- _servce = server;
482
- const _printUrls = server.printUrls;
483
- server.printUrls = () => {
484
- var _a;
485
- _printUrls();
486
- const pwd = import_node_process5.default.cwd();
487
- const vireCacheDir = `${pwd}/node_modules/.vite`;
488
- if ((0, import_node_fs3.existsSync)(vireCacheDir))
489
- (0, import_node_fs3.chmodSync)(vireCacheDir, 511);
490
- let source = `localhost:${config.server.port}`;
491
- const url = (_a = server.resolvedUrls) == null ? void 0 : _a.local[0];
492
- if (url) {
493
- const u = new URL(url);
494
- source = u.host;
495
- }
496
- const base = server.config.base || "/";
497
- try {
498
- if (caddy)
499
- return;
500
- caddy = new CaddyInstant();
501
- caddy.run(source, target, __spreadValues({
502
- base
503
- }, options)).then(() => {
504
- const colorUrl = (url2) => import_picocolors.default.green(url2.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
505
- consola.success(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}${base}`)}`);
506
- }).catch((e) => {
507
- throw e;
508
- });
509
- } catch (e) {
510
- consola.error(e);
511
- }
512
- };
508
+ const _printUrls = server.printUrls.bind(server);
509
+ let source = `localhost:${config.server.port || 5173}`;
510
+ const url = (_a = server.resolvedUrls) == null ? void 0 : _a.local[0];
511
+ if (url) {
512
+ const u = new URL(url);
513
+ source = u.host;
514
+ }
515
+ const base = server.config.base || "/";
516
+ server.printUrls = () => vitePrintUrls(server, options, source, target, base, _printUrls);
513
517
  }
514
518
  },
515
519
  webpack(compiler) {
package/dist/rollup.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  unpluginFactory
3
- } from "./chunk-TVCN6ESH.js";
4
- import "./chunk-3RTK3FSZ.js";
3
+ } from "./chunk-DUCXH7CX.js";
4
+ import "./chunk-NNO3ZUSU.js";
5
5
 
6
6
  // src/rollup.ts
7
7
  import { createRollupPlugin } from "unplugin";
package/dist/utils.cjs CHANGED
@@ -31,7 +31,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
31
31
  var utils_exports = {};
32
32
  __export(utils_exports, {
33
33
  consola: () => consola,
34
- isAdmin: () => isAdmin
34
+ isAdmin: () => isAdmin,
35
+ once: () => once
35
36
  });
36
37
  module.exports = __toCommonJS(utils_exports);
37
38
  var import_node_process = __toESM(require("process"), 1);
@@ -40,6 +41,15 @@ var import_consola = require("consola");
40
41
  var consola = (0, import_consola.createConsola)({
41
42
  level: 3
42
43
  }).withTag("reverse-proxy");
44
+ function once(fn) {
45
+ let called = false;
46
+ return (...args) => {
47
+ if (called)
48
+ return;
49
+ called = true;
50
+ return fn(...args);
51
+ };
52
+ }
43
53
  function isAdmin() {
44
54
  if (import_node_process.default.platform === "win32") {
45
55
  try {
@@ -55,6 +65,7 @@ function isAdmin() {
55
65
  // Annotate the CommonJS export names for ESM import in node:
56
66
  0 && (module.exports = {
57
67
  consola,
58
- isAdmin
68
+ isAdmin,
69
+ once
59
70
  });
60
71
  exports.default = module.exports;
package/dist/utils.d.cts CHANGED
@@ -1,6 +1,7 @@
1
1
  import * as consola_dist_core from 'consola/dist/core';
2
2
 
3
3
  declare const consola: consola_dist_core.ConsolaInstance;
4
+ declare function once<T extends (...args: any[]) => any>(fn: T): (...args: Parameters<T>) => ReturnType<T>;
4
5
  declare function isAdmin(): boolean | undefined;
5
6
 
6
- export { consola, isAdmin };
7
+ export { consola, isAdmin, once };
package/dist/utils.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import * as consola_dist_core from 'consola/dist/core';
2
2
 
3
3
  declare const consola: consola_dist_core.ConsolaInstance;
4
+ declare function once<T extends (...args: any[]) => any>(fn: T): (...args: Parameters<T>) => ReturnType<T>;
4
5
  declare function isAdmin(): boolean | undefined;
5
6
 
6
- export { consola, isAdmin };
7
+ export { consola, isAdmin, once };
package/dist/utils.js CHANGED
@@ -1,8 +1,10 @@
1
1
  import {
2
2
  consola,
3
- isAdmin
4
- } from "./chunk-3RTK3FSZ.js";
3
+ isAdmin,
4
+ once
5
+ } from "./chunk-NNO3ZUSU.js";
5
6
  export {
6
7
  consola,
7
- isAdmin
8
+ isAdmin,
9
+ once
8
10
  };
package/dist/vite.cjs CHANGED
@@ -104,7 +104,7 @@ async function syncHost() {
104
104
  import_node_process2.default.platform === "win32" ? "ipconfig /flushdns" : "sudo -E killall -HUP mDNSResponder",
105
105
  (error, stdout, stderr) => {
106
106
  if (error) {
107
- console.error(`exec error: ${error}`);
107
+ consola.error(`exec error: ${error}`);
108
108
  return resolve(false);
109
109
  }
110
110
  return resolve(true);
@@ -117,6 +117,8 @@ async function addHost(ip, host) {
117
117
  (0, import_node_fs.chmodSync)(getPathOfSystemHostsPath(), 511);
118
118
  hostInstance.add("127.0.0.1", "localhost");
119
119
  hostInstance.add(ip, host);
120
+ if (ip === "localhost")
121
+ hostInstance.add("127.0.0.1", host);
120
122
  await hostInstance.updateFinish();
121
123
  return await syncHost();
122
124
  }
@@ -413,7 +415,7 @@ var CaddyInstant = class {
413
415
  });
414
416
  }
415
417
  });
416
- const originalExit = import_node_process4.default.exit;
418
+ const originalExit = import_node_process4.default.exit.bind(import_node_process4.default);
417
419
  import_node_process4.default.exit = async (code) => {
418
420
  if (!restore || this.stoped)
419
421
  return originalExit(code);
@@ -453,7 +455,31 @@ var CaddyInstant = class {
453
455
  // src/index.ts
454
456
  var config;
455
457
  var caddy;
456
- var _servce;
458
+ function vitePrintUrls(server, options, source, target, base = "/", _printUrls) {
459
+ _printUrls == null ? void 0 : _printUrls();
460
+ const pwd = import_node_process5.default.cwd();
461
+ const viteCacheDir = `${pwd}/node_modules/.vite`;
462
+ if ((0, import_node_fs3.existsSync)(viteCacheDir))
463
+ (0, import_node_fs3.chmodSync)(viteCacheDir, 511);
464
+ const nuxtCacheDir = `${pwd}/.nuxt`;
465
+ if ((0, import_node_fs3.existsSync)(nuxtCacheDir))
466
+ (0, import_node_fs3.chmodSync)(nuxtCacheDir, 511);
467
+ try {
468
+ if (caddy)
469
+ return;
470
+ caddy = new CaddyInstant();
471
+ caddy.run(source, target, __spreadValues({
472
+ base
473
+ }, options)).then(() => {
474
+ const colorUrl = (url) => import_picocolors.default.green(url.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
475
+ consola.success(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}${base}`)}`);
476
+ }).catch((e) => {
477
+ throw e;
478
+ });
479
+ } catch (e) {
480
+ consola.error(e);
481
+ }
482
+ }
457
483
  var unpluginFactory = (options) => ({
458
484
  name: "unplugin-https-reverse-proxy",
459
485
  enforce: "pre",
@@ -462,6 +488,7 @@ var unpluginFactory = (options) => ({
462
488
  config = _config;
463
489
  },
464
490
  configureServer(server) {
491
+ var _a;
465
492
  if (!isAdmin()) {
466
493
  consola.warn("please run as administrator");
467
494
  return;
@@ -478,38 +505,15 @@ var unpluginFactory = (options) => ({
478
505
  consola.fail("please provide target");
479
506
  return;
480
507
  }
481
- _servce = server;
482
- const _printUrls = server.printUrls;
483
- server.printUrls = () => {
484
- var _a;
485
- _printUrls();
486
- const pwd = import_node_process5.default.cwd();
487
- const vireCacheDir = `${pwd}/node_modules/.vite`;
488
- if ((0, import_node_fs3.existsSync)(vireCacheDir))
489
- (0, import_node_fs3.chmodSync)(vireCacheDir, 511);
490
- let source = `localhost:${config.server.port}`;
491
- const url = (_a = server.resolvedUrls) == null ? void 0 : _a.local[0];
492
- if (url) {
493
- const u = new URL(url);
494
- source = u.host;
495
- }
496
- const base = server.config.base || "/";
497
- try {
498
- if (caddy)
499
- return;
500
- caddy = new CaddyInstant();
501
- caddy.run(source, target, __spreadValues({
502
- base
503
- }, options)).then(() => {
504
- const colorUrl = (url2) => import_picocolors.default.green(url2.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
505
- consola.success(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}${base}`)}`);
506
- }).catch((e) => {
507
- throw e;
508
- });
509
- } catch (e) {
510
- consola.error(e);
511
- }
512
- };
508
+ const _printUrls = server.printUrls.bind(server);
509
+ let source = `localhost:${config.server.port || 5173}`;
510
+ const url = (_a = server.resolvedUrls) == null ? void 0 : _a.local[0];
511
+ if (url) {
512
+ const u = new URL(url);
513
+ source = u.host;
514
+ }
515
+ const base = server.config.base || "/";
516
+ server.printUrls = () => vitePrintUrls(server, options, source, target, base, _printUrls);
513
517
  }
514
518
  },
515
519
  webpack(compiler) {
package/dist/vite.js CHANGED
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  vite_default
3
- } from "./chunk-Q7WB6WMX.js";
4
- import "./chunk-TVCN6ESH.js";
5
- import "./chunk-3RTK3FSZ.js";
3
+ } from "./chunk-Y5ZVZOMP.js";
4
+ import "./chunk-DUCXH7CX.js";
5
+ import "./chunk-NNO3ZUSU.js";
6
6
  export {
7
7
  vite_default as default
8
8
  };
package/dist/webpack.cjs CHANGED
@@ -104,7 +104,7 @@ async function syncHost() {
104
104
  import_node_process2.default.platform === "win32" ? "ipconfig /flushdns" : "sudo -E killall -HUP mDNSResponder",
105
105
  (error, stdout, stderr) => {
106
106
  if (error) {
107
- console.error(`exec error: ${error}`);
107
+ consola.error(`exec error: ${error}`);
108
108
  return resolve(false);
109
109
  }
110
110
  return resolve(true);
@@ -117,6 +117,8 @@ async function addHost(ip, host) {
117
117
  (0, import_node_fs.chmodSync)(getPathOfSystemHostsPath(), 511);
118
118
  hostInstance.add("127.0.0.1", "localhost");
119
119
  hostInstance.add(ip, host);
120
+ if (ip === "localhost")
121
+ hostInstance.add("127.0.0.1", host);
120
122
  await hostInstance.updateFinish();
121
123
  return await syncHost();
122
124
  }
@@ -413,7 +415,7 @@ var CaddyInstant = class {
413
415
  });
414
416
  }
415
417
  });
416
- const originalExit = import_node_process4.default.exit;
418
+ const originalExit = import_node_process4.default.exit.bind(import_node_process4.default);
417
419
  import_node_process4.default.exit = async (code) => {
418
420
  if (!restore || this.stoped)
419
421
  return originalExit(code);
@@ -453,7 +455,31 @@ var CaddyInstant = class {
453
455
  // src/index.ts
454
456
  var config;
455
457
  var caddy;
456
- var _servce;
458
+ function vitePrintUrls(server, options, source, target, base = "/", _printUrls) {
459
+ _printUrls == null ? void 0 : _printUrls();
460
+ const pwd = import_node_process5.default.cwd();
461
+ const viteCacheDir = `${pwd}/node_modules/.vite`;
462
+ if ((0, import_node_fs3.existsSync)(viteCacheDir))
463
+ (0, import_node_fs3.chmodSync)(viteCacheDir, 511);
464
+ const nuxtCacheDir = `${pwd}/.nuxt`;
465
+ if ((0, import_node_fs3.existsSync)(nuxtCacheDir))
466
+ (0, import_node_fs3.chmodSync)(nuxtCacheDir, 511);
467
+ try {
468
+ if (caddy)
469
+ return;
470
+ caddy = new CaddyInstant();
471
+ caddy.run(source, target, __spreadValues({
472
+ base
473
+ }, options)).then(() => {
474
+ const colorUrl = (url) => import_picocolors.default.green(url.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
475
+ consola.success(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}${base}`)}`);
476
+ }).catch((e) => {
477
+ throw e;
478
+ });
479
+ } catch (e) {
480
+ consola.error(e);
481
+ }
482
+ }
457
483
  var unpluginFactory = (options) => ({
458
484
  name: "unplugin-https-reverse-proxy",
459
485
  enforce: "pre",
@@ -462,6 +488,7 @@ var unpluginFactory = (options) => ({
462
488
  config = _config;
463
489
  },
464
490
  configureServer(server) {
491
+ var _a;
465
492
  if (!isAdmin()) {
466
493
  consola.warn("please run as administrator");
467
494
  return;
@@ -478,38 +505,15 @@ var unpluginFactory = (options) => ({
478
505
  consola.fail("please provide target");
479
506
  return;
480
507
  }
481
- _servce = server;
482
- const _printUrls = server.printUrls;
483
- server.printUrls = () => {
484
- var _a;
485
- _printUrls();
486
- const pwd = import_node_process5.default.cwd();
487
- const vireCacheDir = `${pwd}/node_modules/.vite`;
488
- if ((0, import_node_fs3.existsSync)(vireCacheDir))
489
- (0, import_node_fs3.chmodSync)(vireCacheDir, 511);
490
- let source = `localhost:${config.server.port}`;
491
- const url = (_a = server.resolvedUrls) == null ? void 0 : _a.local[0];
492
- if (url) {
493
- const u = new URL(url);
494
- source = u.host;
495
- }
496
- const base = server.config.base || "/";
497
- try {
498
- if (caddy)
499
- return;
500
- caddy = new CaddyInstant();
501
- caddy.run(source, target, __spreadValues({
502
- base
503
- }, options)).then(() => {
504
- const colorUrl = (url2) => import_picocolors.default.green(url2.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
505
- consola.success(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}${base}`)}`);
506
- }).catch((e) => {
507
- throw e;
508
- });
509
- } catch (e) {
510
- consola.error(e);
511
- }
512
- };
508
+ const _printUrls = server.printUrls.bind(server);
509
+ let source = `localhost:${config.server.port || 5173}`;
510
+ const url = (_a = server.resolvedUrls) == null ? void 0 : _a.local[0];
511
+ if (url) {
512
+ const u = new URL(url);
513
+ source = u.host;
514
+ }
515
+ const base = server.config.base || "/";
516
+ server.printUrls = () => vitePrintUrls(server, options, source, target, base, _printUrls);
513
517
  }
514
518
  },
515
519
  webpack(compiler) {
package/dist/webpack.js CHANGED
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  webpack_default
3
- } from "./chunk-W4RHTMNZ.js";
4
- import "./chunk-TVCN6ESH.js";
5
- import "./chunk-3RTK3FSZ.js";
3
+ } from "./chunk-QIKJQBZH.js";
4
+ import "./chunk-DUCXH7CX.js";
5
+ import "./chunk-NNO3ZUSU.js";
6
6
  export {
7
7
  webpack_default as default
8
8
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "unplugin-https-reverse-proxy",
3
3
  "type": "module",
4
- "version": "0.0.24",
4
+ "version": "1.0.0-beta.1",
5
5
  "packageManager": "pnpm@8.6.12",
6
6
  "description": "",
7
7
  "license": "MIT",
@@ -141,6 +141,7 @@
141
141
  "build:fix": "esno scripts/postbuild.ts",
142
142
  "lint": "eslint .",
143
143
  "play": "npm -C examples/vite run dev",
144
+ "play:nuxt": "npm -C examples/nuxt run dev",
144
145
  "play:vuecli": "npm -C examples/vuecli run serve",
145
146
  "play:vuecli4": "npm -C examples/vuecli4 run serve",
146
147
  "release": "bumpp",
@@ -162,7 +162,7 @@ export class CaddyInstant {
162
162
  }
163
163
  })
164
164
 
165
- const originalExit = process.exit
165
+ const originalExit = process.exit.bind(process)
166
166
 
167
167
  // @ts-expect-error override
168
168
  process.exit = async (code?: number) => {
package/src/host/index.ts CHANGED
@@ -4,6 +4,7 @@ import { chmodSync } from 'node:fs'
4
4
 
5
5
  // @ts-expect-error no type
6
6
  import _Hosts from 'hosts-so-easy'
7
+ import { consola } from '../utils'
7
8
 
8
9
  const Hosts = _Hosts.default || _Hosts
9
10
 
@@ -26,7 +27,7 @@ async function syncHost() {
26
27
  : 'sudo -E killall -HUP mDNSResponder',
27
28
  (error, stdout, stderr) => {
28
29
  if (error) {
29
- console.error(`exec error: ${error}`)
30
+ consola.error(`exec error: ${error}`)
30
31
  return resolve(false)
31
32
  }
32
33
  return resolve(true)
@@ -39,6 +40,8 @@ export async function addHost(ip: string, host: string) {
39
40
  chmodSync(getPathOfSystemHostsPath(), 0o777)
40
41
  hostInstance.add('127.0.0.1', 'localhost')
41
42
  hostInstance.add(ip, host)
43
+ if (ip === 'localhost')
44
+ hostInstance.add('127.0.0.1', host)
42
45
  await hostInstance.updateFinish()
43
46
  return await syncHost()
44
47
  }
package/src/index.ts CHANGED
@@ -12,7 +12,43 @@ let config: ResolvedConfig
12
12
 
13
13
  let caddy: CaddyInstant
14
14
 
15
- let _servce: ViteDevServer
15
+ export function vitePrintUrls(
16
+ server: ViteDevServer,
17
+ options: Options,
18
+ source: string,
19
+ target: string,
20
+ base: string = '/',
21
+ _printUrls?: () => void,
22
+ ) {
23
+ _printUrls?.()
24
+
25
+ // fix `Error: EPERM: operation not permitted`
26
+ const pwd = process.cwd()
27
+ const viteCacheDir = `${pwd}/node_modules/.vite`
28
+ if (existsSync(viteCacheDir))
29
+ chmodSync(viteCacheDir, 0o777)
30
+ const nuxtCacheDir = `${pwd}/.nuxt`
31
+ if (existsSync(nuxtCacheDir))
32
+ chmodSync(nuxtCacheDir, 0o777)
33
+
34
+ try {
35
+ if (caddy)
36
+ return
37
+ caddy = new CaddyInstant()
38
+ caddy.run(source, target, {
39
+ base,
40
+ ...options,
41
+ }).then(() => {
42
+ const colorUrl = (url: string) => c.green(url.replace(/:(\d+)\//, (_, port) => `:${c.bold(port)}/`))
43
+ consola.success(` ${c.green('➜')} ${c.bold('run caddy reverse proxy success')}: ${colorUrl(`${options.https ? 'https' : 'http'}://${target}${base}`)}`)
44
+ }).catch((e) => {
45
+ throw e
46
+ })
47
+ }
48
+ catch (e) {
49
+ consola.error(e)
50
+ }
51
+ }
16
52
 
17
53
  export const unpluginFactory: UnpluginFactory<Options> = options => ({
18
54
  name: 'unplugin-https-reverse-proxy',
@@ -41,43 +77,18 @@ export const unpluginFactory: UnpluginFactory<Options> = options => ({
41
77
  return
42
78
  }
43
79
 
44
- _servce = server
80
+ const _printUrls = server.printUrls.bind(server)
45
81
 
46
- const _printUrls = server.printUrls
47
- server.printUrls = () => {
48
- _printUrls()
82
+ let source = `localhost:${config.server.port || 5173}`
83
+ const url = server.resolvedUrls?.local[0]
84
+ if (url) {
85
+ const u = new URL(url)
86
+ source = u.host
87
+ }
49
88
 
50
- // fix `Error: EPERM: operation not permitted`
51
- const pwd = process.cwd()
52
- const vireCacheDir = `${pwd}/node_modules/.vite`
53
- if (existsSync(vireCacheDir))
54
- chmodSync(vireCacheDir, 0o777)
89
+ const base = server.config.base || '/'
55
90
 
56
- let source = `localhost:${config.server.port}`
57
- const url = server.resolvedUrls?.local[0]
58
- if (url) {
59
- const u = new URL(url)
60
- source = u.host
61
- }
62
- const base = server.config.base || '/'
63
- try {
64
- if (caddy)
65
- return
66
- caddy = new CaddyInstant()
67
- caddy.run(source, target, {
68
- base,
69
- ...options,
70
- }).then(() => {
71
- const colorUrl = (url: string) => c.green(url.replace(/:(\d+)\//, (_, port) => `:${c.bold(port)}/`))
72
- consola.success(` ${c.green('➜')} ${c.bold('run caddy reverse proxy success')}: ${colorUrl(`${options.https ? 'https' : 'http'}://${target}${base}`)}`)
73
- }).catch((e) => {
74
- throw e
75
- })
76
- }
77
- catch (e) {
78
- consola.error(e)
79
- }
80
- }
91
+ server.printUrls = () => vitePrintUrls(server, options, source, target, base, _printUrls)
81
92
  },
82
93
  },
83
94
  webpack(compiler) {
package/src/nuxt.ts CHANGED
@@ -1,8 +1,11 @@
1
1
  import { addVitePlugin, addWebpackPlugin, defineNuxtModule } from '@nuxt/kit'
2
+ import type { ViteDevServer } from 'vite'
2
3
  import vite from './vite'
3
4
  import webpack from './webpack'
4
5
  import type { Options } from './types'
5
6
  import '@nuxt/schema'
7
+ import { consola, once } from './utils'
8
+ import { vitePrintUrls } from '.'
6
9
 
7
10
  export interface ModuleOptions extends Options {
8
11
 
@@ -17,6 +20,23 @@ export default defineNuxtModule<ModuleOptions>({
17
20
  addVitePlugin(() => vite(options))
18
21
  addWebpackPlugin(() => webpack(options))
19
22
 
20
- // ...
23
+ // https://github.com/nuxt/cli/issues/220#issuecomment-1735394141
24
+ let url = ''
25
+ _nuxt.hook('listen', (_, listener) => {
26
+ url = listener.url
27
+ })
28
+
29
+ _nuxt.hook('vite:serverCreated', once((server: ViteDevServer) => {
30
+ const source = new URL(url).host
31
+
32
+ if (!options.target) {
33
+ consola.fail('please provide target')
34
+ return
35
+ }
36
+
37
+ const base = _nuxt.options.app.baseURL || '/'
38
+
39
+ vitePrintUrls(server, options, source, options.target, base)
40
+ }))
21
41
  },
22
42
  })
package/src/utils.ts CHANGED
@@ -6,6 +6,16 @@ export const consola = createConsola({
6
6
  level: 3,
7
7
  }).withTag('reverse-proxy')
8
8
 
9
+ export function once<T extends (...args: any[]) => any>(fn: T): (...args: Parameters<T>) => ReturnType<T> {
10
+ let called = false
11
+ return (...args: any[]) => {
12
+ if (called)
13
+ return
14
+ called = true
15
+ return fn(...args)
16
+ }
17
+ }
18
+
9
19
  export function isAdmin() {
10
20
  if (process.platform === 'win32') {
11
21
  try {