unplugin-https-reverse-proxy 1.0.0-beta.1 → 1.0.0-beta.12

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.
@@ -0,0 +1,310 @@
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
+
25
+ // scripts/postinstall.ts
26
+ var import_node_fs3 = require("fs");
27
+ var import_node_process5 = __toESM(require("process"), 1);
28
+
29
+ // src/utils.ts
30
+ var import_node_process = __toESM(require("process"), 1);
31
+ var import_node_path = __toESM(require("path"), 1);
32
+ var import_promises = require("fs/promises");
33
+ var import_node_child_process = require("child_process");
34
+ var import_consola = require("consola");
35
+ var consola = (0, import_consola.createConsola)({
36
+ level: 3
37
+ }).withTag("reverse-proxy");
38
+ function once(fn) {
39
+ let called = false;
40
+ return (...args) => {
41
+ if (called)
42
+ return;
43
+ called = true;
44
+ return fn(...args);
45
+ };
46
+ }
47
+
48
+ // src/caddy/index.ts
49
+ var import_node_os2 = require("os");
50
+ var import_node_process4 = __toESM(require("process"), 1);
51
+ var import_node_fs2 = require("fs");
52
+ var import_node_child_process3 = require("child_process");
53
+ var import_got_cjs = require("got-cjs");
54
+ var import_http_proxy_agent = require("http-proxy-agent");
55
+ var import_https_proxy_agent = require("https-proxy-agent");
56
+ var import_kill_port = __toESM(require("kill-port"), 1);
57
+
58
+ // src/host/index.ts
59
+ var import_node_process2 = __toESM(require("process"), 1);
60
+ var import_node_child_process2 = require("child_process");
61
+ var import_node_fs = require("fs");
62
+ var import_hosts_so_easy = __toESM(require("hosts-so-easy"), 1);
63
+ var Hosts = import_hosts_so_easy.default.default || import_hosts_so_easy.default;
64
+ var hostInstance = new Hosts({
65
+ debounceTime: 50,
66
+ hostsFile: getPathOfSystemHostsPath()
67
+ });
68
+ function getPathOfSystemHostsPath() {
69
+ return import_node_process2.default.platform === "win32" ? `${import_node_process2.default.env.windir || "C:\\WINDOWS"}\\system32\\drivers\\etc\\hosts` : "/etc/hosts";
70
+ }
71
+
72
+ // src/caddy/constants.ts
73
+ var import_node_path2 = require("path");
74
+ var import_node_os = __toESM(require("os"), 1);
75
+ var import_node_process3 = __toESM(require("process"), 1);
76
+ var import_dotenv_flow = __toESM(require("dotenv-flow"), 1);
77
+ import_dotenv_flow.default.config({
78
+ silent: true
79
+ });
80
+ function resolvePath(path2) {
81
+ if (path2.startsWith("~"))
82
+ return (0, import_node_path2.join)(import_node_os.default.homedir() || "", path2.slice(1));
83
+ return path2;
84
+ }
85
+ var TEMP_DIR = resolvePath(import_node_process3.default.env.UHRP_CADDY_PATH || import_node_process3.default.env.TEMP || import_node_process3.default.env.TMPDIR || "/tmp");
86
+ var caddyPath = (0, import_node_path2.join)(TEMP_DIR, `caddy${import_node_process3.default.platform === "win32" ? ".exe" : ""}`);
87
+ var caddyFilePath = (0, import_node_path2.join)(TEMP_DIR, "CADDYFILE");
88
+ var supportList = [
89
+ {
90
+ platform: "dragonfly",
91
+ arch: "amd64"
92
+ },
93
+ {
94
+ platform: "freebsd",
95
+ arch: "amd64"
96
+ },
97
+ {
98
+ platform: "freebsd",
99
+ arch: "arm-6"
100
+ },
101
+ {
102
+ platform: "freebsd",
103
+ arch: "arm-7"
104
+ },
105
+ {
106
+ platform: "freebsd",
107
+ arch: "arm64"
108
+ },
109
+ {
110
+ platform: "linux",
111
+ arch: "amd64"
112
+ },
113
+ {
114
+ platform: "linux",
115
+ arch: "arm-5"
116
+ },
117
+ {
118
+ platform: "linux",
119
+ arch: "arm-6"
120
+ },
121
+ {
122
+ platform: "linux",
123
+ arch: "arm-7"
124
+ },
125
+ {
126
+ platform: "linux",
127
+ arch: "arm64"
128
+ },
129
+ {
130
+ platform: "linux",
131
+ arch: "mips"
132
+ },
133
+ {
134
+ platform: "linux",
135
+ arch: "mips64"
136
+ },
137
+ {
138
+ platform: "linux",
139
+ arch: "mips64le"
140
+ },
141
+ {
142
+ platform: "linux",
143
+ arch: "mipsle"
144
+ },
145
+ {
146
+ platform: "linux",
147
+ arch: "ppc64"
148
+ },
149
+ {
150
+ platform: "linux",
151
+ arch: "ppc64le"
152
+ },
153
+ {
154
+ platform: "linux",
155
+ arch: "s390x"
156
+ },
157
+ {
158
+ platform: "darwin",
159
+ arch: "amd64"
160
+ },
161
+ {
162
+ platform: "darwin",
163
+ arch: "arm64"
164
+ },
165
+ {
166
+ platform: "openbsd",
167
+ arch: "amd64"
168
+ },
169
+ {
170
+ platform: "openbsd",
171
+ arch: "arm-6"
172
+ },
173
+ {
174
+ platform: "openbsd",
175
+ arch: "arm-7"
176
+ },
177
+ {
178
+ platform: "openbsd",
179
+ arch: "arm64"
180
+ },
181
+ {
182
+ platform: "windows",
183
+ arch: "amd64"
184
+ },
185
+ {
186
+ platform: "windows",
187
+ arch: "arm-6"
188
+ },
189
+ {
190
+ platform: "windows",
191
+ arch: "arm-7"
192
+ },
193
+ {
194
+ platform: "windows",
195
+ arch: "arm64"
196
+ }
197
+ ];
198
+
199
+ // src/caddy/utils.ts
200
+ var import_node_net = require("net");
201
+ var import_ora = __toESM(require("ora"), 1);
202
+ var spinner;
203
+ function getNumber(number) {
204
+ if (number <= 0)
205
+ return "0%";
206
+ else if (number < 100)
207
+ return `${number.toFixed(2)}%`;
208
+ else
209
+ return "100%";
210
+ }
211
+ function logProgress(percent) {
212
+ if (!spinner) {
213
+ consola.log("");
214
+ spinner = (0, import_ora.default)({
215
+ text: "Start Download Caddy: 0%",
216
+ indent: 2
217
+ });
218
+ spinner.start();
219
+ } else {
220
+ spinner.text = `Download Caddy: ${getNumber(Number(percent) * 100)}`;
221
+ }
222
+ }
223
+ function logProgressOver() {
224
+ if (!spinner) {
225
+ spinner = (0, import_ora.default)({
226
+ text: "Start Download Caddy: 100%",
227
+ indent: 2
228
+ });
229
+ spinner.start();
230
+ } else {
231
+ spinner.succeed();
232
+ consola.log("");
233
+ }
234
+ }
235
+
236
+ // src/caddy/index.ts
237
+ async function download() {
238
+ if (await testCaddy())
239
+ return caddyPath;
240
+ return new Promise((resolve, reject) => {
241
+ if (!(0, import_node_fs2.existsSync)(TEMP_DIR)) {
242
+ (0, import_node_fs2.mkdirSync)(TEMP_DIR, { recursive: true });
243
+ (0, import_node_fs2.chmodSync)(TEMP_DIR, 511);
244
+ }
245
+ const file = (0, import_node_fs2.createWriteStream)(caddyPath);
246
+ let _platform = (0, import_node_os2.platform)().toString();
247
+ _platform === "win32" && (_platform = "windows");
248
+ let _arch = import_node_process4.default.arch.toString();
249
+ _arch === "x64" && (_arch = "amd64");
250
+ const support = supportList.find((item) => item.platform === _platform && item.arch === _arch);
251
+ if (!support)
252
+ return reject(new Error("not support"));
253
+ const dowmloadLink = `https://caddyserver.com/api/download?os=${support.platform}&arch=${support.arch}`;
254
+ (0, import_node_fs2.existsSync)(caddyPath) && (0, import_node_fs2.unlinkSync)(caddyPath);
255
+ const httpProxy = import_node_process4.default.env.HTTP_PROXY || import_node_process4.default.env.http_proxy;
256
+ const httpsProxy = import_node_process4.default.env.HTTPS_PROXY || import_node_process4.default.env.https_proxy;
257
+ const httpAgent = httpProxy ? new import_http_proxy_agent.HttpProxyAgent(httpProxy) : void 0;
258
+ const httpsAgent = httpsProxy ? new import_https_proxy_agent.HttpsProxyAgent(httpsProxy) : void 0;
259
+ const chmodCaddyOnce = once(() => {
260
+ (0, import_node_fs2.chmodSync)(caddyPath, 511);
261
+ });
262
+ import_got_cjs.got.stream(dowmloadLink, {
263
+ agent: {
264
+ http: httpAgent,
265
+ https: httpsAgent
266
+ }
267
+ }).on("downloadProgress", (progress) => {
268
+ logProgress(progress.percent);
269
+ chmodCaddyOnce();
270
+ }).pipe(file).on("finish", () => {
271
+ logProgressOver();
272
+ if (import_node_process4.default.platform === "win32")
273
+ return resolve(caddyPath);
274
+ (0, import_node_fs2.chmodSync)(caddyPath, 511);
275
+ resolve(caddyPath);
276
+ }).on("error", (err) => {
277
+ reject(err);
278
+ });
279
+ });
280
+ }
281
+ function testCaddy() {
282
+ return new Promise((resolve, reject) => {
283
+ if (!(0, import_node_fs2.existsSync)(caddyPath))
284
+ return resolve(false);
285
+ (0, import_node_fs2.chmodSync)(caddyPath, 511);
286
+ const child = import_node_process4.default.platform === "win32" ? (0, import_node_child_process3.spawn)(caddyPath, []) : (0, import_node_child_process3.spawn)("sudo", ["-E", caddyPath]);
287
+ child.on("close", () => {
288
+ return resolve(false);
289
+ });
290
+ child.on("error", (err) => {
291
+ return reject(err);
292
+ });
293
+ child.stdout.on("data", (_data) => {
294
+ return resolve(true);
295
+ });
296
+ });
297
+ }
298
+
299
+ // scripts/postinstall.ts
300
+ async function run() {
301
+ try {
302
+ if (import_node_process5.default.env.UHRP_AUTO_INSTALL_CADDY) {
303
+ (0, import_node_fs3.existsSync)(caddyPath) && (0, import_node_fs3.unlinkSync)(caddyPath);
304
+ await download();
305
+ }
306
+ } catch (e) {
307
+ consola.error(e.code);
308
+ }
309
+ }
310
+ run();
@@ -1,7 +1,19 @@
1
1
  import { join } from 'node:path'
