vite-plugin-php 1.0.10 → 1.0.20

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
@@ -15,6 +15,11 @@ export default defineConfig({
15
15
  });
16
16
  ```
17
17
 
18
+ Check out the [starter repo](https://github.com/nititech/php-vite-starter) for an easy and convenient start:
19
+ <a href="https://github.com/nititech/php-vite-starter" target="_blank"><img src="https://nititech.de/kosmo-starter-button.png" alt="Starter Repo"></a>
20
+
21
+ #### ⚡️⚡️⚡️ New feature: URL rewrite!
22
+
18
23
  ## Write some PHP code in your `index.php`
19
24
 
20
25
  ```php
@@ -40,6 +45,21 @@ The plugin will serve you the processed `index.php` as usual, including all impo
40
45
 
41
46
  ## Configuration
42
47
 
48
+ The configuration takes following properties:
49
+
50
+ ```ts
51
+ type UsePHPConfig = {
52
+ binary?: string;
53
+ entry?: string | string[];
54
+ rewriteUrl?: (requestUrl: URL) => URL | undefined;
55
+ tempDir?: string;
56
+ cleanup?: {
57
+ dev?: boolean;
58
+ build?: boolean;
59
+ };
60
+ };
61
+ ```
62
+
43
63
  By default the plugin is trying to access the system `php`-binary and load the `index.php` file as the main entry point.
44
64
  However you have the possibility to use an other binary or even compile multiple entry-points:
45
65
 
@@ -61,7 +81,6 @@ Should you have multiple entry-points, you will be able to access each one accor
61
81
  | shop/index.php | `/shop/` `/shop/index.php` | `shop/index.php` |
62
82
  | ... | ... | ... |
63
83
 
64
- **⚡️ New feature:** Wildcard selectors!\
65
84
  Since version 1.0.6 you can specify wildcard entry points:
66
85
 
