unplugin-https-reverse-proxy 3.0.0 → 4.0.0

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/vite.cjs CHANGED
@@ -13476,33 +13476,63 @@ var caddyTemplate = `{
13476
13476
  }
13477
13477
  }
13478
13478
 
13479
- {% for p in proxies %}
13480
- {{ p.target }}{{ p.portSuffix }} {
13481
- {{ p.tls }}
13482
- reverse_proxy http://{{ p.source }} {% if p.healthCheck %} {
13483
- health_uri {{ p.base }}
13484
- health_interval 2s
13485
- health_timeout 5s
13486
- fail_duration 2s
13487
- unhealthy_status 502 503 504 404
13488
- lb_try_duration 3s
13489
- lb_try_interval 300ms
13479
+ {% for host in hosts %}
13480
+ {{ host.host }}{{ host.portSuffix }} {
13481
+ {{ host.tls }}
13490
13482
 
13491
- @error status 502 503 504 404
13492
- handle_response @error {
13493
- respond * 503 {
13494
- body "Service Unavailable"
13495
- close
13483
+ {% for route in host.routes %}
13484
+ handle {{ route.base }}* {
13485
+ reverse_proxy http://{{ route.source }} {% if route.healthCheck %} {
13486
+ health_uri {{ route.base }}
13487
+ health_interval 2s
13488
+ health_timeout 5s
13489
+ health_headers {
13490
+ User-Agent {http.request.header.User-Agent}
13491
+ Accept text/html,application/json,*/*
13492
+ }
13493
+ fail_duration 2s
13494
+ unhealthy_status 502 503 504 404
13495
+ lb_try_duration 3s
13496
+ lb_try_interval 300ms
13497
+
13498
+ @error status 502 503 504 404
13499
+ handle_response @error {
13500
+ respond * 503 {
13501
+ body "Service Unavailable"
13502
+ close
13503
+ }
13496
13504
  }
13497
13505
  }
13506
+ {% endif %}
13498
13507
  }
13499
- {% endif %}
13508
+ {% endfor %}
13500
13509
  }
13501
13510
  {% endfor %}
13502
13511
  `;
13503
13512
  var liquid = new Liquid();
13504
13513
  async function genCaddyfile(ctx) {
13505
- return await liquid.parseAndRender(caddyTemplate, ctx);
13514
+ const hostsMap = /* @__PURE__ */ new Map();
13515
+ for (const proxy of ctx.proxies) {
13516
+ const key = `${proxy.target}${proxy.portSuffix}`;
13517
+ if (!hostsMap.has(key)) {
13518
+ hostsMap.set(key, {
13519
+ host: proxy.target,
13520
+ portSuffix: proxy.portSuffix,
13521
+ tls: proxy.tls,
13522
+ routes: []
13523
+ });
13524
+ }
13525
+ let base = proxy.base || "/";
13526
+ if (!base.startsWith("/"))
13527
+ base = `/${base}`;
13528
+ if (base !== "/" && !base.endsWith("/"))
13529
+ base = `${base}/`;
13530
+ hostsMap.get(key).routes.push({ ...proxy, base });
13531
+ }
13532
+ for (const host of hostsMap.values())
13533
+ host.routes.sort((a, b) => b.base.length - a.base.length);
13534
+ const hosts = Array.from(hostsMap.values());
13535
+ return await liquid.parseAndRender(caddyTemplate, { ...ctx, hosts });
13506
13536
  }
13507
13537
 
13508
13538
  // src/caddy/config.ts
