vite-plugin-php 1.0.11 → 1.0.30

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,17 @@ 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
+ ## ⚡ Latest changes
22
+
23
+ | Version | Feature |
24
+ | ------- | ------------------------ |
25
+ | 1.0.30 | PHP header forwarding |
26
+ | 1.0.20 | URL rewrites |
27
+ | 1.0.11 | Improved Windows support |
28
+
21
29
  ## Write some PHP code in your `index.php`
22
30
 
23
31
  ```php
@@ -43,6 +51,21 @@ The plugin will serve you the processed `index.php` as usual, including all impo
43
51
 
44
52
  ## Configuration
45
53
 
54
+ The configuration takes following properties:
55
+
56
+ ```ts
57
+ type UsePHPConfig = {
58
+ binary?: string;
59
+ entry?: string | string[];
60
+ rewriteUrl?: (requestUrl: URL) => URL | undefined;
61
+ tempDir?: string;
62
+ cleanup?: {
63
+ dev?: boolean;
64
+ build?: boolean;
65
+ };
66
+ };
67
+ ```
68
+
46
69
  By default the plugin is trying to access the system `php`-binary and load the `index.php` file as the main entry point.
47
70
  However you have the possibility to use an other binary or even compile multiple entry-points:
48
71
 
@@ -64,7 +87,6 @@ Should you have multiple entry-points, you will be able to access each one accor
64
87
  | shop/index.php | `/shop/` `/shop/index.php` | `shop/index.php` |
65
88
  | ... | ... | ... |
66
89
 
67
- **⚡️ New feature:** Wildcard selectors!\
68
90
  Since version 1.0.6 you can specify wildcard entry points:
69
91
 
70
92
  ```js
@@ -82,6 +104,29 @@ usePHP({
82
104
 
83
105
  These entries will also render according to the routing table above.
84
106
 
107
+ ##### Rewrite urls
108
+
109
+ 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.
110
+ The rewriteUrl function has one parameter - the requested URL given as URL object - and return either a modified URL object or undefined:
111
+
112
+ ```js
113
+ usePHP({
114
+ entry: ['index.php', 'partials/**/*.php'],
115
+ rewriteUrl(requestUrl) {
116
+ if (['.js', '.css'].some((s) => requestUrl.pathname.includes(s))) {
117
+ return;
118
+ }
119
+
120
+ requestUrl.search = '_request_=' + requestUrl.pathname;
121
+ requestUrl.pathname = 'index.php';
122
+
123
+ return requestUrl;
124
+ },
125
+ });
126
+ ```
127
+
128
+ ⚠️ **Attention:** If using the rewriteUrl property you will need to exclude (_return undefined_) assets like CSS, JavaScript, Images, etc.., that match your transpiled php file names, on your own!
129
+
85
130
  ## Known issues
86
131
 
87
132
  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,53 +6903,74 @@ 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();
6912
+ }
6913
+ const requestUrl = url.pathname;
6914
+ if (url.pathname.endsWith("/")) {
6915
+ url.pathname += "index.php";
6906
6916
  }
6907
- requestUrl = requestUrl.substring(1);
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;
6915
- const phpResult = await new Promise(
6916
- (resolve2, reject) => {
6917
- const chunks = [];
6918
- http__default.request(
6919
- url.toString(),
6920
- {
6921
- method: req.method,
6922
- headers: req.headers
6923
- },
6924
- (msg) => {
6925
- msg.on(
6926
- "data",
6927
- (data) => chunks.push(data)
6928
- );
6929
- msg.on("end", () => {
6930
- const result = Buffer.concat(
6931
- chunks
6932
- ).toString("utf8");
6933
- resolve2(result);
6934
- });
6935
- }
6936
- ).on("error", reject).end();
6937
- }
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
6938
  );
6939
- let out = phpResult.toString();
6940
- out = await server.transformIndexHtml(
6941
- requestUrl || "/",
6942
- out
6939
+ const phpResult = await new Promise((resolve2, reject) => {
6940
+ const chunks = [];
6941
+ http__default.request(
6942
+ url.toString(),
6943
+ {
6944
+ method: req.method,
6945
+ headers: req.headers
6946
+ },
6947
+ (msg) => {
6948
+ msg.on(
6949
+ "data",
6950
+ (data) => chunks.push(data)
6951
+ );
6952
+ msg.on("end", () => {
6953
+ const content = Buffer.concat(
6954
+ chunks
6955
+ ).toString("utf8");
6956
+ resolve2({
6957
+ statusCode: msg.statusCode,
6958
+ headers: msg.headers,
6959
+ content
6960
+ });
6961
+ });
6962
+ }
6963
+ ).on("error", reject).end();
6964
+ });
6965
+ const out = await server.transformIndexHtml(
6966
+ entryPathname || "/",
6967
+ phpResult.content,
6968
+ req.originalUrl
6943
6969
  );
6944
- res.end(out);
6970
+ res.writeHead(phpResult.statusCode || 200, {
6971
+ ...req.headers,
6972
+ ...phpResult.headers
6973
+ }).end(out);
6945
6974
  return;
6946
6975
  }
6947
6976
  }
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,53 +6890,74 @@ 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();
6899
+ }
6900
+ const requestUrl = url.pathname;
6901
+ if (url.pathname.endsWith("/")) {
6902
+ url.pathname += "index.php";
6893
6903
  }
