@inspecto-dev/plugin 0.3.9 → 0.3.10

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/index.js CHANGED
@@ -1861,6 +1861,24 @@ function resolveProjectRoot() {
1861
1861
  return gitRoot;
1862
1862
  }
1863
1863
 
1864
+ // src/server/server-url.ts
1865
+ function resolveServerHost(cwd, configRoot) {
1866
+ const userConfig = loadUserConfigSync(false, cwd, configRoot);
1867
+ const configuredHost = userConfig["server.host"]?.trim();
1868
+ if (configuredHost) return configuredHost;
1869
+ if (process.env["VITEST"]) return "127.0.0.1";
1870
+ return "127.0.0.1";
1871
+ }
1872
+ function resolvePublicServerUrl(args) {
1873
+ const userConfig = loadUserConfigSync(false, args.cwd, args.configRoot);
1874
+ const configuredPublicUrl = userConfig["server.publicUrl"]?.trim();
1875
+ if (configuredPublicUrl) {
1876
+ return configuredPublicUrl.replace(/\/$/, "");
1877
+ }
1878
+ const host = resolveServerHost(args.cwd, args.configRoot);
1879
+ return `http://${host}:${args.port}`;
1880
+ }
1881
+
1864
1882
  // src/server/index.ts
1865
1883
  var serverLogger4 = createLogger("inspecto:server", { logLevel: getGlobalLogLevel() });
1866
1884
  var PORT_FILE_NAME = "inspecto.port.json";
@@ -1915,6 +1933,7 @@ async function startServer() {
1915
1933
  serverState.projectRoot = resolveProjectRoot();
1916
1934
  serverState.configRoot = serverState.projectRoot;
1917
1935
  serverState.cwd = process.cwd();
1936
+ const serverHost = resolveServerHost(serverState.cwd, serverState.configRoot);
1918
1937
  portfinder.basePort = 5678;
1919
1938
  const port = await portfinder.getPortPromise();
1920
1939
  watchConfig(
@@ -1941,7 +1960,7 @@ async function startServer() {
1941
1960
  });
1942
1961
  });