@@ -13667,8 +13697,13 @@ function useCaddyConfig() {
13667
13697
  function writeCaddyConfig(_proxies) {
13668
13698
  const proxies = configJsonRef() || {};
13669
13699
  _proxies.forEach((p) => {
13670
- const index = `${p.target}${p.portSuffix}`;
13671
- proxies[index] = p;
13700
+ let base = p.base || "/";
13701
+ if (!base.startsWith("/"))
13702
+ base = `/${base}`;
13703
+ if (base !== "/" && !base.endsWith("/"))
13704
+ base = `${base}/`;
13705
+ const index = `${p.target}${p.portSuffix}${base}`;
13706
+ proxies[index] = { ...p, base };
13672
13707
  });
13673
13708
  const content = JSON.stringify(proxies, null, 2);
13674
13709
  configContentRef(content);
@@ -13886,13 +13921,22 @@ var CaddyInstant = class {
13886
13921
  throw new Error("update host failed");
13887
13922
  consola.success("update host success\n");
13888
13923
  const targetHost = target.split(":")[0];
13924
+ const normalizeBase = (b) => {
13925
+ let out = b || "/";
13926
+ if (!out.startsWith("/"))
13927
+ out = `/${out}`;
13928
+ if (out !== "/" && !out.endsWith("/"))
13929
+ out = `${out}/`;
13930
+ return out;
13931
+ };
13932
+ const baseNormalized = normalizeBase(base);
13889
13933
  const proxies = [
13890
13934
  {
13891
13935
  target: targetHost,
13892
13936
  portSuffix: https ? "" : ":80",
13893
13937
  tls: https ? "tls internal" : "",
13894
13938
  source,
13895
- base,
13939
+ base: baseNormalized,
13896
13940
  healthCheck
13897
13941
  }
13898
13942
  ];
@@ -13902,14 +13946,12 @@ var CaddyInstant = class {
13902
13946
  effect(async () => {
13903
13947
  const { configJsonRef } = getCaddyConfig();
13904
13948
  const proxies2 = Object.values(configJsonRef() || {});
13905
- if (!(this.caddyfile || "").includes(`${target.split(":")[0]}${https ? "" : ":80"}`)) {
13906
- const ctx = validateTemplateContext({
13907
- caddy_root: this.caddyRoot,
13908
- proxies: proxies2
13909
- });
13910
- this.caddyfile = await genCaddyfile(ctx);
13911
- await this.writeCaddyfile();
13912
- }
13949
+ const ctx = validateTemplateContext({
13950
+ caddy_root: this.caddyRoot,
13951
+ proxies: proxies2
13952
+ });
13953
+ this.caddyfile = await genCaddyfile(ctx);
13954
+ await this.writeCaddyfile();
13913
13955
  })
13914
13956
  );
13915
13957
  if (!(0, import_node_fs3.existsSync)(caddyLockFilePath))
@@ -14137,7 +14179,7 @@ var unpluginFactory = (options) => ({
14137
14179
  }
14138
14180
  },
14139
14181
  webpack(compiler) {
14140
- var _a4, _b3;
14182
+ var _a4, _b3, _c2;
14141
14183
  if (import_node_process5.default.env.NODE_ENV !== "development")
14142
14184
  return;
14143
14185
  const {
@@ -14168,7 +14210,16 @@ var unpluginFactory = (options) => ({
14168
14210
  const source = `${devServer.host}:${devServer.port}`;
14169
14211
  if (caddy)
14170
14212
  return;
14171
- const _close = (_b3 = compiler.close) == null ? void 0 : _b3.bind(compiler);
14213
+ let base = "/";
14214
+ const publicPath = (_b3 = compiler.options.output) == null ? void 0 : _b3.publicPath;
14215
+ if (typeof publicPath === "string" && publicPath) {
14216
+ base = publicPath;
14217
+ }
14218
+ if (!base.startsWith("/"))
14219
+ base = `/${base}`;
14220
+ if (base !== "/" && !base.endsWith("/"))
14221
+ base = `${base}/`;
14222
+ const _close = (_c2 = compiler.close) == null ? void 0 : _c2.bind(compiler);
14172
14223
  if (_close) {
14173
14224
  compiler.close = async (callback) => {
14174
14225
  import_node_process5.default.on("exit", () => {
@@ -14180,18 +14231,19 @@ var unpluginFactory = (options) => ({
14180
14231
  }
14181
14232
  caddy = new CaddyInstant();
14182
14233
  caddy.run(source, target, {
14234
+ base,
14183
14235
  ...options
14184
14236
  }).then(() => {
14185
14237
  compiler.hooks.done.tap("unplugin-https-reverse-proxy", async () => {
14186
14238
  await printForwardProxy();
14187
- consola.success(` ${import_picocolors2.default.green("\u279C")} ${import_picocolors2.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}`)}`);
14239
+ consola.success(` ${import_picocolors2.default.green("\u279C")} ${import_picocolors2.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}${base}`)}`);
14188
14240
  });
14189
14241
  }).catch((e) => {
14190
14242
  throw e;
14191
14243
  });
14192
14244
  },
14193
14245
  async rspack(compiler) {
14194
- var _a4, _b3, _c2;
14246
+ var _a4, _b3, _c2, _d2;
14195
14247
  if (import_node_process5.default.env.NODE_ENV !== "development")
14196
14248
  return;
14197
14249
  const {
@@ -14224,11 +14276,21 @@ var unpluginFactory = (options) => ({
14224
14276
  if (caddy)
14225
14277
  return;
14226
14278
  caddy = new CaddyInstant();
14279
+ let base = "/";
14280
+ const publicPath = (_d2 = compiler.options.output) == null ? void 0 : _d2.publicPath;
14281
+ if (typeof publicPath === "string" && publicPath) {
14282
+ base = publicPath;
14283
+ }
14284
+ if (!base.startsWith("/"))
14285
+ base = `/${base}`;
14286
+ if (base !== "/" && !base.endsWith("/"))
14287
+ base = `${base}/`;
14227
14288
  caddy.run(source, target, {
14289
+ base,
14228
14290
  ...options
14229
14291
  }).then(async () => {
14230
14292
  await printForwardProxy();
14231
- consola.success(` ${import_picocolors2.default.green("\u279C")} ${import_picocolors2.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}`)}`);
14293
+ consola.success(` ${import_picocolors2.default.green("\u279C")} ${import_picocolors2.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}${base}`)}`);
14232
14294
  }).catch((e) => {
14233
14295
  throw e;
14234
14296
  });
package/dist/vite.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  unpluginFactory
3
- } from "./chunk-5MC5SB7O.js";
3
+ } from "./chunk-K7HSLE6X.js";
4
4
  import "./chunk-2P7SUCQ3.js";
5
5
 
6
6
  // src/vite.ts
package/dist/webpack.cjs CHANGED
@@ -13476,33 +13476,63 @@ var caddyTemplate = `{
13476
13476
  }
13477
13477
  }
13478
13478
 
13479
- {% for p in proxies %}
13480
- {{ p.target }}{{ p.portSuffix }} {
13481
- {{ p.tls }}
13482
- reverse_proxy http://{{ p.source }} {% if p.healthCheck %} {
13483
- health_uri {{ p.base }}
13484
- health_interval 2s
13485
- health_timeout 5s
13486
- fail_duration 2s
13487
- unhealthy_status 502 503 504 404
13488
- lb_try_duration 3s
13489
- lb_try_interval 300ms
13479
+ {% for host in hosts %}
13480
+ {{ host.host }}{{ host.portSuffix }} {
13481
+ {{ host.tls }}
13490
13482
 
13491
- @error status 502 503 504 404
13492
- handle_response @error {
13493
- respond * 503 {
13494
- body "Service Unavailable"
13495
- close
13483
+ {% for route in host.routes %}
13484
+ handle {{ route.base }}* {
13485
+ reverse_proxy http://{{ route.source }} {% if route.healthCheck %} {
13486
+ health_uri {{ route.base }}
13487
+ health_interval 2s
13488
+ health_timeout 5s
13489
+ health_headers {
13490
+ User-Agent {http.request.header.User-Agent}
13491
+ Accept text/html,application/json,*/*
13492
+ }
13493
+ fail_duration 2s
13494
+ unhealthy_status 502 503 504 404
13495
+ lb_try_duration 3s
13496
+ lb_try_interval 300ms
13497
+
13498
+ @error status 502 503 504 404
13499
+ handle_response @error {
13500
+ respond * 503 {
13501
+ body "Service Unavailable"
13502
+ close
13503
+ }
13496
13504
  }
13497
13505
  }
13506
+ {% endif %}
13498
13507
  }
13499
- {% endif %}
13508
+ {% endfor %}
13500
13509
  }
13501
13510
  {% endfor %}
13502
13511
  `;
13503
13512
  var liquid = new Liquid();
13504
13513
  async function genCaddyfile(ctx) {
13505
- return await liquid.parseAndRender(caddyTemplate, ctx);
13514
+ const hostsMap = /* @__PURE__ */ new Map();
13515
+ for (const proxy of ctx.proxies) {
13516
+ const key = `${proxy.target}${proxy.portSuffix}`;
13517
+ if (!hostsMap.has(key)) {
13518
+ hostsMap.set(key, {
13519
+ host: proxy.target,
13520
+ portSuffix: proxy.portSuffix,
13521
+ tls: proxy.tls,
13522
+ routes: []
13523
+ });
13524
+ }
13525
+ let base = proxy.base || "/";
13526
+ if (!base.startsWith("/"))
13527
+ base = `/${base}`;
13528
+ if (base !== "/" && !base.endsWith("/"))
13529
+ base = `${base}/`;
13530
+ hostsMap.get(key).routes.push({ ...proxy, base });
13531
+ }
13532
+ for (const host of hostsMap.values())
13533
+ host.routes.sort((a, b) => b.base.length - a.base.length);
13534
+ const hosts = Array.from(hostsMap.values());
13535
+ return await liquid.parseAndRender(caddyTemplate, { ...ctx, hosts });
13506
13536
  }
13507
13537
 
13508
13538
  // src/caddy/config.ts
@@ -13667,8 +13697,13 @@ function useCaddyConfig() {
13667
13697
  function writeCaddyConfig(_proxies) {
13668
13698
  const proxies = configJsonRef() || {};
13669
13699
  _proxies.forEach((p) => {
13670
- const index = `${p.target}${p.portSuffix}`;
13671
- proxies[index] = p;
13700
+ let base = p.base || "/";
13701
+ if (!base.startsWith("/"))
13702
+ base = `/${base}`;
13703
+ if (base !== "/" && !base.endsWith("/"))
13704
+ base = `${base}/`;
13705
+ const index = `${p.target}${p.portSuffix}${base}`;
13706
+ proxies[index] = { ...p, base };
13672
13707
  });
13673
13708
  const content = JSON.stringify(proxies, null, 2);
13674
13709
  configContentRef(content);
@@ -13886,13 +13921,22 @@ var CaddyInstant = class {
13886
13921
  throw new Error("update host failed");
13887
13922
  consola.success("update host success\n");
13888
13923
  const targetHost = target.split(":")[0];
13924
+ const normalizeBase = (b) => {
13925
+ let out = b || "/";
13926
+ if (!out.startsWith("/"))
13927
+ out = `/${out}`;
13928
+ if (out !== "/" && !out.endsWith("/"))
13929
+ out = `${out}/`;
13930
+ return out;
13931
+ };
13932
+ const baseNormalized = normalizeBase(base);
13889
13933
  const proxies = [
13890
13934
  {
13891
13935
  target: targetHost,
13892
13936
  portSuffix: https ? "" : ":80",
13893
13937
  tls: https ? "tls internal" : "",
13894
13938
  source,
13895
- base,
13939
+ base: baseNormalized,
13896
13940
  healthCheck
13897
13941
  }
13898
13942
  ];
@@ -13902,14 +13946,12 @@ var CaddyInstant = class {
13902
13946
  effect(async () => {
13903
13947
  const { configJsonRef } = getCaddyConfig();
13904
13948
  const proxies2 = Object.values(configJsonRef() || {});
13905
- if (!(this.caddyfile || "").includes(`${target.split(":")[0]}${https ? "" : ":80"}`)) {
13906
- const ctx = validateTemplateContext({
13907
- caddy_root: this.caddyRoot,
13908
- proxies: proxies2
13909
- });
13910
- this.caddyfile = await genCaddyfile(ctx);
13911
- await this.writeCaddyfile();
13912
- }
13949
+ const ctx = validateTemplateContext({
13950
+ caddy_root: this.caddyRoot,
13951
+ proxies: proxies2
13952
+ });
13953
+ this.caddyfile = await genCaddyfile(ctx);
13954
+ await this.writeCaddyfile();
13913
13955
  })
13914
13956
  );
13915
13957
  if (!(0, import_node_fs3.existsSync)(caddyLockFilePath))
@@ -14137,7 +14179,7 @@ var unpluginFactory = (options) => ({
14137
14179
  }
14138
14180
  },
14139
14181
  webpack(compiler) {
14140
- var _a4, _b3;
14182
+ var _a4, _b3, _c2;
14141
14183
  if (import_node_process5.default.env.NODE_ENV !== "development")
14142
14184
  return;
14143
14185
  const {
@@ -14168,7 +14210,16 @@ var unpluginFactory = (options) => ({
14168
14210
  const source = `${devServer.host}:${devServer.port}`;
14169
14211
  if (caddy)
14170
14212
  return;
14171
- const _close = (_b3 = compiler.close) == null ? void 0 : _b3.bind(compiler);
14213
+ let base = "/";
14214
+ const publicPath = (_b3 = compiler.options.output) == null ? void 0 : _b3.publicPath;
14215
+ if (typeof publicPath === "string" && publicPath) {
14216
+ base = publicPath;
14217
+ }
14218
+ if (!base.startsWith("/"))
14219
+ base = `/${base}`;
14220
+ if (base !== "/" && !base.endsWith("/"))
14221
+ base = `${base}/`;
14222
+ const _close = (_c2 = compiler.close) == null ? void 0 : _c2.bind(compiler);
14172
14223
  if (_close) {
14173
14224
  compiler.close = async (callback) => {
14174
14225
  import_node_process5.default.on("exit", () => {
@@ -14180,18 +14231,19 @@ var unpluginFactory = (options) => ({
14180
14231
  }
14181
14232
  caddy = new CaddyInstant();
14182
14233
  caddy.run(source, target, {
14234
+ base,
14183
14235
  ...options
14184
14236
  }).then(() => {
14185
14237
  compiler.hooks.done.tap("unplugin-https-reverse-proxy", async () => {
14186
14238
  await printForwardProxy();
14187
- consola.success(` ${import_picocolors2.default.green("\u279C")} ${import_picocolors2.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}`)}`);
14239
+ consola.success(` ${import_picocolors2.default.green("\u279C")} ${import_picocolors2.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}${base}`)}`);
14188
14240
  });
14189
14241
  }).catch((e) => {
14190
14242
  throw e;
14191
14243
  });
14192
14244
  },
14193
14245
  async rspack(compiler) {
14194
- var _a4, _b3, _c2;
14246
+ var _a4, _b3, _c2, _d2;
14195
14247
  if (import_node_process5.default.env.NODE_ENV !== "development")
14196
14248
  return;
14197
14249
  const {
@@ -14224,11 +14276,21 @@ var unpluginFactory = (options) => ({
14224
14276
  if (caddy)
14225
14277
  return;
14226
14278
  caddy = new CaddyInstant();
14279
+ let base = "/";
14280
+ const publicPath = (_d2 = compiler.options.output) == null ? void 0 : _d2.publicPath;
14281
+ if (typeof publicPath === "string" && publicPath) {
14282
+ base = publicPath;
14283
+ }
14284
+ if (!base.startsWith("/"))
14285
+ base = `/${base}`;
14286
+ if (base !== "/" && !base.endsWith("/"))
14287
+ base = `${base}/`;
14227
14288
  caddy.run(source, target, {
14289
+ base,
14228
14290
  ...options
14229
14291
  }).then(async () => {
14230
14292
  await printForwardProxy();
14231
- consola.success(` ${import_picocolors2.default.green("\u279C")} ${import_picocolors2.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}`)}`);
14293
+ consola.success(` ${import_picocolors2.default.green("\u279C")} ${import_picocolors2.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}${base}`)}`);
14232
14294
  }).catch((e) => {
14233
14295
  throw e;
14234
14296
  });
package/dist/webpack.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  unpluginFactory
3
- } from "./chunk-5MC5SB7O.js";
3
+ } from "./chunk-K7HSLE6X.js";
4
4
  import "./chunk-2P7SUCQ3.js";
5
5
 
6
6
  // src/webpack.ts
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "unplugin-https-reverse-proxy",
3
3
  "type": "module",
4
- "version": "3.0.0",
4
+ "version": "4.0.0",
5
5
  "description": "",
6
6
  "license": "MIT",
7
7
  "homepage": "https://github.com/zcf0508/unplugin-https-reverse-proxy#readme",
@@ -46,27 +46,36 @@ export const caddyTemplate = `{
46
46
  }
47
47
  }
48
48
 
49
- {% for p in proxies %}
50
- {{ p.target }}{{ p.portSuffix }} {
51
- {{ p.tls }}
52
- reverse_proxy http://{{ p.source }} {% if p.healthCheck %} {
53
- health_uri {{ p.base }}
54
- health_interval 2s
55
- health_timeout 5s
56
- fail_duration 2s
57
- unhealthy_status 502 503 504 404
58
- lb_try_duration 3s
59
- lb_try_interval 300ms
60
-
61
- @error status 502 503 504 404
62
- handle_response @error {
63
- respond * 503 {
64
- body "Service Unavailable"
65
- close
49
+ {% for host in hosts %}
50
+ {{ host.host }}{{ host.portSuffix }} {
51
+ {{ host.tls }}
52
+
53
+ {% for route in host.routes %}
54
+ handle {{ route.base }}* {
55
+ reverse_proxy http://{{ route.source }} {% if route.healthCheck %} {
56
+ health_uri {{ route.base }}
57
+ health_interval 2s
58
+ health_timeout 5s
59
+ health_headers {
60
+ User-Agent {http.request.header.User-Agent}
61
+ Accept text/html,application/json,*/*
62
+ }
63
+ fail_duration 2s
64
+ unhealthy_status 502 503 504 404
65
+ lb_try_duration 3s
66
+ lb_try_interval 300ms
67
+
68
+ @error status 502 503 504 404
69
+ handle_response @error {
70
+ respond * 503 {
71
+ body "Service Unavailable"
72
+ close
73
+ }
66
74
  }
67
75
  }
76
+ {% endif %}
68
77
  }
69
- {% endif %}
78
+ {% endfor %}
70
79
  }
71
80
  {% endfor %}
72
81
  `
@@ -74,5 +83,35 @@ export const caddyTemplate = `{
74
83
  const liquid = new Liquid()
75
84
 
76
85
  export async function genCaddyfile(ctx: typeof CaddyContext.infer): Promise<string> {
77
- return await liquid.parseAndRender(caddyTemplate, ctx)
86
+ const hostsMap = new Map<string, {
87
+ host: string
88
+ portSuffix: string
89
+ tls: string
90
+ routes: typeof ProxyType.infer[]
91
+ }>()
92
+
93
+ for (const proxy of ctx.proxies) {
94
+ const key = `${proxy.target}${proxy.portSuffix}`
95
+ if (!hostsMap.has(key)) {
96
+ hostsMap.set(key, {
97
+ host: proxy.target,
98
+ portSuffix: proxy.portSuffix,
99
+ tls: proxy.tls,
100
+ routes: [],
101
+ })
102
+ }
103
+ let base = proxy.base || '/'
104
+ if (!base.startsWith('/'))
105
+ base = `/${base}`
106
+ if (base !== '/' && !base.endsWith('/'))
107
+ base = `${base}/`
108
+ hostsMap.get(key)!.routes.push({ ...proxy, base })
109
+ }
110
+
111
+ for (const host of hostsMap.values())
112
+ host.routes.sort((a, b) => b.base.length - a.base.length)
113
+
114
+ const hosts = Array.from(hostsMap.values())
115
+
116
+ return await liquid.parseAndRender(caddyTemplate, { ...ctx, hosts })
78
117
  }
@@ -44,8 +44,13 @@ export function useCaddyConfig(): {
44
44
  const proxies = configJsonRef() || {}
45
45
 
46
46
  _proxies.forEach((p) => {
47
- const index = `${p.target}${p.portSuffix}`
48
- proxies[index] = p
47
+ let base = p.base || '/'
48
+ if (!base.startsWith('/'))
49
+ base = `/${base}`
50
+ if (base !== '/' && !base.endsWith('/'))
51
+ base = `${base}/`
52
+ const index = `${p.target}${p.portSuffix}${base}`
53
+ proxies[index] = { ...p, base }
49
54
  })
50
55
 
51
56
  const content = JSON.stringify(proxies, null, 2)
@@ -199,6 +199,15 @@ export class CaddyInstant {
199
199
  consola.success('update host success\n')
200
200
 
201
201
  const targetHost = target.split(':')[0]
202
+ const normalizeBase = (b: string): string => {
203
+ let out = b || '/'
204
+ if (!out.startsWith('/'))
205
+ out = `/${out}`
206
+ if (out !== '/' && !out.endsWith('/'))
207
+ out = `${out}/`
208
+ return out
209
+ }
210
+ const baseNormalized = normalizeBase(base)
202
211
 
203
212
  const proxies = [
204
213
  {
@@ -206,7 +215,7 @@ export class CaddyInstant {
206
215
  portSuffix: https ? '' : ':80',
207
216
  tls: https ? 'tls internal' : '',
208
217
  source,
209
- base,
218
+ base: baseNormalized,
210
219
  healthCheck,
211
220
  },
212
221
  ]
@@ -218,15 +227,13 @@ export class CaddyInstant {
218
227
  effect(async () => {
219
228
  const { configJsonRef } = getCaddyConfig()
220
229
  const proxies = Object.values(configJsonRef() || {})
221
- if (!(this.caddyfile || '').includes(`${target.split(':')[0]}${https ? '' : ':80'}`)) {
222
- const ctx = validateTemplateContext({
223
- caddy_root: this.caddyRoot,
224
- proxies,
225
- })
226
-
227
- this.caddyfile = await genCaddyfile(ctx)
228
- await this.writeCaddyfile()
229
- }
230
+ const ctx = validateTemplateContext({
231
+ caddy_root: this.caddyRoot,
232
+ proxies,
233
+ })
234
+
235
+ this.caddyfile = await genCaddyfile(ctx)
236
+ await this.writeCaddyfile()
230
237
  }),
231
238
  )
232
239
 
package/src/index.ts CHANGED
@@ -160,6 +160,16 @@ export const unpluginFactory: UnpluginFactory<Options> = options => ({
160
160
  if (caddy)
161
161
  return
162
162
 
163
+ let base = '/'
164
+ const publicPath = compiler.options.output?.publicPath as unknown
165
+ if (typeof publicPath === 'string' && publicPath) {
166
+ base = publicPath
167
+ }
168
+ if (!base.startsWith('/'))
169
+ base = `/${base}`
170
+ if (base !== '/' && !base.endsWith('/'))
171
+ base = `${base}/`
172
+
163
173
  // https://github.com/webpack/webpack-dev-server/blob/master/lib/Server.js#L1913
164
174
  const _close = compiler.close?.bind(compiler)
165
175
  if (_close) {
@@ -177,12 +187,13 @@ export const unpluginFactory: UnpluginFactory<Options> = options => ({
177
187
  caddy = new CaddyInstant()
178
188
 
179
189
  caddy.run(source, target, {
190
+ base,
180
191
  ...options,
181
192
  }).then(() => {
182
193
  compiler.hooks.done.tap('unplugin-https-reverse-proxy', async () => {
183
194
  await printForwardProxy()
184
195
 
185
- consola.success(` ${c.green('➜')} ${c.bold('run caddy reverse proxy success')}: ${colorUrl(`${options.https ? 'https' : 'http'}://${target}`)}`)
196
+ consola.success(` ${c.green('➜')} ${c.bold('run caddy reverse proxy success')}: ${colorUrl(`${options.https ? 'https' : 'http'}://${target}${base}`)}`)
186
197
  })
187
198
  }).catch((e) => {
188
199
  throw e
@@ -231,12 +242,23 @@ export const unpluginFactory: UnpluginFactory<Options> = options => ({
231
242
 
232
243
  caddy = new CaddyInstant()
233
244
 
245
+ let base = '/'
246
+ const publicPath = compiler.options.output?.publicPath as unknown
247
+ if (typeof publicPath === 'string' && publicPath) {
248
+ base = publicPath
249
+ }
250
+ if (!base.startsWith('/'))
251
+ base = `/${base}`
252
+ if (base !== '/' && !base.endsWith('/'))
253
+ base = `${base}/`
254
+
234
255
  caddy.run(source, target, {
256
+ base,
235
257
  ...options,
236
258
  }).then(async () => {
237
259
  await printForwardProxy()
238
260
 
239
- consola.success(` ${c.green('➜')} ${c.bold('run caddy reverse proxy success')}: ${colorUrl(`${options.https ? 'https' : 'http'}://${target}`)}`)
261
+ consola.success(` ${c.green('➜')} ${c.bold('run caddy reverse proxy success')}: ${colorUrl(`${options.https ? 'https' : 'http'}://${target}${base}`)}`)
240
262
  }).catch((e) => {
241
263
  throw e
242
264
  })