2
+ import os from 'node:os'
2
3
  import process from 'node:process'
4
+ import dotenvFlow from 'dotenv-flow'
3
5
 
4
- const TEMP_DIR = process.env.TEMP || process.env.TMPDIR || '/tmp'
6
+ dotenvFlow.config({
7
+ silent: true,
8
+ })
9
+
10
+ function resolvePath(path: string) {
11
+ if (path.startsWith('~'))
12
+ return join(os.homedir() || '', path.slice(1))
13
+ return path
14
+ }
15
+
16
+ export const TEMP_DIR = resolvePath(process.env.UHRP_CADDY_PATH || process.env.TEMP || process.env.TMPDIR || '/tmp')
5
17
  export const caddyPath = join(TEMP_DIR, `caddy${process.platform === 'win32' ? '.exe' : ''}`)
6
18
  export const caddyFilePath = join(TEMP_DIR, 'CADDYFILE')
7
19
 
@@ -1,14 +1,14 @@
1
1
  import { platform } from 'node:os'
2
2
  import process from 'node:process'
3
- import { chmodSync, createWriteStream, existsSync, unlinkSync } from 'node:fs'
3
+ import { chmodSync, createWriteStream, existsSync, mkdirSync, unlinkSync } from 'node:fs'
4
4
  import { spawn } from 'node:child_process'
