vite-plugin-phpsandbox 0.2.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) {
@@ -200,6 +210,8 @@ function phpSandboxPlugin() {
200
210
  let managedHmrServer;
201
211
  let releaseManagedHmrServer;
202
212
  let sandboxType;
213
+ let needsHmrServer = false;
214
+ let hmrPort = HMR_PORT;
203
215
  let startupLog;
204
216
  function setManagedHmrServer(server) {
205
217
  if (managedHmrServer === server) return;
@@ -209,6 +221,7 @@ function phpSandboxPlugin() {
209
221
  releaseManagedHmrServer = void 0;
210
222
  return;
211
223
  }
224
+ const port = hmrPort;
212
225
  let released = false;
213
226
  releaseManagedHmrServer = () => {
214
227
  if (released) return;
@@ -217,33 +230,47 @@ function phpSandboxPlugin() {
217
230
  managedHmrServer = void 0;
218
231
  releaseManagedHmrServer = void 0;
219
232
  }
220
- releaseServer(getGlobalState(), HMR_PORT, server);
233
+ releaseServer(getGlobalState(), port, server);
221
234
  };
222
235
  }
223
236
  return {
224
237
  name: "vite-plugin-phpsandbox",
225
238
  apply: "serve",
226
239
  enforce: "post",
227
- async config(userConfig) {
240
+ async config(userConfig, configEnv) {
228
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
+ }
229
249
  sandboxType = normalizeSandboxType(env.PHPSANDBOX_TYPE);
230
250
  if (!sandboxType) {
231
- setManagedHmrServer(void 0);
251
+ needsHmrServer = false;
232
252
  startupLog = void 0;
253
+ setManagedHmrServer(void 0);
233
254
  return {};
234
255
  }
235
256
  const isLaravel = sandboxType === "laravel";
236
- const publicHost = resolvePublicHost(env);
237
257
  const existingServer = userConfig.server ?? {};
238
- const hmrServer = shouldCreateHmrServer(existingServer, isLaravel) ? managedHmrServer ?? await acquireServer(getGlobalState(), HMR_PORT) : void 0;
239
- setManagedHmrServer(hmrServer);
258
+ hmrPort = resolveHmrPort(existingServer, env);
259
+ const publicHost = resolvePublicHost(env, hmrPort);
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
- hmrServer
272
+ hmrServer,
273
+ hmrPort
247
274
  )
248
275
  };
249
276
  },
@@ -252,13 +279,14 @@ function phpSandboxPlugin() {
252
279
  const nextStartupLog = formatStartupLog(
253
280
  sandboxType,
254
281
  config.server,
255
- Boolean(managedHmrServer)
282
+ needsHmrServer
256
283
  );
257
284
  if (nextStartupLog === startupLog) return;
258
285
  startupLog = nextStartupLog;
259
286
  console.info(nextStartupLog);
260
287
  },
261
288
  configureServer(server) {
289
+ if (!sandboxType) return;
262
290
  const release = releaseManagedHmrServer;
263
291
  if (!release) return;
264
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) {
@@ -164,6 +174,8 @@ function phpSandboxPlugin() {
164
174
  let managedHmrServer;
165
175
  let releaseManagedHmrServer;
166
176
  let sandboxType;
177
+ let needsHmrServer = false;
178
+ let hmrPort = HMR_PORT;
167
179
  let startupLog;
168
180
  function setManagedHmrServer(server) {
169
181
  if (managedHmrServer === server) return;
@@ -173,6 +185,7 @@ function phpSandboxPlugin() {
173
185
  releaseManagedHmrServer = void 0;
174
186
  return;
175
187
  }
188
+ const port = hmrPort;
176
189
  let released = false;
177
190
  releaseManagedHmrServer = () => {
178
191
  if (released) return;
@@ -181,33 +194,47 @@ function phpSandboxPlugin() {
181
194
  managedHmrServer = void 0;
182
195
  releaseManagedHmrServer = void 0;
183
196
  }
184
- releaseServer(getGlobalState(), HMR_PORT, server);
197
+ releaseServer(getGlobalState(), port, server);
185
198
  };
186
199
  }
187
200
  return {
188
201
  name: "vite-plugin-phpsandbox",
189
202
  apply: "serve",
190
203
  enforce: "post",
191
- async config(userConfig) {
204
+ async config(userConfig, configEnv) {
192
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
+ }
193
213
  sandboxType = normalizeSandboxType(env.PHPSANDBOX_TYPE);
194
214
  if (!sandboxType) {
195
- setManagedHmrServer(void 0);
215
+ needsHmrServer = false;
196
216
  startupLog = void 0;
217
+ setManagedHmrServer(void 0);
197
218
  return {};
198
219
  }
199
220
  const isLaravel = sandboxType === "laravel";
200
- const publicHost = resolvePublicHost(env);
201
221
  const existingServer = userConfig.server ?? {};
202
- const hmrServer = shouldCreateHmrServer(existingServer, isLaravel) ? managedHmrServer ?? await acquireServer(getGlobalState(), HMR_PORT) : void 0;
203
- setManagedHmrServer(hmrServer);
222
+ hmrPort = resolveHmrPort(existingServer, env);
223
+ const publicHost = resolvePublicHost(env, hmrPort);
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
- hmrServer
236
+ hmrServer,
237
+ hmrPort
211
238
  )
212
239
  };
213
240
  },
@@ -216,13 +243,14 @@ function phpSandboxPlugin() {
216
243
  const nextStartupLog = formatStartupLog(
217
244
  sandboxType,
218
245
  config.server,
219
- Boolean(managedHmrServer)
246
+ needsHmrServer
220
247
  );
221
248
  if (nextStartupLog === startupLog) return;
222
249
  startupLog = nextStartupLog;
223
250
  console.info(nextStartupLog);
224
251
  },
225
252
  configureServer(server) {
253
+ if (!sandboxType) return;
226
254
  const release = releaseManagedHmrServer;
227
255
  if (!release) return;
228
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.2.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",