67
86
  ```js
@@ -79,6 +98,29 @@ usePHP({
79
98
 
80
99
  These entries will also render according to the routing table above.
81
100
 
101
+ ##### Rewrite urls
102
+
103
+ If you are using some sort of Apaches _mod_rewrite_ magic or nginx rewrite rules you can simulate them with the newly added in `rewriteUrl` property.
104
+ The rewriteUrl function has one parameter - the requested URL given as URL object - and return either a modified URL object or undefined:
105
+
106
+ ```js
107
+ usePHP({
108
+ entry: ['index.php', 'partials/**/*.php'],
109
+ rewriteUrl(requestUrl) {
110
+ if (['.js', '.css'].some((s) => requestUrl.pathname.includes(s))) {
111
+ return;
112
+ }
113
+
114
+ requestUrl.search = '_request_=' + requestUrl.pathname;
115
+ requestUrl.pathname = 'index.php';
116
+
117
+ return requestUrl;
118
+ },
119
+ });
120
+ ```
121
+
122
+ ⚠️ **Attention:** If using the rewriteUrl property you will need to exclude (_return undefined_) assets like CSS, JavaScript, Images, etc.. on your own!
123
+
82
124
  ## Known issues
83
125
 
84
126
  Vite won't be able to process PHP-computed styles, scripts or images:
@@ -94,6 +136,27 @@ If you encounter any other bugs or need some other features feel free to open an
94
136
  Love open source? Enjoying my project?\
95
137
  Your support can keep the momentum going! Consider a donation to fuel the creation of more innovative open source software.
96
138
 
97
- | via Ko-Fi | Buy me a coffee | via PayPal |
98
- | --------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
99
- | [![ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/Y8Y2ALMG) | <a href="https://www.buymeacoffee.com/donnikitos" target="_blank"><img src="https://nititech.de/donate-buymeacoffee.png" alt="Buy Me A Coffee" width="174"></a> | <a href="https://www.paypal.com/donate/?hosted_button_id=EPXZPRTR7JHDW" target="_blank"><img src="https://nititech.de/donate-paypal.png" alt="PayPal" width="174"></a> |
139
+ <table>
140
+ <tr>
141
+ <td>
142
+ via Ko-Fi
143
+ </td>
144
+ <td>
145
+ Buy me a coffee
146
+ </td>
147
+ <td>
148
+ via PayPal
149
+ </td>
150
+ </tr>
151
+ <tr>
152
+ <td>
153
+ <a href="https://ko-fi.com/Y8Y2ALMG" target="_blank"><img src="https://ko-fi.com/img/githubbutton_sm.svg" alt="Ko-Fi" width="174"></a>
154
+ </td>
155
+ <td>
156
+ <a href="https://www.buymeacoffee.com/donnikitos" target="_blank"><img src="https://nititech.de/donate-buymeacoffee.png" alt="Buy Me A Coffee" width="174"></a>
157
+ </td>
158
+ <td>
159
+ <a href="https://www.paypal.com/donate/?hosted_button_id=EPXZPRTR7JHDW" target="_blank"><img src="https://nititech.de/donate-paypal.png" alt="PayPal" width="174"></a>
160
+ </td>
161
+ </tr>
162
+ </table>
package/dist/index.cjs CHANGED
@@ -4,6 +4,7 @@ const require$$0 = require('fs');
4
4
  const require$$0$1 = require('path');
5
5
  const http = require('http');
6
6
  const child_process = require('child_process');
7
+ const url = require('url');
7
8
  const require$$0$2 = require('os');
8
9
  const require$$0$3 = require('util');
9
10
  const require$$0$4 = require('stream');
@@ -70,7 +71,7 @@ function start(root) {
70
71
  "localhost:" + phpServer.port,
71
72
  "-t",
72
73
  root,
73
- decodeURI(routerFileUrl.pathname)
74
+ url.fileURLToPath(routerFileUrl)
74
75
  ]).once("spawn", () => {
75
76
  console.log(
76
77
  `PHP development server started (PID: ${globalThis.php?.pid})`
@@ -103,6 +104,10 @@ const phpServer = {
103
104
 
104
105
  var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
105
106
 
107
+ function getDefaultExportFromCjs (x) {
108
+ return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
109
+ }
110
+
106
111
  var tasks = {};
107
112
 
108
113
  var utils$k = {};
@@ -6818,10 +6823,14 @@ function assertPatternsInput(input) {
6818
6823
  }
6819
6824
  var out = FastGlob;
6820
6825
 
6826
+ const fastGlob = /*@__PURE__*/getDefaultExportFromCjs(out);
6827
+
6828
+ const internalParam = "__314159265359__";
6821
6829
  function usePHP(cfg = {}) {
6822
6830
  const {
6823
6831
  binary = "php",
6824
6832
  entry = "index.php",
6833
+ rewriteUrl = (requestUrl) => requestUrl,
6825
6834
  tempDir = ".php-tmp",
6826
6835
  cleanup = {}
6827
6836
  } = cfg;
@@ -6866,7 +6875,7 @@ function usePHP(cfg = {}) {
6866
6875
  writeFile(gitIgnoreFile, "*\n**/*.php.html");
6867
6876
  }
6868
6877
  entries = entries.flatMap(
6869
- (entry2) => out.globSync(entry2, {
6878
+ (entry2) => fastGlob.globSync(entry2, {
6870
6879
  dot: true,
6871
6880
  onlyFiles: true,
6872
6881
  unique: true,
@@ -6894,23 +6903,39 @@ function usePHP(cfg = {}) {
6894
6903
  phpServer.start(viteServer?.config.root);
6895
6904
  server.middlewares.use(async (req, res, next) => {
6896
6905
  try {
6897
- if (req.url) {
6898
- const url = new URL(
6899
- req.url,
6900
- "http://localhost:" + phpServer.port
6901
- );
6902
- let requestUrl = url.pathname;
6903
- if (requestUrl.endsWith("/")) {
6904
- requestUrl += "index.php";
6906
+ if (req.url && !["/@vite", "/@fs"].some(
6907
+ (path) => req.url.startsWith(path)
6908
+ )) {
6909
+ const url = new URL(req.url, "http://localhost");
6910
+ if (config?.server.port) {
6911
+ url.port = config.server.port.toString();
6905
6912
  }
6906
- requestUrl = requestUrl.substring(1);
6913
+ const requestUrl = url.pathname;
6914
+ if (url.pathname.endsWith("/")) {
6915
+ url.pathname += "index.php";
6916
+ }
6917
+ const routedUrl = rewriteUrl(url);
6918
+ if (routedUrl) {
6919
+ url.pathname = routedUrl.pathname;
6920
+ url.search = routedUrl.search;
6921
+ url.hash = routedUrl.hash;
6922
+ }
6923
+ const entryPathname = url.pathname.substring(1);
6907
6924
  const entry2 = entries.find((file) => {
6908
- return file === requestUrl || file.substring(0, file.lastIndexOf(".")) === requestUrl;
6925
+ return file === entryPathname || file.substring(0, file.lastIndexOf(".")) === entryPathname;
6909
6926
  });
6910
6927
  if (entry2) {
6911
6928
  const tempFile = `${tempDir}/${entry2}.html`;
6912
6929
  if (require$$0.existsSync(require$$0$1.resolve(tempFile))) {
6913
6930
  url.pathname = tempFile;
6931
+ url.port = phpServer.port.toString();
6932
+ url.searchParams.set(
6933
+ internalParam,
6934
+ new URLSearchParams({
6935
+ REQUEST_URI: requestUrl,
6936
+ PHP_SELF: "/" + entry2
6937
+ }).toString()
6938
+ );
6914
6939
  const phpResult = await new Promise(
6915
6940
  (resolve2, reject) => {
6916
6941
  const chunks = [];
@@ -6937,7 +6962,7 @@ function usePHP(cfg = {}) {
6937
6962
  );
6938
6963
  let out = phpResult.toString();
6939
6964
  out = await server.transformIndexHtml(
6940
- requestUrl || "/",
6965
+ entryPathname || "/",
6941
6966
  out
6942
6967
  );
6943
6968
  res.end(out);
package/dist/index.d.cts CHANGED
@@ -3,6 +3,7 @@ import { Plugin } from 'vite';
3
3
  type UsePHPConfig = {
4
4
  binary?: string;
5
5
  entry?: string | string[];
6
+ rewriteUrl?: (requestUrl: URL) => URL | undefined;
6
7
  tempDir?: string;
7
8
  cleanup?: {
8
9
  dev?: boolean;
package/dist/index.d.mts CHANGED
@@ -3,6 +3,7 @@ import { Plugin } from 'vite';
3
3
  type UsePHPConfig = {
4
4
  binary?: string;
5
5
  entry?: string | string[];
6
+ rewriteUrl?: (requestUrl: URL) => URL | undefined;
6
7
  tempDir?: string;
7
8
  cleanup?: {
8
9
  dev?: boolean;
package/dist/index.d.ts CHANGED
@@ -3,6 +3,7 @@ import { Plugin } from 'vite';
3
3
  type UsePHPConfig = {
4
4
  binary?: string;
5
5
  entry?: string | string[];
6
+ rewriteUrl?: (requestUrl: URL) => URL | undefined;
6
7
  tempDir?: string;
7
8
  cleanup?: {
8
9
  dev?: boolean;
package/dist/index.mjs CHANGED
@@ -2,6 +2,7 @@ import require$$0$4, { mkdirSync, writeFileSync, readFileSync, existsSync, rmSyn
2
2
  import require$$0$1, { dirname, resolve } from 'path';
3
3
  import http from 'http';
4
4
  import { spawn } from 'child_process';
5
+ import { fileURLToPath } from 'url';
5
6
  import require$$0 from 'os';
6
7
  import require$$0$2 from 'util';
7
8
  import require$$0$3 from 'stream';
@@ -57,7 +58,7 @@ function start(root) {
57
58
  "localhost:" + phpServer.port,
58
59
  "-t",
59
60
  root,
60
- decodeURI(routerFileUrl.pathname)
61
+ fileURLToPath(routerFileUrl)
61
62
  ]).once("spawn", () => {
62
63
  console.log(
63
64
  `PHP development server started (PID: ${globalThis.php?.pid})`
@@ -90,6 +91,10 @@ const phpServer = {
90
91
 
91
92
  var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
92
93
 
94
+ function getDefaultExportFromCjs (x) {
95
+ return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
96
+ }
97
+
93
98
  var tasks = {};
94
99
 
95
100
  var utils$k = {};
@@ -6805,10 +6810,14 @@ function assertPatternsInput(input) {
6805
6810
  }
6806
6811
  var out = FastGlob;
6807
6812
 
6813
+ const fastGlob = /*@__PURE__*/getDefaultExportFromCjs(out);
6814
+
6815
+ const internalParam = "__314159265359__";
6808
6816
  function usePHP(cfg = {}) {
6809
6817
  const {
6810
6818
  binary = "php",
6811
6819
  entry = "index.php",
6820
+ rewriteUrl = (requestUrl) => requestUrl,
6812
6821
  tempDir = ".php-tmp",
6813
6822
  cleanup = {}
6814
6823
  } = cfg;
@@ -6853,7 +6862,7 @@ function usePHP(cfg = {}) {
6853
6862
  writeFile(gitIgnoreFile, "*\n**/*.php.html");
6854
6863
  }
6855
6864
  entries = entries.flatMap(
6856
- (entry2) => out.globSync(entry2, {
6865
+ (entry2) => fastGlob.globSync(entry2, {
6857
6866
  dot: true,
6858
6867
  onlyFiles: true,
6859
6868
  unique: true,
@@ -6881,23 +6890,39 @@ function usePHP(cfg = {}) {
6881
6890
  phpServer.start(viteServer?.config.root);
6882
6891
  server.middlewares.use(async (req, res, next) => {
6883
6892
  try {
6884
- if (req.url) {
6885
- const url = new URL(
6886
- req.url,
6887
- "http://localhost:" + phpServer.port
6888
- );
6889
- let requestUrl = url.pathname;
6890
- if (requestUrl.endsWith("/")) {
6891
- requestUrl += "index.php";
6893
+ if (req.url && !["/@vite", "/@fs"].some(
6894
+ (path) => req.url.startsWith(path)
6895
+ )) {
6896
+ const url = new URL(req.url, "http://localhost");
6897
+ if (config?.server.port) {
6898
+ url.port = config.server.port.toString();
6892
6899
  }
6893
- requestUrl = requestUrl.substring(1);
6900
+ const requestUrl = url.pathname;
6901
+ if (url.pathname.endsWith("/")) {
6902
+ url.pathname += "index.php";
6903
+ }
6904
+ const routedUrl = rewriteUrl(url);
6905
+ if (routedUrl) {
6906
+ url.pathname = routedUrl.pathname;
6907
+ url.search = routedUrl.search;
6908
+ url.hash = routedUrl.hash;
6909
+ }
6910
+ const entryPathname = url.pathname.substring(1);
6894
6911
  const entry2 = entries.find((file) => {
6895
- return file === requestUrl || file.substring(0, file.lastIndexOf(".")) === requestUrl;
6912
+ return file === entryPathname || file.substring(0, file.lastIndexOf(".")) === entryPathname;
6896
6913
  });
6897
6914
  if (entry2) {
6898
6915
  const tempFile = `${tempDir}/${entry2}.html`;
6899
6916
  if (existsSync(resolve(tempFile))) {
6900
6917
  url.pathname = tempFile;
6918
+ url.port = phpServer.port.toString();
6919
+ url.searchParams.set(
6920
+ internalParam,
6921
+ new URLSearchParams({
6922
+ REQUEST_URI: requestUrl,
6923
+ PHP_SELF: "/" + entry2
6924
+ }).toString()
6925
+ );
6901
6926
  const phpResult = await new Promise(
6902
6927
  (resolve2, reject) => {
6903
6928
  const chunks = [];
@@ -6924,7 +6949,7 @@ function usePHP(cfg = {}) {
6924
6949
  );
6925
6950
  let out = phpResult.toString();
6926
6951
  out = await server.transformIndexHtml(
6927
- requestUrl || "/",
6952
+ entryPathname || "/",
6928
6953
  out
6929
6954
  );
6930
6955
  res.end(out);
package/dist/router.php CHANGED
@@ -1,6 +1,18 @@
1
1
  <?php
2
2
  $sourceFile = $_SERVER['SCRIPT_FILENAME'];
3
3
 
4
+ $internal_param = '__314159265359__';
5
+ parse_str($_GET[$internal_param], $internal_vars);
6
+
7
+ foreach ($internal_vars as $key => $value) {
8
+ $_SERVER[$key] = $value;
9
+ }
10
+ unset($_GET[$internal_param]);
11
+
12
+ $_SERVER['SCRIPT_NAME'] = $_SERVER['PHP_SELF'];
13
+ $_SERVER['SCRIPT_FILENAME'] = $_SERVER['DOCUMENT_ROOT'] . $_SERVER['SCRIPT_NAME'];
14
+ $_SERVER['QUERY_STRING'] = http_build_query($_GET);
15
+
4
16
  $source = file_get_contents($sourceFile);
5
17
  $codeTokens = json_decode(file_get_contents("$sourceFile.json"), true);
6
18
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vite-plugin-php",
3
- "version": "1.0.10",
4
- "description": "Precompile PHP-files with the speed of Vite",
3
+ "version": "1.0.20",
4
+ "description": "Process PHP-files with the speed and tools of Vite",
5
5
  "keywords": [
6
6
  "vite",
7
7
  "vite-plugin",
@@ -12,7 +12,10 @@
12
12
  "php",
13
13
  "php-loader",
14
14
  "php-compiler",
15
- "loader"
15
+ "loader",
16
+ "url rewrite",
17
+ "url router",
18
+ "mod_rewrite"
16
19
  ],
17
20
  "author": "Nikita 'donnikitos' Nitichevski <me@donnikitos.com> (https://donnikitos.com/)",
18
21
  "license": "MIT",
@@ -51,10 +54,10 @@
51
54
  },
52
55
  "dependencies": {},
53
56
  "devDependencies": {
54
- "@types/node": "^20.11.5",
57
+ "@types/node": "^20.12.7",
55
58
  "fast-glob": "^3.3.2",
56
- "typescript": "^5.3.3",
59
+ "typescript": "^5.4.5",
57
60
  "unbuild": "^2.0.0",
58
- "vite": "^5.1.3"
61
+ "vite": "^5.2.8"
59
62
  }
60
63
  }