vite-plugin-phpsandbox 0.3.0 → 0.4.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
@@ -49,6 +49,8 @@ The plugin reads these environment variables by default:
49
49
 
50
50
  When `PHPSANDBOX_TYPE=laravel`, the plugin does not create a standalone HMR server. It sets `server.port` to the resolved HMR port and configures the advertised HMR endpoint instead.
51
51
 
52
+ When a public host is available, Laravel mode also defaults `server.origin` to its HTTPS URL so development assets use the public proxy rather than localhost. An explicitly configured origin is preserved. Other sandbox types retain their existing asset origin because their standalone server handles HMR only.
53
+
52
54
  ## API
53
55
 
54
56
  ```ts
package/dist/index.cjs CHANGED
@@ -52,8 +52,17 @@ function normalizeSandboxType(value) {
52
52
  const normalized = value?.trim().toLowerCase();
53
53
  return normalized ? normalized : void 0;
54
54
  }
55
- function resolvePublicHost(env) {
56
- return env.SUBDOMAIN && env.NOTEBOOK_HOST ? `${env.SUBDOMAIN}-${HMR_PORT}.${env.NOTEBOOK_HOST}` : void 0;
55
+ function resolvePublicHost(env, hmrPort) {
56
+ return env.SUBDOMAIN && env.NOTEBOOK_HOST ? `${env.SUBDOMAIN}-${hmrPort}.${env.NOTEBOOK_HOST}` : void 0;
57
+ }
58
+ function resolveHmrPort(existingServer, env) {
59
+ const existingHmr = resolveExistingHmrOptions(existingServer);
60
+ const value = env.PHPSANDBOX_HMR_PORT ?? env.VITE_HMR_PORT ?? existingHmr?.port;
61
+ const port = Number(value);
62
+ return Number.isInteger(port) && port > 0 ? port : HMR_PORT;
63
+ }
64
+ function shouldSkipHmrSetup(configEnv) {
65
+ return configEnv.command !== "serve" || process.argv.some((arg) => arg === "optimize");
57
66
  }
58
67
  function isRecord(value) {
59
68
  return typeof value === "object" && value !== null;
@@ -136,11 +145,11 @@ function resolveAllowedHosts(existingAllowedHosts, notebookHost, publicHost) {
136
145
  function resolveExistingHmrOptions(serverOptions) {
137
146
  return isRecord(serverOptions.hmr) ? serverOptions.hmr : void 0;
138
147
  }
139
- function resolveHmrOptions(existingServer, publicHost, hmrServer) {
148
+ function resolveHmrOptions(existingServer, publicHost, hmrServer, hmrPort) {
140
149
  if (existingServer.hmr === false) return false;
141
150
  return {
142
151
  ...resolveExistingHmrOptions(existingServer),
143
- port: HMR_PORT,
152
+ port: hmrPort,
144
153
  ...hmrServer ? { server: hmrServer } : {},
145
154
  ...publicHost ? {
146
155
  protocol: "wss",
@@ -152,12 +161,13 @@ function resolveHmrOptions(existingServer, publicHost, hmrServer) {
152
161
  function shouldCreateHmrServer(existingServer, isLaravel) {
153
162
  return !isLaravel && existingServer.hmr !== false;
154
163
  }
155
- function resolveServerConfig(existingServer, env, isLaravel, publicHost, hmrServer) {
164
+ function resolveServerConfig(existingServer, env, isLaravel, publicHost, hmrServer, hmrPort) {
156
165
  return {
157
166
  ...existingServer,
158
167
  host: existingServer.host ?? true,
159
- ...isLaravel ? { port: HMR_PORT } : {},
160
- hmr: resolveHmrOptions(existingServer, publicHost, hmrServer),
168
+ ...isLaravel ? { port: hmrPort } : {},
169
+ ...isLaravel && publicHost ? { origin: existingServer.origin ?? `https://${publicHost}` } : {},
170
+ hmr: resolveHmrOptions(existingServer, publicHost, hmrServer, hmrPort),
161
171
  allowedHosts: resolveAllowedHosts(
162
172
  existingServer.allowedHosts,
163
173
  env.NOTEBOOK_HOST,
@@ -184,7 +194,8 @@ function formatHmrEndpoint(hmr) {
184
194
  function formatHmrSummary(serverOptions, hasManagedServer) {
185
195
  if (serverOptions.hmr === false) return "disabled";
186
196
  const hmr = resolveExistingHmrOptions(serverOptions);
187
- const localEndpoint = hasManagedServer ? `0.0.0.0:${HMR_PORT}` : `vite:${serverOptions.port ?? HMR_PORT}`;
197
+ const port = hmr?.port ?? serverOptions.port ?? HMR_PORT;
198
+ const localEndpoint = hasManagedServer ? `0.0.0.0:${port}` : `vite:${port}`;
188
199
  return `${localEndpoint}, public=${formatHmrEndpoint(hmr)}`;
189
200
  }
190
201
  function formatStartupLog(sandboxType, serverOptions, hasManagedServer) {
@@ -201,6 +212,7 @@ function phpSandboxPlugin() {
201
212
  let releaseManagedHmrServer;
202
213
  let sandboxType;
203
214
  let needsHmrServer = false;
215
+ let hmrPort = HMR_PORT;
204
216
  let startupLog;
205
217
  function setManagedHmrServer(server) {
206
218
  if (managedHmrServer === server) return;
@@ -210,6 +222,7 @@ function phpSandboxPlugin() {
210
222
  releaseManagedHmrServer = void 0;
211
223
  return;
212
224
  }
225
+ const port = hmrPort;
213
226
  let released = false;
214
227
  releaseManagedHmrServer = () => {
215
228
  if (released) return;
@@ -218,32 +231,47 @@ function phpSandboxPlugin() {
218
231
  managedHmrServer = void 0;
219
232
  releaseManagedHmrServer = void 0;
220
233
  }
221
- releaseServer(getGlobalState(), HMR_PORT, server);
234
+ releaseServer(getGlobalState(), port, server);
222
235
  };
223
236
  }
224
237
  return {
225
238
  name: "vite-plugin-phpsandbox",
226
239
  apply: "serve",
227
240
  enforce: "post",
228
- config(userConfig) {
241
+ async config(userConfig, configEnv) {
229
242
  const env = process.env;
243
+ if (shouldSkipHmrSetup(configEnv)) {
244
+ sandboxType = void 0;
245
+ needsHmrServer = false;
246
+ startupLog = void 0;
247
+ setManagedHmrServer(void 0);
248
+ return {};
249
+ }
230
250
  sandboxType = normalizeSandboxType(env.PHPSANDBOX_TYPE);
231
251
  if (!sandboxType) {
232
252
  needsHmrServer = false;
233
253
  startupLog = void 0;
254
+ setManagedHmrServer(void 0);
234
255
  return {};
235
256
  }
236
257
  const isLaravel = sandboxType === "laravel";
237
- const publicHost = resolvePublicHost(env);
238
258
  const existingServer = userConfig.server ?? {};
259
+ hmrPort = resolveHmrPort(existingServer, env);
260
+ const publicHost = resolvePublicHost(env, hmrPort);
239
261
  needsHmrServer = shouldCreateHmrServer(existingServer, isLaravel);
262
+ let hmrServer;
263
+ if (needsHmrServer) {
264
+ hmrServer = managedHmrServer ?? await acquireServer(getGlobalState(), hmrPort);
265
+ setManagedHmrServer(hmrServer);
266
+ }
240
267
  return {
241
268
  server: resolveServerConfig(
242
269
  existingServer,
243
270
  env,
244
271
  isLaravel,
245
272
  publicHost,
246
- void 0
273
+ hmrServer,
274
+ hmrPort
247
275
  )
248
276
  };
249
277
  },
@@ -258,16 +286,8 @@ function phpSandboxPlugin() {
258
286
  startupLog = nextStartupLog;
259
287
  console.info(nextStartupLog);
260
288
  },
261
- async configureServer(server) {
289
+ configureServer(server) {
262
290
  if (!sandboxType) return;
263
- if (needsHmrServer) {
264
- const hmrServer = managedHmrServer ?? await acquireServer(getGlobalState(), HMR_PORT);
265
- setManagedHmrServer(hmrServer);
266
- const hmrConfig = server.config.server.hmr;
267
- if (isRecord(hmrConfig)) {
268
- hmrConfig.server = hmrServer;
269
- }
270
- }
271
291
  const release = releaseManagedHmrServer;
272
292
  if (!release) return;
273
293
  server.httpServer?.once("close", release);
package/dist/index.js CHANGED
@@ -16,8 +16,17 @@ function normalizeSandboxType(value) {
16
16
  const normalized = value?.trim().toLowerCase();
17
17
  return normalized ? normalized : void 0;
18
18
  }
19
- function resolvePublicHost(env) {
20
- return env.SUBDOMAIN && env.NOTEBOOK_HOST ? `${env.SUBDOMAIN}-${HMR_PORT}.${env.NOTEBOOK_HOST}` : void 0;
19
+ function resolvePublicHost(env, hmrPort) {
20
+ return env.SUBDOMAIN && env.NOTEBOOK_HOST ? `${env.SUBDOMAIN}-${hmrPort}.${env.NOTEBOOK_HOST}` : void 0;
21
+ }
22
+ function resolveHmrPort(existingServer, env) {
23
+ const existingHmr = resolveExistingHmrOptions(existingServer);
24
+ const value = env.PHPSANDBOX_HMR_PORT ?? env.VITE_HMR_PORT ?? existingHmr?.port;
25
+ const port = Number(value);
26
+ return Number.isInteger(port) && port > 0 ? port : HMR_PORT;
27
+ }
28
+ function shouldSkipHmrSetup(configEnv) {
29
+ return configEnv.command !== "serve" || process.argv.some((arg) => arg === "optimize");
21
30
  }
22
31
  function isRecord(value) {
23
32
  return typeof value === "object" && value !== null;
@@ -100,11 +109,11 @@ function resolveAllowedHosts(existingAllowedHosts, notebookHost, publicHost) {
100
109
  function resolveExistingHmrOptions(serverOptions) {
101
110
  return isRecord(serverOptions.hmr) ? serverOptions.hmr : void 0;
102
111
  }
103
- function resolveHmrOptions(existingServer, publicHost, hmrServer) {
112
+ function resolveHmrOptions(existingServer, publicHost, hmrServer, hmrPort) {
104
113
  if (existingServer.hmr === false) return false;
105
114
  return {
106
115
  ...resolveExistingHmrOptions(existingServer),
107
- port: HMR_PORT,
116
+ port: hmrPort,
108
117
  ...hmrServer ? { server: hmrServer } : {},
109
118
  ...publicHost ? {
110
119
  protocol: "wss",
@@ -116,12 +125,13 @@ function resolveHmrOptions(existingServer, publicHost, hmrServer) {
116
125
  function shouldCreateHmrServer(existingServer, isLaravel) {
117
126
  return !isLaravel && existingServer.hmr !== false;
118
127
  }
119
- function resolveServerConfig(existingServer, env, isLaravel, publicHost, hmrServer) {
128
+ function resolveServerConfig(existingServer, env, isLaravel, publicHost, hmrServer, hmrPort) {
120
129
  return {
121
130
  ...existingServer,
122
131
  host: existingServer.host ?? true,
123
- ...isLaravel ? { port: HMR_PORT } : {},
124
- hmr: resolveHmrOptions(existingServer, publicHost, hmrServer),
132
+ ...isLaravel ? { port: hmrPort } : {},
133
+ ...isLaravel && publicHost ? { origin: existingServer.origin ?? `https://${publicHost}` } : {},
134
+ hmr: resolveHmrOptions(existingServer, publicHost, hmrServer, hmrPort),
125
135
  allowedHosts: resolveAllowedHosts(
126
136
  existingServer.allowedHosts,
127
137
  env.NOTEBOOK_HOST,
@@ -148,7 +158,8 @@ function formatHmrEndpoint(hmr) {
148
158
  function formatHmrSummary(serverOptions, hasManagedServer) {
149
159
  if (serverOptions.hmr === false) return "disabled";
150
160
  const hmr = resolveExistingHmrOptions(serverOptions);
151
- const localEndpoint = hasManagedServer ? `0.0.0.0:${HMR_PORT}` : `vite:${serverOptions.port ?? HMR_PORT}`;
161
+ const port = hmr?.port ?? serverOptions.port ?? HMR_PORT;
162
+ const localEndpoint = hasManagedServer ? `0.0.0.0:${port}` : `vite:${port}`;
152
163
  return `${localEndpoint}, public=${formatHmrEndpoint(hmr)}`;
153
164
  }
154
165
  function formatStartupLog(sandboxType, serverOptions, hasManagedServer) {
@@ -165,6 +176,7 @@ function phpSandboxPlugin() {
165
176
  let releaseManagedHmrServer;
166
177
  let sandboxType;
167
178
  let needsHmrServer = false;
179
+ let hmrPort = HMR_PORT;
168
180
  let startupLog;
169
181
  function setManagedHmrServer(server) {
170
182
  if (managedHmrServer === server) return;
@@ -174,6 +186,7 @@ function phpSandboxPlugin() {
174
186
  releaseManagedHmrServer = void 0;
175
187
  return;
176
188
  }
189
+ const port = hmrPort;
177
190
  let released = false;
178
191
  releaseManagedHmrServer = () => {
179
192
  if (released) return;
@@ -182,32 +195,47 @@ function phpSandboxPlugin() {
182
195
  managedHmrServer = void 0;
183
196
  releaseManagedHmrServer = void 0;
184
197
  }
185
- releaseServer(getGlobalState(), HMR_PORT, server);
198
+ releaseServer(getGlobalState(), port, server);
186
199
  };
187
200
  }
188
201
  return {
189
202
  name: "vite-plugin-phpsandbox",
190
203
  apply: "serve",
191
204
  enforce: "post",
192
- config(userConfig) {
205
+ async config(userConfig, configEnv) {
193
206
  const env = process.env;
207
+ if (shouldSkipHmrSetup(configEnv)) {
208
+ sandboxType = void 0;
209
+ needsHmrServer = false;
210
+ startupLog = void 0;
211
+ setManagedHmrServer(void 0);
212
+ return {};
213
+ }
194
214
  sandboxType = normalizeSandboxType(env.PHPSANDBOX_TYPE);
195
215
  if (!sandboxType) {
196
216
  needsHmrServer = false;
197
217
  startupLog = void 0;
218
+ setManagedHmrServer(void 0);
198
219
  return {};
199
220
  }
200
221
  const isLaravel = sandboxType === "laravel";
201
- const publicHost = resolvePublicHost(env);
202
222
  const existingServer = userConfig.server ?? {};
223
+ hmrPort = resolveHmrPort(existingServer, env);
224
+ const publicHost = resolvePublicHost(env, hmrPort);
203
225
  needsHmrServer = shouldCreateHmrServer(existingServer, isLaravel);
226
+ let hmrServer;
227
+ if (needsHmrServer) {
228
+ hmrServer = managedHmrServer ?? await acquireServer(getGlobalState(), hmrPort);
229
+ setManagedHmrServer(hmrServer);
230
+ }
204
231
  return {
205
232
  server: resolveServerConfig(
206
233
  existingServer,
207
234
  env,
208
235
  isLaravel,
209
236
  publicHost,
210
- void 0
237
+ hmrServer,
238
+ hmrPort
211
239
  )
212
240
  };
213
241
  },
@@ -222,16 +250,8 @@ function phpSandboxPlugin() {
222
250
  startupLog = nextStartupLog;
223
251
  console.info(nextStartupLog);
224
252
  },
225
- async configureServer(server) {
253
+ configureServer(server) {
226
254
  if (!sandboxType) return;
227
- if (needsHmrServer) {
228
- const hmrServer = managedHmrServer ?? await acquireServer(getGlobalState(), HMR_PORT);
229
- setManagedHmrServer(hmrServer);
230
- const hmrConfig = server.config.server.hmr;
231
- if (isRecord(hmrConfig)) {
232
- hmrConfig.server = hmrServer;
233
- }
234
- }
235
255
  const release = releaseManagedHmrServer;
236
256
  if (!release) return;
237
257
  server.httpServer?.once("close", release);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-phpsandbox",
3
- "version": "0.3.0",
3
+ "version": "0.4.1",
4
4
  "description": "Vite plugin for PHP sandbox environments that need stable HMR over public proxy hosts.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -36,7 +36,8 @@
36
36
  "scripts": {
37
37
  "build": "tsup src/index.ts --format esm,cjs --dts",
38
38
  "check": "tsc --noEmit",
39
- "release:check": "npm run check && npm run build && npm pack --dry-run",
39
+ "test": "npm run build && node --test tests/*.test.mjs",
40
+ "release:check": "npm run check && npm run test && npm pack --dry-run",
40
41
  "release": "npm version minor && npm run release:check && npm publish",
41
42
  "prepublishOnly": "npm run build && npm run check"
42
43
  },