1943
1962
  await new Promise((resolve2, reject) => {
1944
- serverInstance.listen(port, "0.0.0.0", () => {
1963
+ serverInstance.listen(port, serverHost, () => {
1945
1964
  serverInstance.unref();
1946
1965
  resolve2();
1947
1966
  });
@@ -1963,7 +1982,7 @@ async function startServer() {
1963
1982
  } catch {
1964
1983
  }
1965
1984
  });
1966
- serverLogger4.info(`server running at http://0.0.0.0:${port}`);
1985
+ serverLogger4.info(`server running at http://${serverHost}:${port}`);
1967
1986
  return port;
1968
1987
  }
1969
1988
  async function readBody(req) {
@@ -2380,26 +2399,28 @@ var resolveClientModule = () => {
2380
2399
  };
2381
2400
 
2382
2401
  // src/injectors/webpack.ts
2383
- function getWebpackHtmlScript(serverPort) {
2402
+ function getWebpackHtmlScript(serverPort, publicServerUrl) {
2384
2403
  return `
2385
2404
  window.__AI_INSPECTOR_PORT__ = ${serverPort};
2405
+ window.__AI_INSPECTOR_SERVER_URL__ = '${publicServerUrl ?? `http://127.0.0.1:${serverPort}`}';
2386
2406
  window.addEventListener('load', () => {
2387
2407
  if (window.InspectoClient) {
2388
2408
  window.InspectoClient.mountInspector({
2389
- serverUrl: 'http://0.0.0.0:' + window.__AI_INSPECTOR_PORT__,
2409
+ serverUrl: window.__AI_INSPECTOR_SERVER_URL__,
2390
2410
  });
2391
2411
  }
2392
2412
  });
2393
2413
  `;
2394
2414
  }
2395
- function getWebpackAssetScript(serverPort) {
2415
+ function getWebpackAssetScript(serverPort, publicServerUrl) {
2396
2416
  return `
2397
2417
  if (typeof window !== 'undefined') {
2398
2418
  window.__AI_INSPECTOR_PORT__ = ${serverPort};
2419
+ window.__AI_INSPECTOR_SERVER_URL__ = '${publicServerUrl ?? `http://127.0.0.1:${serverPort}`}';
2399
2420
  const _initInspecto = () => {
2400
2421
  if (window.InspectoClient) {
2401
2422
  window.InspectoClient.mountInspector({
2402
- serverUrl: 'http://0.0.0.0:' + window.__AI_INSPECTOR_PORT__,
2423
+ serverUrl: window.__AI_INSPECTOR_SERVER_URL__,
2403
2424
  });
2404
2425
  } else {
2405
2426
  setTimeout(_initInspecto, 100);
@@ -2413,7 +2434,7 @@ if (typeof window !== 'undefined') {
2413
2434
  }
2414
2435
  `;
2415
2436
  }
2416
- function injectWebpack(compiler, serverPortFn, resolveClientModule2) {
2437
+ function injectWebpack(compiler, serverPortFn, publicServerUrlFn, resolveClientModule2) {
2417
2438
  const inspectoClientPath = resolveClientModule2();
2418
2439
  if (compiler.webpack && compiler.webpack.EntryPlugin) {
2419
2440
  new compiler.webpack.EntryPlugin(compiler.context, inspectoClientPath, {
@@ -2428,11 +2449,12 @@ function injectWebpack(compiler, serverPortFn, resolveClientModule2) {
2428
2449
  const hooks = HtmlWebpackPlugin.constructor.getHooks(compilation);
2429
2450
  hooks.alterAssetTagGroups.tapPromise("inspecto-overlay", async (data) => {
2430
2451
  const port = await serverPortFn();
2452
+ const publicServerUrl = publicServerUrlFn(port);
2431
2453
  data.headTags.unshift({
2432
2454
  tagName: "script",
2433
2455
  voidTag: false,
2434
2456
  meta: { plugin: "inspecto-overlay" },
2435
- innerHTML: getWebpackHtmlScript(port)
2457
+ innerHTML: getWebpackHtmlScript(port, publicServerUrl)
2436
2458
  });
2437
2459
  return data;
2438
2460
  });
@@ -2445,13 +2467,14 @@ function injectWebpack(compiler, serverPortFn, resolveClientModule2) {
2445
2467
  },
2446
2468
  async (assets) => {
2447
2469
  const port = await serverPortFn();
2470
+ const publicServerUrl = publicServerUrlFn(port);
2448
2471
  const mainAssetKey = Object.keys(assets).find(
2449
2472
  (key) => key.endsWith(".js") && (key.includes("main") || key.includes("app") || key.includes("umi"))
2450
2473
  );
2451
2474
  if (!mainAssetKey) return;
2452
2475
  const originalSource = assets[mainAssetKey].source();
2453
2476
  assets[mainAssetKey] = new compiler.webpack.sources.RawSource(
2454
- getWebpackAssetScript(port) + "\n" + originalSource
2477
+ getWebpackAssetScript(port, publicServerUrl) + "\n" + originalSource
2455
2478
  );
2456
2479
  }
2457
2480
  );
@@ -2485,12 +2508,13 @@ function injectRspack(compiler, serverPortFn, resolveClientModule2) {
2485
2508
  }
2486
2509
 
2487
2510
  // src/injectors/vite.ts
2488
- function getViteVirtualModuleScript(serverPort) {
2511
+ function getViteVirtualModuleScript(serverPort, publicServerUrl) {
2489
2512
  return `
2490
2513
  import { mountInspector } from '@inspecto-dev/core';
2491
2514
  window.__AI_INSPECTOR_PORT__ = ${serverPort};
2515
+ window.__AI_INSPECTOR_SERVER_URL__ = '${publicServerUrl ?? `http://127.0.0.1:${serverPort}`}';
2492
2516
  mountInspector({
2493
- serverUrl: 'http://0.0.0.0:' + window.__AI_INSPECTOR_PORT__,
2517
+ serverUrl: window.__AI_INSPECTOR_SERVER_URL__,
2494
2518
  });
2495
2519
  `;
2496
2520
  }
@@ -2523,6 +2547,11 @@ var InspectoPlugin = createUnplugin((userOptions = {}) => {
2523
2547
  }
2524
2548
  return serverPort;
2525
2549
  };
2550
+ const getPublicServerUrl = (port) => resolvePublicServerUrl({
2551
+ cwd: serverState.cwd || process.cwd(),
2552
+ configRoot: serverState.configRoot || projectRoot,
2553
+ port
2554
+ });
2526
2555
  return {
2527
2556
  name: "inspecto-overlay",
2528
2557
  enforce: "pre",
@@ -2535,7 +2564,7 @@ var InspectoPlugin = createUnplugin((userOptions = {}) => {
2535
2564
  },
2536
2565
  webpack: (compiler) => {
2537
2566
  if (isProduction) return;
2538
- injectWebpack(compiler, ensureServer, resolveClientModule);
2567
+ injectWebpack(compiler, ensureServer, getPublicServerUrl, resolveClientModule);
2539
2568
  },
2540
2569
  rspack: (compiler) => {
2541
2570
  if (isProduction) return;
@@ -2561,7 +2590,10 @@ var InspectoPlugin = createUnplugin((userOptions = {}) => {
2561
2590
  },
2562
2591
  load(id) {
2563
2592
  if (id === VITE_VIRTUAL_MODULE_ID) {
2564
- return getViteVirtualModuleScript(serverPort ?? DEFAULT_PORT);
2593
+ return getViteVirtualModuleScript(
2594
+ serverPort ?? DEFAULT_PORT,
2595
+ getPublicServerUrl(serverPort ?? DEFAULT_PORT)
2596
+ );
2565
2597
  }
2566
2598
  return null;
2567
2599
  },