5
5
  import { got } from 'got-cjs'
6
6
  import { HttpProxyAgent } from 'http-proxy-agent'
7
7
  import { HttpsProxyAgent } from 'https-proxy-agent'
8
8
  import kill from 'kill-port'
9
- import { consola } from '../utils'
9
+ import { chmodRecursive, consola, once } from '../utils'
10
10
  import { addHost, removeHost } from '../host'
11
- import { caddyPath, supportList } from './constants'
11
+ import { TEMP_DIR, caddyPath, supportList } from './constants'
12
12
  import { logProgress, logProgressOver, tryPort } from './utils'
13
13
 
14
14
  export async function download() {
@@ -18,6 +18,11 @@ export async function download() {
18
18
  return new Promise<string>((resolve, reject) => {
19
19
  // https://caddyserver.com/api/download?os=darwin&arch=amd64
20
20
 
21
+ if (!existsSync(TEMP_DIR)) {
22
+ mkdirSync(TEMP_DIR, { recursive: true })
23
+ chmodSync(TEMP_DIR, 0o777)
24
+ }
25
+
21
26
  const file = createWriteStream(caddyPath)
22
27
 
23
28
  let _platform = platform().toString()
@@ -42,6 +47,11 @@ export async function download() {
42
47
  const httpAgent = httpProxy ? new HttpProxyAgent(httpProxy) : undefined
43
48
  const httpsAgent = httpsProxy ? new HttpsProxyAgent(httpsProxy) : undefined
44
49
 
50
+ const chmodCaddyOnce = once(() => {
51
+ // chmod +x
52
+ chmodSync(caddyPath, 0o777)
53
+ })
54
+
45
55
  got.stream(dowmloadLink, {
46
56
  agent: {
47
57
  http: httpAgent,
@@ -49,12 +59,13 @@ export async function download() {
49
59
  },
50
60
  }).on('downloadProgress', (progress) => {
51
61
  logProgress(progress.percent)
62
+ chmodCaddyOnce()
52
63
  }).pipe(file).on('finish', () => {
53
64
  logProgressOver()
54
65
  if (process.platform === 'win32')
55
66
  return resolve(caddyPath)
56
67
  // chmod +x
57
- chmodSync(caddyPath, 0o755)
68
+ chmodSync(caddyPath, 0o777)
58
69
  resolve(caddyPath)
59
70
  }).on('error', (err) => {
60
71
  // consola.error(err)
@@ -67,7 +78,7 @@ function testCaddy() {
67
78
  return new Promise((resolve, reject) => {
68
79
  if (!existsSync(caddyPath))
69
80
  return resolve(false)
70
- chmodSync(caddyPath, 0o755)
81
+ chmodSync(caddyPath, 0o777)
71
82
  const child = process.platform === 'win32' ? spawn(caddyPath, []) : spawn('sudo', ['-E', caddyPath])
72
83
  child.on('close', () => {
73
84
  return resolve(false)
@@ -166,6 +177,22 @@ export class CaddyInstant {
166
177
 
167
178
  // @ts-expect-error override
168
179
  process.exit = async (code?: number) => {
180
+ const port = Number(source.split(':')[1])
181
+ if (!Number.isNaN(port) && await tryPort(port))
182
+ await kill(port, 'tcp')
183
+
184
+ // fix `Error: EPERM: operation not permitted`
185
+ const pwd = process.cwd()
186
+ const viteCacheDir = `${pwd}/node_modules/.vite`
187
+ if (existsSync(viteCacheDir))
188
+ chmodRecursive(viteCacheDir, 0o777)
189
+ const nuxtCacheDir = `${pwd}/.nuxt`
190
+ if (existsSync(nuxtCacheDir))
191
+ await chmodRecursive(nuxtCacheDir, 0o777)
192
+ const webpackCacheDir = `${pwd}/node_modules/.cache`
193
+ if (existsSync(webpackCacheDir))
194
+ chmodRecursive(webpackCacheDir, 0o777)
195
+
169
196
  if (!restore || this.stoped)
170
197
  return originalExit(code)
171
198
 
package/src/index.ts CHANGED
@@ -1,10 +1,9 @@
1
1
  import process from 'node:process'
2
- import { chmodSync, existsSync } from 'node:fs'
3
2
  import type { UnpluginFactory } from 'unplugin'
4
3
  import { createUnplugin } from 'unplugin'
5
- import type { ResolvedConfig, ViteDevServer } from 'vite'
4
+ import type { ResolvedConfig } from 'vite'
6
5
  import c from 'picocolors'
7
- import { consola, isAdmin } from './utils'
6
+ import { consola, isAdmin, once } from './utils'
8
7
  import type { Options } from './types'
9
8
  import { CaddyInstant } from './caddy'
10
9
 
@@ -12,43 +11,30 @@ let config: ResolvedConfig
12
11
 
13
12
  let caddy: CaddyInstant
14
13
 
15
- export function vitePrintUrls(
16
- server: ViteDevServer,
14
+ const cwd = process.cwd()
15
+
16
+ export const vitePrintUrls = once((
17
17
  options: Options,
18
18
  source: string,
19
19
  target: string,
20
20
  base: string = '/',
21
21
  _printUrls?: () => void,
22
- ) {
22
+ ) => {
23
23
  _printUrls?.()
24
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
- }
25
+ if (caddy)
26
+ return
27
+ caddy = new CaddyInstant()
28
+ caddy.run(source, target, {
29
+ base,
30
+ ...options,
31
+ }).then(() => {
32
+ const colorUrl = (url: string) => c.green(url.replace(/:(\d+)\//, (_, port) => `:${c.bold(port)}/`))
33
+ consola.success(` ${c.green('➜')} ${c.bold('run caddy reverse proxy success')}: ${colorUrl(`${options.https ? 'https' : 'http'}://${target}${base}`)}`)
34
+ }).catch((e) => {
35
+ throw e
36
+ })
37
+ })
52
38
 
53
39
  export const unpluginFactory: UnpluginFactory<Options> = options => ({
54
40
  name: 'unplugin-https-reverse-proxy',
@@ -58,20 +44,21 @@ export const unpluginFactory: UnpluginFactory<Options> = options => ({
58
44
  config = _config
59
45
  },
60
46
  configureServer(server) {
61
- if (!isAdmin()) {
62
- consola.warn('please run as administrator')
63
- return
64
- }
65
-
66
- if (config.command === 'build')
47
+ if (config.command !== 'serve')
67
48
  return
68
49
 
69
50
  const {
70
51
  enable = true,
71
52
  target = '',
72
53
  } = options
54
+
73
55
  if (!enable)
74
56
  return
57
+
58
+ if (!isAdmin()) {
59
+ consola.warn('please run as administrator')
60
+ return
61
+ }
75
62
  if (!target) {
76
63
  consola.fail('please provide target')
77
64
  return
@@ -88,15 +75,72 @@ export const unpluginFactory: UnpluginFactory<Options> = options => ({
88
75
 
89
76
  const base = server.config.base || '/'
90
77
 
91
- server.printUrls = () => vitePrintUrls(server, options, source, target, base, _printUrls)
78
+ server.printUrls = () => vitePrintUrls(options, source, target, base, _printUrls)
92
79
  },
93
80
  },
94
81
  webpack(compiler) {
82
+ if (process.env.NODE_ENV !== 'development')
83
+ return
84
+
85
+ const {
86
+ enable = true,
87
+ target = '',
88
+ } = options
89
+
90
+ if (!enable)
91
+ return
92
+
95
93
  if (!isAdmin()) {
96
94
  consola.warn('please run as administrator')
97
95
  return
98
96
  }
99
97
 
98
+ if (!target) {
99
+ consola.fail('please provide target')
100
+ return
101
+ }
102
+
103
+ const devServer = {
104
+ host: 'localhost',
105
+ port: 8080,
106
+ ...compiler.options.devServer,
107
+ // @ts-expect-error vuecli
108
+ ...(process.VUE_CLI_SERVICE?.projectOptions.devServer),
109
+ }
110
+ const source = `${devServer.host}:${devServer.port}`
111
+
112
+ if (caddy)
113
+ return
114
+
115
+ // https://github.com/webpack/webpack-dev-server/blob/master/lib/Server.js#L1913
116
+ const _close = compiler.close?.bind(compiler)
117
+ if (_close) {
118
+ compiler.close = async (callback: Parameters<typeof _close>[0]) => {
119
+ process.on('exit', () => {
120
+ _close && _close(callback)
121
+ })
122
+ }
123
+ }
124
+ else {
125
+ // webpack 4 not support yet
126
+ options.https = false
127
+ }
128
+
129
+ caddy = new CaddyInstant()
130
+
131
+ caddy.run(source, target, {
132
+ ...options,
133
+ }).then(() => {
134
+ const colorUrl = (url: string) => c.green(url.replace(/:(\d+)\//, (_, port) => `:${c.bold(port)}/`))
135
+
136
+ compiler.hooks.done.tap('unplugin-https-reverse-proxy', () => {
137
+ consola.success(` ${c.green('➜')} ${c.bold('run caddy reverse proxy success')}: ${colorUrl(`${options.https ? 'https' : 'http'}://${target}`)}`)
138
+ })
139
+ }).catch((e) => {
140
+ throw e
141
+ })
142
+ },
143
+ async rspack(compiler) {
100
144
  if (process.env.NODE_ENV !== 'development')
101
145
  return
102
146
 
@@ -104,57 +148,45 @@ export const unpluginFactory: UnpluginFactory<Options> = options => ({
104
148
  enable = true,
105
149
  target = '',
106
150
  } = options
151
+
107
152
  if (!enable)
108
153
  return
154
+
155
+ if (!isAdmin()) {
156
+ consola.warn('please run as administrator')
157
+ return
158
+ }
159
+
109
160
  if (!target) {
110
161
  consola.fail('please provide target')
111
162
  return
112
163
  }
113
164
 
114
- try {
115
- const devServer = {
116
- host: 'localhost',
117
- port: 8080,
118
- ...compiler.options.devServer,
119
- // @ts-expect-error vuecli
120
- ...process.VUE_CLI_SERVICE.projectOptions.devServer,
121
- }
122
- const source = `${devServer.host}:${devServer.port}`
165
+ const devServer = {
166
+ host: 'localhost',
167
+ port: 8080,
168
+ ...compiler.options.devServer,
169
+ ...(await (await import('@rsbuild/core'))?.loadConfig({
170
+ cwd,
171
+ }))?.content?.server,
172
+ }
123
173
 
124
- if (caddy)
125
- return
174
+ const source = `${devServer.host}:${devServer.port}`
126
175
 
127
- // https://github.com/webpack/webpack-dev-server/blob/master/lib/Server.js#L1913
128
- const _close = compiler.close?.bind(compiler)
129
- if (_close) {
130
- compiler.close = async (callback: Parameters<typeof _close>[0]) => {
131
- process.on('exit', () => {
132
- _close && _close(callback)
133
- })
134
- }
135
- }
136
- else {
137
- // webpack 4 not support yet
138
- options.https = false
139
- }
176
+ if (caddy)
177
+ return
140
178
 
141
- caddy = new CaddyInstant()
179
+ caddy = new CaddyInstant()
142
180
 
143
- caddy.run(source, target, {
144
- ...options,
145
- }).then(() => {
146
- const colorUrl = (url: string) => c.green(url.replace(/:(\d+)\//, (_, port) => `:${c.bold(port)}/`))
181
+ caddy.run(source, target, {
182
+ ...options,
183
+ }).then(() => {
184
+ const colorUrl = (url: string) => c.green(url.replace(/:(\d+)\//, (_, port) => `:${c.bold(port)}/`))
147
185
 
148
- compiler.hooks.done.tap('unplugin-https-reverse-proxy', () => {
149
- consola.success(` ${c.green('➜')} ${c.bold('run caddy reverse proxy success')}: ${colorUrl(`${options.https ? 'https' : 'http'}://${target}`)}`)
150
- })
151
- }).catch((e) => {
152
- throw e
153
- })
154
- }
155
- catch (e) {
156
- consola.error(e)
157
- }
186
+ consola.success(` ${c.green('➜')} ${c.bold('run caddy reverse proxy success')}: ${colorUrl(`${options.https ? 'https' : 'http'}://${target}`)}`)
187
+ }).catch((e) => {
188
+ throw e
189
+ })
158
190
  },
159
191
  })
160
192