vite-plugin-php 2.0.0 → 2.0.2

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
@@ -29,7 +29,7 @@ export default defineConfig({
29
29
 
30
30
  <p align="center" style="text-align: center;">
31
31
  <b>
32
- <a href="https://vite-php.nititech.de/">Wiki</a> | <a href="https://github.com/donnikitos/vite-plugin-php/discussions">Discussions</a> | <a href="https://github.com/nititech/php-vite-starter">Starter-Repo</a>
32
+ <a href="https://www.npmjs.com/package/vite-plugin-php">NPM</a> | <a href="https://vite-php.nititech.de/">Wiki</a> | <a href="https://github.com/donnikitos/vite-plugin-php/discussions">Discussions</a> | <a href="https://github.com/nititech/php-vite-starter">Starter-Repo</a>
33
33
  </b>
34
34
  </p>
35
35
 
@@ -84,6 +84,8 @@ The configuration takes following properties:
84
84
  ```ts
85
85
  type UsePHPConfig = {
86
86
  binary?: string;
87
+ // Override default PHP server host address. Default is `localhost`.
88
+ php?: { host?: string };
87
89
  entry?: string | string[];
88
90
  rewriteUrl?: (requestUrl: URL) => URL | undefined;
89
91
  tempDir?: string;
package/dist/index.cjs CHANGED
@@ -5,6 +5,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  const node_child_process = require('node:child_process');
6
6
  const url = require('url');
7
7
  const node_path = require('node:path');
8
+ const tcpPortUsed = require('tcp-port-used');
8
9
  const require$$0 = require('os');
9
10
  const require$$0$1 = require('path');
10
11
  const require$$0$2 = require('util');
@@ -18,6 +19,7 @@ const vite = require('vite');
18
19
  var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
19
20
  function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
20
21
 
22
+ const tcpPortUsed__default = /*#__PURE__*/_interopDefaultCompat(tcpPortUsed);
21
23
  const require$$0__default = /*#__PURE__*/_interopDefaultCompat(require$$0);
22
24
  const require$$0__default$1 = /*#__PURE__*/_interopDefaultCompat(require$$0$1);
23
25
  const require$$0__default$2 = /*#__PURE__*/_interopDefaultCompat(require$$0$2);
@@ -210,38 +212,46 @@ log.error = function(json, options = {}) {
210
212
 
211
213
  const PHP_Server = {
212
214
  binary: "php",
213
- port: 65535,
215
+ host: "localhost",
216
+ port: 6535,
214
217
  process: void 0,
215
218
  start,
216
219
  stop
217
220
  };
218
- function start(root) {
221
+ async function start(root) {
219
222
  if (!PHP_Server.process?.pid) {
220
- const routerFileUrl = new URL("./router.php", (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href)));
221
- PHP_Server.process = node_child_process.spawn(PHP_Server.binary, [
222
- "-S",
223
- "localhost:" + PHP_Server.port,
224
- "-t",
225
- root,
226
- url.fileURLToPath(routerFileUrl)
227
- ]).once("spawn", () => {
228
- log(`Server started (PID: ${PHP_Server.process?.pid})`);
229
- }).on("error", (error) => {
230
- log(`Server error: ${error.message})`, {
231
- type: "error"
223
+ await new Promise(async (resolve, reject) => {
224
+ const routerFileUrl = new URL("./router.php", (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href)));
225
+ while (await tcpPortUsed__default.check(PHP_Server.port, PHP_Server.host)) {
226
+ PHP_Server.port++;
227
+ }
228
+ PHP_Server.process = node_child_process.spawn(PHP_Server.binary, [
229
+ "-S",
230
+ `${PHP_Server.host}:${PHP_Server.port}`,
231
+ "-t",
232
+ root,
233
+ url.fileURLToPath(routerFileUrl)
234
+ ]).once("spawn", () => {
235
+ log(`Server started (PID: ${PHP_Server.process?.pid})`);
236
+ resolve(PHP_Server.process?.pid);
237
+ }).on("error", (error) => {
238
+ log(`Server error: ${error.message})`, {
239
+ type: "error"
240
+ });
241
+ reject(error);
232
242
  });
233
- });
234
- PHP_Server.process.stdout?.on("data", (data) => {
235
- log("", { timestamp: true });
236
- `${data}`.trim().split("\r\n").forEach((line) => {
237
- if (line.startsWith(internalParam + ":")) {
238
- log.error(
239
- line.substring((internalParam + ":").length),
240
- { prefix: false }
241
- );
242
- } else {
243
- log(line);
244
- }
243
+ PHP_Server.process.stdout?.on("data", (data) => {
244
+ log("", { timestamp: true });
245
+ `${data}`.trim().split("\r\n").forEach((line) => {
246
+ if (line.startsWith(internalParam + ":")) {
247
+ log.error(
248
+ line.substring((internalParam + ":").length),
249
+ { prefix: false }
250
+ );
251
+ } else {
252
+ log(line);
253
+ }
254
+ });
245
255
  });
246
256
  });
247
257
  }
@@ -7175,9 +7185,9 @@ const servePlugin = {
7175
7185
  PHP_Code.fromFile(entry).applyEnv().write(tempName(entry));
7176
7186
  });
7177
7187
  },
7178
- configureServer(server) {
7188
+ async configureServer(server) {
7179
7189
  if (!PHP_Server.process) {
7180
- PHP_Server.start(server?.config.root);
7190
+ await PHP_Server.start(server?.config.root);
7181
7191
  }
7182
7192
  server.middlewares.use(async (req, res, next) => {
7183
7193
  try {
@@ -7209,6 +7219,7 @@ const servePlugin = {
7209
7219
  const tempFile = tempName(entry);
7210
7220
  if (node_fs.existsSync(node_path.resolve(tempFile))) {
7211
7221
  url.pathname = tempFile;
7222
+ url.host = PHP_Server.host;
7212
7223
  url.port = PHP_Server.port.toString();
7213
7224
  url.searchParams.set(
7214
7225
  internalParam,
@@ -7273,7 +7284,7 @@ const servePlugin = {
7273
7284
  "html"
7274
7285
  )) {
7275
7286
  out = await server.transformIndexHtml(
7276
- `/${entry}.html`,
7287
+ `/${entry}.html:${requestUrl}`,
7277
7288
  out,
7278
7289
  requestUrl
7279
7290
  );
@@ -7411,6 +7422,7 @@ const buildPlugin = {
7411
7422
  function usePHP(cfg = {}) {
7412
7423
  const { entry = "index.php" } = cfg;
7413
7424
  PHP_Server.binary = cfg.binary ?? PHP_Server.binary;
7425
+ PHP_Server.host = cfg.php?.host ?? PHP_Server.host;
7414
7426
  serve.rewriteUrl = cfg.rewriteUrl ?? serve.rewriteUrl;
7415
7427
  shared.entries = Array.isArray(entry) ? entry : [entry];
7416
7428
  shared.tempDir = cfg.tempDir ?? shared.tempDir;
package/dist/index.d.cts CHANGED
@@ -22,6 +22,9 @@ type EPHPError = (typeof EPHPError)[keyof typeof EPHPError];
22
22
 
23
23
  type UsePHPConfig = {
24
24
  binary?: string;
25
+ php?: {
26
+ host?: string;
27
+ };
25
28
  entry?: string | string[];
26
29
  rewriteUrl?: (requestUrl: URL) => URL | undefined;
27
30
  tempDir?: string;
package/dist/index.d.mts CHANGED
@@ -22,6 +22,9 @@ type EPHPError = (typeof EPHPError)[keyof typeof EPHPError];
22
22
 
23
23
  type UsePHPConfig = {
24
24
  binary?: string;
25
+ php?: {
26
+ host?: string;
27
+ };
25
28
  entry?: string | string[];
26
29
  rewriteUrl?: (requestUrl: URL) => URL | undefined;
27
30
  tempDir?: string;
package/dist/index.d.ts CHANGED
@@ -22,6 +22,9 @@ type EPHPError = (typeof EPHPError)[keyof typeof EPHPError];
22
22
 
23
23
  type UsePHPConfig = {
24
24
  binary?: string;
25
+ php?: {
26
+ host?: string;
27
+ };
25
28
  entry?: string | string[];
26
29
  rewriteUrl?: (requestUrl: URL) => URL | undefined;
27
30
  tempDir?: string;
package/dist/index.mjs CHANGED
@@ -1,6 +1,7 @@
1
1
  import { spawn } from 'node:child_process';
2
2
  import { fileURLToPath } from 'url';
3
3
  import { resolve, dirname, relative } from 'node:path';
4
+ import tcpPortUsed from 'tcp-port-used';
4
5
  import require$$0 from 'os';
5
6
  import require$$0$1 from 'path';
6
7
  import require$$0$2 from 'util';
@@ -195,38 +196,46 @@ log.error = function(json, options = {}) {
195
196
 
196
197
  const PHP_Server = {
197
198
  binary: "php",
198
- port: 65535,
199
+ host: "localhost",
200
+ port: 6535,
199
201
  process: void 0,
200
202
  start,
201
203
  stop
202
204
  };
203
- function start(root) {
205
+ async function start(root) {
204
206
  if (!PHP_Server.process?.pid) {
205
- const routerFileUrl = new URL("./router.php", import.meta.url);
206
- PHP_Server.process = spawn(PHP_Server.binary, [
207
- "-S",
208
- "localhost:" + PHP_Server.port,
209
- "-t",
210
- root,
211
- fileURLToPath(routerFileUrl)
212
- ]).once("spawn", () => {
213
- log(`Server started (PID: ${PHP_Server.process?.pid})`);
214
- }).on("error", (error) => {
215
- log(`Server error: ${error.message})`, {
216
- type: "error"
207
+ await new Promise(async (resolve, reject) => {
208
+ const routerFileUrl = new URL("./router.php", import.meta.url);
209
+ while (await tcpPortUsed.check(PHP_Server.port, PHP_Server.host)) {
210
+ PHP_Server.port++;
211
+ }
212
+ PHP_Server.process = spawn(PHP_Server.binary, [
213
+ "-S",
214
+ `${PHP_Server.host}:${PHP_Server.port}`,
215
+ "-t",
216
+ root,
217
+ fileURLToPath(routerFileUrl)
218
+ ]).once("spawn", () => {
219
+ log(`Server started (PID: ${PHP_Server.process?.pid})`);
220
+ resolve(PHP_Server.process?.pid);
221
+ }).on("error", (error) => {
222
+ log(`Server error: ${error.message})`, {
223
+ type: "error"
224
+ });
225
+ reject(error);
217
226
  });
218
- });
219
- PHP_Server.process.stdout?.on("data", (data) => {
220
- log("", { timestamp: true });
221
- `${data}`.trim().split("\r\n").forEach((line) => {
222
- if (line.startsWith(internalParam + ":")) {
223
- log.error(
224
- line.substring((internalParam + ":").length),
225
- { prefix: false }
226
- );
227
- } else {
228
- log(line);
229
- }
227
+ PHP_Server.process.stdout?.on("data", (data) => {
228
+ log("", { timestamp: true });
229
+ `${data}`.trim().split("\r\n").forEach((line) => {
230
+ if (line.startsWith(internalParam + ":")) {
231
+ log.error(
232
+ line.substring((internalParam + ":").length),
233
+ { prefix: false }
234
+ );
235
+ } else {
236
+ log(line);
237
+ }
238
+ });
230
239
  });
231
240
  });
232
241
  }
@@ -7160,9 +7169,9 @@ const servePlugin = {
7160
7169
  PHP_Code.fromFile(entry).applyEnv().write(tempName(entry));
7161
7170
  });
7162
7171
  },
7163
- configureServer(server) {
7172
+ async configureServer(server) {
7164
7173
  if (!PHP_Server.process) {
7165
- PHP_Server.start(server?.config.root);
7174
+ await PHP_Server.start(server?.config.root);
7166
7175
  }
7167
7176
  server.middlewares.use(async (req, res, next) => {
7168
7177
  try {
@@ -7194,6 +7203,7 @@ const servePlugin = {
7194
7203
  const tempFile = tempName(entry);
7195
7204
  if (existsSync(resolve(tempFile))) {
7196
7205
  url.pathname = tempFile;
7206
+ url.host = PHP_Server.host;
7197
7207
  url.port = PHP_Server.port.toString();
7198
7208
  url.searchParams.set(
7199
7209
  internalParam,
@@ -7258,7 +7268,7 @@ const servePlugin = {
7258
7268
  "html"
7259
7269
  )) {
7260
7270
  out = await server.transformIndexHtml(
7261
- `/${entry}.html`,
7271
+ `/${entry}.html:${requestUrl}`,
7262
7272
  out,
7263
7273
  requestUrl
7264
7274
  );
@@ -7396,6 +7406,7 @@ const buildPlugin = {
7396
7406
  function usePHP(cfg = {}) {
7397
7407
  const { entry = "index.php" } = cfg;
7398
7408
  PHP_Server.binary = cfg.binary ?? PHP_Server.binary;
7409
+ PHP_Server.host = cfg.php?.host ?? PHP_Server.host;
7399
7410
  serve.rewriteUrl = cfg.rewriteUrl ?? serve.rewriteUrl;
7400
7411
  shared.entries = Array.isArray(entry) ? entry : [entry];
7401
7412
  shared.tempDir = cfg.tempDir ?? shared.tempDir;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-php",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "author": "Nikita 'donnikitos' Nitichevski <me@donnikitos.com> (https://donnikitos.com/)",
5
5
  "repository": {
6
6
  "type": "git",
@@ -10,6 +10,7 @@
10
10
  "module": "./dist/index.mjs",
11
11
  "devDependencies": {
12
12
  "@types/node": "^22.9.0",
13
+ "@types/tcp-port-used": "^1.0.4",
13
14
  "fast-glob": "^3.3.2",
14
15
  "picocolors": "^1.1.1",
15
16
  "typescript": "^5.6.3",
@@ -65,5 +66,8 @@
65
66
  "prepack": "npm run build"
66
67
  },
67
68
  "type": "module",
68
- "types": "./dist/index.d.ts"
69
+ "types": "./dist/index.d.ts",
70
+ "dependencies": {
71
+ "tcp-port-used": "^1.0.2"
72
+ }
69
73
  }