vite-plugin-phpsandbox 0.3.0 → 0.4.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/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,12 @@ 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
+ hmr: resolveHmrOptions(existingServer, publicHost, hmrServer, hmrPort),
161
170
  allowedHosts: resolveAllowedHosts(
162
171
  existingServer.allowedHosts,
163
172
  env.NOTEBOOK_HOST,
@@ -184,7 +193,8 @@ function formatHmrEndpoint(hmr) {
184
193
  function formatHmrSummary(serverOptions, hasManagedServer) {
185
194
  if (serverOptions.hmr === false) return "disabled";
186
195
  const hmr = resolveExistingHmrOptions(serverOptions);
187
- const localEndpoint = hasManagedServer ? `0.0.0.0:${HMR_PORT}` : `vite:${serverOptions.port ?? HMR_PORT}`;
196
+ const port = hmr?.port ?? serverOptions.port ?? HMR_PORT;
197
+ const localEndpoint = hasManagedServer ? `0.0.0.0:${port}` : `vite:${port}`;
188
198
  return `${localEndpoint}, public=${formatHmrEndpoint(hmr)}`;
189
199
  }
190
200
  function formatStartupLog(sandboxType, serverOptions, hasManagedServer) {
@@ -201,6 +211,7 @@ function phpSandboxPlugin() {
201
211
  let releaseManagedHmrServer;
202
212
  let sandboxType;
203
213
  let needsHmrServer = false;
214
+ let hmrPort = HMR_PORT;
204
215
  let startupLog;
205
216
  function setManagedHmrServer(server) {
206
217
  if (managedHmrServer === server) return;
@@ -210,6 +221,7 @@ function phpSandboxPlugin() {
210
221
  releaseManagedHmrServer = void 0;
211
222
  return;
212
223
  }
224
+ const port = hmrPort;
213
225
  let released = false;
214
226
  releaseManagedHmrServer = () => {
215
227
  if (released) return;
@@ -218,32 +230,47 @@ function phpSandboxPlugin() {
218
230
  managedHmrServer = void 0;
219
231
  releaseManagedHmrServer = void 0;
220
232
  }
221
- releaseServer(getGlobalState(), HMR_PORT, server);
233
+ releaseServer(getGlobalState(), port, server);
222
234
  };
223
235
  }
224
236
  return {
225
237
  name: "vite-plugin-phpsandbox",
226
238
  apply: "serve",
227
239
  enforce: "post",
228
- config(userConfig) {
240
+ async config(userConfig, configEnv) {
229
241
  const env = process.env;
242
+ if (shouldSkipHmrSetup(configEnv)) {
243
+ sandboxType = void 0;
244
+ needsHmrServer = false;
245
+ startupLog = void 0;
246
+ setManagedHmrServer(void 0);
247
+ return {};
248
+ }
230
249
  sandboxType = normalizeSandboxType(env.PHPSANDBOX_TYPE);
231
250
  if (!sandboxType) {
232
251
  needsHmrServer = false;
233
252
  startupLog = void 0;
253
+ setManagedHmrServer(void 0);
234
254
  return {};
235
255
  }
236
256
  const isLaravel = sandboxType === "laravel";
237
- const publicHost = resolvePublicHost(env);
238
257
  const existingServer = userConfig.server ?? {};
258
+ hmrPort = resolveHmrPort(existingServer, env);
259
+ const publicHost = resolvePublicHost(env, hmrPort);
239
260
  needsHmrServer = shouldCreateHmrServer(existingServer, isLaravel);
261
+ let hmrServer;
262
+ if (needsHmrServer) {
263
+ hmrServer = managedHmrServer ?? await acquireServer(getGlobalState(), hmrPort);
264
+ setManagedHmrServer(hmrServer);
265
+ }
240
266
  return {
241
267
  server: resolveServerConfig(
242
268
  existingServer,
243
269
  env,
244
270
  isLaravel,
245
271
  publicHost,
246
- void 0
272
+ hmrServer,
273
+ hmrPort
247
274
  )
248
275
  };
249
276
  },
@@ -258,16 +285,8 @@ function phpSandboxPlugin() {
258
285
  startupLog = nextStartupLog;
259
286
  console.info(nextStartupLog);
260
287
  },
261
- async configureServer(server) {
288
+ configureServer(server) {
262
289
  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
290
  const release = releaseManagedHmrServer;
272
291
  if (!release) return;
273
292
  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,12 @@ 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
+ hmr: resolveHmrOptions(existingServer, publicHost, hmrServer, hmrPort),
125
134
  allowedHosts: resolveAllowedHosts(
126
135
  existingServer.allowedHosts,
127
136
  env.NOTEBOOK_HOST,
@@ -148,7 +157,8 @@ function formatHmrEndpoint(hmr) {
148
157
  function formatHmrSummary(serverOptions, hasManagedServer) {
149
158
  if (serverOptions.hmr === false) return "disabled";
150
159
  const hmr = resolveExistingHmrOptions(serverOptions);
151
- const localEndpoint = hasManagedServer ? `0.0.0.0:${HMR_PORT}` : `vite:${serverOptions.port ?? HMR_PORT}`;
160
+ const port = hmr?.port ?? serverOptions.port ?? HMR_PORT;
161
+ const localEndpoint = hasManagedServer ? `0.0.0.0:${port}` : `vite:${port}`;
152
162
  return `${localEndpoint}, public=${formatHmrEndpoint(hmr)}`;
153
163
  }
154
164
  function formatStartupLog(sandboxType, serverOptions, hasManagedServer) {
@@ -165,6 +175,7 @@ function phpSandboxPlugin() {
165
175
  let releaseManagedHmrServer;
166
176
  let sandboxType;
167
177
  let needsHmrServer = false;
178
+ let hmrPort = HMR_PORT;
168
179
  let startupLog;
169
180
  function setManagedHmrServer(server) {
170
181
  if (managedHmrServer === server) return;
@@ -174,6 +185,7 @@ function phpSandboxPlugin() {
174
185
  releaseManagedHmrServer = void 0;
175
186
  return;
176
187
  }
188
+ const port = hmrPort;
177
189
  let released = false;
178
190
  releaseManagedHmrServer = () => {
179
191
  if (released) return;
@@ -182,32 +194,47 @@ function phpSandboxPlugin() {
182
194
  managedHmrServer = void 0;
183
195
  releaseManagedHmrServer = void 0;
184
196
  }
185
- releaseServer(getGlobalState(), HMR_PORT, server);
197
+ releaseServer(getGlobalState(), port, server);
186
198
  };
187
199
  }
188
200
  return {
189
201
  name: "vite-plugin-phpsandbox",
190
202
  apply: "serve",
191
203
  enforce: "post",
192
- config(userConfig) {
204
+ async config(userConfig, configEnv) {
193
205
  const env = process.env;
206
+ if (shouldSkipHmrSetup(configEnv)) {
207
+ sandboxType = void 0;
208
+ needsHmrServer = false;
209
+ startupLog = void 0;
210
+ setManagedHmrServer(void 0);
211
+ return {};
212
+ }
194
213
  sandboxType = normalizeSandboxType(env.PHPSANDBOX_TYPE);
195
214
  if (!sandboxType) {
196
215
  needsHmrServer = false;
197
216
  startupLog = void 0;
217
+ setManagedHmrServer(void 0);
198
218
  return {};
199
219
  }
200
220
  const isLaravel = sandboxType === "laravel";
201
- const publicHost = resolvePublicHost(env);
202
221
  const existingServer = userConfig.server ?? {};
222
+ hmrPort = resolveHmrPort(existingServer, env);
223
+ const publicHost = resolvePublicHost(env, hmrPort);
203
224
  needsHmrServer = shouldCreateHmrServer(existingServer, isLaravel);
225
+ let hmrServer;
226
+ if (needsHmrServer) {
227
+ hmrServer = managedHmrServer ?? await acquireServer(getGlobalState(), hmrPort);
228
+ setManagedHmrServer(hmrServer);
229
+ }
204
230
  return {
205
231
  server: resolveServerConfig(
206
232
  existingServer,
207
233
  env,
208
234
  isLaravel,
209
235
  publicHost,
210
- void 0
236
+ hmrServer,
237
+ hmrPort
211
238
  )
212
239
  };
213
240
  },
@@ -222,16 +249,8 @@ function phpSandboxPlugin() {
222
249
  startupLog = nextStartupLog;
223
250
  console.info(nextStartupLog);
224
251
  },
225
- async configureServer(server) {
252
+ configureServer(server) {
226
253
  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
254
  const release = releaseManagedHmrServer;
236
255
  if (!release) return;
237
256
  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.0",
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",