6894
- requestUrl = requestUrl.substring(1);
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;
6902
- const phpResult = await new Promise(
6903
- (resolve2, reject) => {
6904
- const chunks = [];
6905
- http.request(
6906
- url.toString(),
6907
- {
6908
- method: req.method,
6909
- headers: req.headers
6910
- },
6911
- (msg) => {
6912
- msg.on(
6913
- "data",
6914
- (data) => chunks.push(data)
6915
- );
6916
- msg.on("end", () => {
6917
- const result = Buffer.concat(
6918
- chunks
6919
- ).toString("utf8");
6920
- resolve2(result);
6921
- });
6922
- }
6923
- ).on("error", reject).end();
6924
- }
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
6925
  );
6926
- let out = phpResult.toString();
6927
- out = await server.transformIndexHtml(
6928
- requestUrl || "/",
6929
- out
6926
+ const phpResult = await new Promise((resolve2, reject) => {
6927
+ const chunks = [];
6928
+ http.request(
6929
+ url.toString(),
6930
+ {
6931
+ method: req.method,
6932
+ headers: req.headers
6933
+ },
6934
+ (msg) => {
6935
+ msg.on(
6936
+ "data",
6937
+ (data) => chunks.push(data)
6938
+ );
6939
+ msg.on("end", () => {
6940
+ const content = Buffer.concat(
6941
+ chunks
6942
+ ).toString("utf8");
6943
+ resolve2({
6944
+ statusCode: msg.statusCode,
6945
+ headers: msg.headers,
6946
+ content
6947
+ });
6948
+ });
6949
+ }
6950
+ ).on("error", reject).end();
6951
+ });
6952
+ const out = await server.transformIndexHtml(
6953
+ entryPathname || "/",
6954
+ phpResult.content,
6955
+ req.originalUrl
6930
6956
  );
6931
- res.end(out);
6957
+ res.writeHead(phpResult.statusCode || 200, {
6958
+ ...req.headers,
6959
+ ...phpResult.headers
6960
+ }).end(out);
6932
6961
  return;
6933
6962
  }
6934
6963
  }
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
 
@@ -10,10 +22,11 @@ $source = str_replace(
10
22
  $source,
11
23
  );
12
24
 
13
- die((function ($__SOURCE) {
25
+ (function () {
14
26
  try {
15
- return eval("?> $__SOURCE <?php");
27
+ eval('?> ' . func_get_arg(0) . ' <?php');
28
+ die();
16
29
  } catch (\Throwable $th) {
17
- return $th->getMessage();
30
+ die($th->getMessage());
18
31
  }
19
- })($source));
32
+ })($source);
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.30",
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,14 @@
12
12
  "php",
13
13
  "php-loader",
14
14
  "php-compiler",
15
- "loader"
15
+ "php processing",
16
+ "php transpilation",
17
+ "php-vite",
18
+ "vite-php",
19
+ "loader",
20
+ "url rewrite",
21
+ "url router",
22
+ "mod_rewrite"
16
23
  ],
17
24
  "author": "Nikita 'donnikitos' Nitichevski <me@donnikitos.com> (https://donnikitos.com/)",
18
25
  "license": "MIT",