vite-plugin-php 1.0.11 → 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,9 +15,11 @@ export default defineConfig({
15
15
  });
16
16
  ```
17
17
 
18
- Check out the starter repo for an easy and convenient start:
18
+ Check out the [starter repo](https://github.com/nititech/php-vite-starter) for an easy and convenient start:
19
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
20
 
21
+ #### ⚡️⚡️⚡️ New feature: URL rewrite!
22
+
21
23
  ## Write some PHP code in your `index.php`
22
24
 
23
25
  ```php
@@ -43,6 +45,21 @@ The plugin will serve you the processed `index.php` as usual, including all impo
43
45
 
44
46
  ## Configuration
45
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
+
46
63
  By default the plugin is trying to access the system `php`-binary and load the `index.php` file as the main entry point.
47
64
  However you have the possibility to use an other binary or even compile multiple entry-points:
48
65
 
@@ -64,7 +81,6 @@ Should you have multiple entry-points, you will be able to access each one accor
64
81
  | shop/index.php | `/shop/` `/shop/index.php` | `shop/index.php` |
65
82
  | ... | ... | ... |
66
83
 
67
- **⚡️ New feature:** Wildcard selectors!\
68
84
  Since version 1.0.6 you can specify wildcard entry points:
69
85
 
70
86
  ```js
@@ -82,6 +98,29 @@ usePHP({
82
98
 
83
99
  These entries will also render according to the routing table above.
84
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
+
85
124
  ## Known issues
86
125
 
87
126
  Vite won't be able to process PHP-computed styles, scripts or images:
package/dist/index.cjs CHANGED
@@ -104,6 +104,10 @@ const phpServer = {
104
104
 
105
105
  var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
106
106
 
107
+ function getDefaultExportFromCjs (x) {
108
+ return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
109
+ }
110
+
107
111
  var tasks = {};
108
112
 
109
113
  var utils$k = {};
@@ -6819,10 +6823,14 @@ function assertPatternsInput(input) {
6819
6823
  }
6820
6824
  var out = FastGlob;
6821
6825
 
6826
+ const fastGlob = /*@__PURE__*/getDefaultExportFromCjs(out);
6827
+
6828
+ const internalParam = "__314159265359__";
6822
6829
  function usePHP(cfg = {}) {
6823
6830
  const {
6824
6831
  binary = "php",
6825
6832
  entry = "index.php",
6833
+ rewriteUrl = (requestUrl) => requestUrl,
6826
6834
  tempDir = ".php-tmp",
6827
6835
  cleanup = {}
6828
6836
  } = cfg;
@@ -6867,7 +6875,7 @@ function usePHP(cfg = {}) {
6867
6875
  writeFile(gitIgnoreFile, "*\n**/*.php.html");
6868
6876
  }
6869
6877
  entries = entries.flatMap(
6870
- (entry2) => out.globSync(entry2, {
6878
+ (entry2) => fastGlob.globSync(entry2, {
6871
6879
  dot: true,
6872
6880
  onlyFiles: true,
6873
6881
  unique: true,
@@ -6895,23 +6903,39 @@ function usePHP(cfg = {}) {
6895
6903
  phpServer.start(viteServer?.config.root);
6896
6904
  server.middlewares.use(async (req, res, next) => {
6897
6905
  try {
6898
- if (req.url) {
6899
- const url = new URL(
6900
- req.url,
6901
- "http://localhost:" + phpServer.port
6902
- );
6903
- let requestUrl = url.pathname;
6904
- if (requestUrl.endsWith("/")) {
6905
- 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();
6906
6912
  }
6907
- 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);
6908
6924
  const entry2 = entries.find((file) => {
6909
- return file === requestUrl || file.substring(0, file.lastIndexOf(".")) === requestUrl;
6925
+ return file === entryPathname || file.substring(0, file.lastIndexOf(".")) === entryPathname;
6910
6926
  });
6911
6927
  if (entry2) {
6912
6928
  const tempFile = `${tempDir}/${entry2}.html`;
6913
6929
  if (require$$0.existsSync(require$$0$1.resolve(tempFile))) {
6914
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
+ );
6915
6939
  const phpResult = await new Promise(
6916
6940
  (resolve2, reject) => {
6917
6941
  const chunks = [];
@@ -6938,7 +6962,7 @@ function usePHP(cfg = {}) {
6938
6962
  );
6939
6963
  let out = phpResult.toString();
6940
6964
  out = await server.transformIndexHtml(
6941
- requestUrl || "/",
6965
+ entryPathname || "/",
6942
6966
  out
6943
6967
  );
6944
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
@@ -91,6 +91,10 @@ const phpServer = {
91
91
 
92
92
  var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
93
93
 
94
+ function getDefaultExportFromCjs (x) {
95
+ return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
96
+ }
97
+
94
98
  var tasks = {};
95
99
 
96
100
  var utils$k = {};
@@ -6806,10 +6810,14 @@ function assertPatternsInput(input) {
6806
6810
  }
6807
6811
  var out = FastGlob;
6808
6812
 
6813
+ const fastGlob = /*@__PURE__*/getDefaultExportFromCjs(out);
6814
+
6815
+ const internalParam = "__314159265359__";
6809
6816
  function usePHP(cfg = {}) {
6810
6817
  const {
6811
6818
  binary = "php",
6812
6819
  entry = "index.php",
6820
+ rewriteUrl = (requestUrl) => requestUrl,
6813
6821
  tempDir = ".php-tmp",
6814
6822
  cleanup = {}
6815
6823
  } = cfg;
@@ -6854,7 +6862,7 @@ function usePHP(cfg = {}) {
6854
6862
  writeFile(gitIgnoreFile, "*\n**/*.php.html");
6855
6863
  }
6856
6864
  entries = entries.flatMap(
6857
- (entry2) => out.globSync(entry2, {
6865
+ (entry2) => fastGlob.globSync(entry2, {
6858
6866
  dot: true,
6859
6867
  onlyFiles: true,
6860
6868
  unique: true,
@@ -6882,23 +6890,39 @@ function usePHP(cfg = {}) {
6882
6890
  phpServer.start(viteServer?.config.root);
6883
6891
  server.middlewares.use(async (req, res, next) => {
6884
6892
  try {
6885
- if (req.url) {
6886
- const url = new URL(
6887
- req.url,
6888
- "http://localhost:" + phpServer.port
6889
- );
6890
- let requestUrl = url.pathname;
6891
- if (requestUrl.endsWith("/")) {
6892
- 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();
6893
6899
  }
6894
- 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);
6895
6911
  const entry2 = entries.find((file) => {
6896
- return file === requestUrl || file.substring(0, file.lastIndexOf(".")) === requestUrl;
6912
+ return file === entryPathname || file.substring(0, file.lastIndexOf(".")) === entryPathname;
6897
6913
  });
6898
6914
  if (entry2) {
6899
6915
  const tempFile = `${tempDir}/${entry2}.html`;
6900
6916
  if (existsSync(resolve(tempFile))) {
6901
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
+ );
6902
6926
  const phpResult = await new Promise(
6903
6927
  (resolve2, reject) => {
6904
6928
  const chunks = [];
@@ -6925,7 +6949,7 @@ function usePHP(cfg = {}) {
6925
6949
  );
6926
6950
  let out = phpResult.toString();
6927
6951
  out = await server.transformIndexHtml(
6928
- requestUrl || "/",
6952
+ entryPathname || "/",
6929
6953
  out
6930
6954
  );
6931
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.11",
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",