vite-plugin-php 1.0.71 → 2.0.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/README.md CHANGED
@@ -1,9 +1,21 @@
1
- # vite-plugin-php
2
-
3
- [![npm](https://img.shields.io/npm/dt/vite-plugin-php?style=for-the-badge)](https://www.npmjs.com/package/vite-plugin-php) [![GitHub Repo stars](https://img.shields.io/github/stars/donnikitos/vite-plugin-php?label=GitHub%20Stars&style=for-the-badge)](https://github.com/donnikitos/vite-plugin-php) [![GitHub](https://img.shields.io/github/license/donnikitos/vite-plugin-php?color=blue&style=for-the-badge)](https://github.com/donnikitos/vite-plugin-php/blob/master/LICENSE)
4
- ![GitHub last commit](https://img.shields.io/github/last-commit/donnikitos/vite-plugin-php?style=for-the-badge) [![Issues](https://img.shields.io/github/issues/donnikitos/vite-plugin-php?style=for-the-badge)](https://github.com/donnikitos/vite-plugin-php/issues)
5
-
6
- Use Vite's speed and tooling to process PHP-files!
1
+ <p align="center" style="text-align: center;">
2
+ <img
3
+ src="https://vite-php.nititech.de/assets/vite-php.logo.svg"
4
+ alt="vite-plugin-php logo"
5
+ style="width: 250px; max-width: 100%;" />
6
+ </p>
7
+
8
+ <div style="display: grid; grid-template-columns: max-content max-content; column-gap: 10px;">
9
+ <a href="https://github.com/donnikitos/vite-plugin-php/blob/master/LICENSE" target="_blank"><img src="https://img.shields.io/github/license/donnikitos/vite-plugin-php?color=blue&style=for-the-badge" alt="License" /></a>
10
+ <div></div>
11
+ <a href="https://www.npmjs.com/package/vite-plugin-php" target="_blank"><img src="https://img.shields.io/npm/dt/vite-plugin-php?style=for-the-badge" alt="NPM" /></a>
12
+ <a href="https://github.com/donnikitos/vite-plugin-php" target="_blank"><img src="https://img.shields.io/github/stars/donnikitos/vite-plugin-php?label=GitHub%20Stars&style=for-the-badge" alt="GitHub Stars" /></a>
13
+ <a href="https://github.com/donnikitos/vite-plugin-php/issues" target="_blank"><img src="https://img.shields.io/github/issues/donnikitos/vite-plugin-php?style=for-the-badge" alt="Issues" /></a>
14
+ <img src="https://img.shields.io/github/last-commit/donnikitos/vite-plugin-php?style=for-the-badge" alt="Last Commit" />
15
+ </div>
16
+
17
+ \
18
+ Use Vite's speed and tooling to work with PHP!
7
19
 
8
20
  ```js
9
21
  // vite.config.js
@@ -15,23 +27,28 @@ export default defineConfig({
15
27
  });
16
28
  ```
17
29
 
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>
30
+ <p align="center" style="text-align: center;">
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>
33
+ </b>
34
+ </p>
20
35
 
21
36
  ## ⚡ Latest changes
22
37
 
23
- | Version | Feature |
24
- | ------- | ----------------------------------------------------------------------------------------------------------- |
25
- | 1.0.71 | Fixed assets prepending for namespaced PHP-files |
26
- | 1.0.70 | Added include path override for relative PHP imports in dev mode |
27
- | 1.0.69 | Using new token format to escape PHP in HTML |
28
- | 1.0.68 | Improved transpiled code evaluation (removed native `eval()`) |
29
- | 1.0.67 | Removed whitespaces from PHP responses in dev mode |
30
- | 1.0.66 | Fixed file monitoring on Windows |
31
- | 1.0.65 | Fixed request body forwarding for all request methods |
32
- | 1.0.62 | HTML transforms are now only applied to HTML contents during dev |
33
- | 1.0.60 | Fixed inline module transpiling -> PHP code is being properly inserted into transpiled inline module chunks |
34
- | ... | ... |
38
+ ##### Major Release 2.0.0 !!!
39
+
40
+ Including full _PHP error logging_ into console, rewritten code for _better performance_, bug fixes, etc.\
41
+ [See changelog](https://vite-php.nititech.de/changelog).
42
+
43
+ ##### Releases >= 1.0.0
44
+
45
+ | Version | Feature |
46
+ | ------- | ---------------------------------------------------------------- |
47
+ | 1.0.71 | Fixed assets prepending for namespaced PHP-files |
48
+ | 1.0.70 | Added include path override for relative PHP imports in dev mode |
49
+ | 1.0.69 | Using new token format to escape PHP in HTML |
50
+ | 1.0.68 | Improved transpiled code evaluation (removed native `eval()`) |
51
+ | ... | ... |
35
52
 
36
53
  ## Write some PHP code in your `index.php`
37
54
 
@@ -45,11 +62,15 @@ Check out the [starter repo](https://github.com/nititech/php-vite-starter) for a
45
62
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
46
63
  </head>
47
64
  <body>
48
- <?="Render some text with PHP!";?>
65
+ <div id="root">
66
+ <?="Render some text with PHP!"; ?>
67
+ </div>
68
+
69
+ <?php if(isset($_GET['show_hello'])): ?>
70
+ Hello world!
71
+ <?php endif; ?>
49
72
 
50
- <?php if(isset($_GET['dont_load'])) { ?>
51
- <script src="./src/some_script.js" type="module"></script>
52
- <?php } ?>
73
+ <script src="./src/react-app.tsx" type="module"></script>
53
74
  </body>
54
75
  </html>
55
76
  ```
@@ -66,20 +87,44 @@ type UsePHPConfig = {
66
87
  entry?: string | string[];
67
88
  rewriteUrl?: (requestUrl: URL) => URL | undefined;
68
89
  tempDir?: string;
69
- cleanup?: {
70
- dev?: boolean;
71
- build?: boolean;
90
+ dev?: {
91
+ // Takes on either a bitmask, or named constants EPHPError
92
+ errorLevels?: number;
93
+ cleanup?: boolean;
72
94
  };
73
95
  };
96
+
97
+ // Detailed description on https://www.php.net/manual/en/errorfunc.constants.php
98
+ const EPHPError = {
99
+ ERROR: 1,
100
+ WARNING: 2,
101
+ PARSE: 4,
102
+ NOTICE: 8,
103
+ CORE_ERROR: 16,
104
+ CORE_WARNING: 32,
105
+ COMPILE_ERROR: 64,
106
+ COMPILE_WARNING: 128,
107
+ USER_ERROR: 256,
108
+ USER_WARNING: 512,
109
+ USER_NOTICE: 1024,
110
+ STRICT: 2048,
111
+ RECOVERABLE_ERROR: 4096,
112
+ DEPRECATED: 8192,
113
+ USER_DEPRECATED: 16384,
114
+ ALL: 32767,
115
+ };
74
116
  ```
75
117
 
76
118
  By default the plugin is trying to access the system `php`-binary and load the `index.php` file as the main entry point.
119
+
120
+ #### Alternative entry points
121
+
77
122
  However you have the possibility to use an other binary or even compile multiple entry-points:
78
123
 
79
124
  ```js
80
125
  usePHP({
81
126
  binary: '/opt/lampp/bin/php-8.1.10',
82
- entry: ['index.php', 'about.php', 'contact.php'],
127
+ entry: ['index.php', 'index_alt.php', 'pages/contact.php'],
83
128
  });
84
129
  ```
85
130
 
@@ -94,7 +139,7 @@ Should you have multiple entry-points, you will be able to access each one accor
94
139
  | shop/index.php | `/shop/` `/shop/index.php` | `shop/index.php` |
95
140
  | ... | ... | ... |
96
141
 
97
- Since version 1.0.6 you can specify wildcard entry points:
142
+ You can also specify wildcard entry points:
98
143
 
99
144
  ```js
100
145
  usePHP({
@@ -111,7 +156,7 @@ usePHP({
111
156
 
112
157
  These entries will also render according to the routing table above.
113
158
 
114
- ##### Rewrite urls
159
+ #### Rewrite urls
115
160
 
116
161
  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.
117
162
  The rewriteUrl function has one parameter - the requested URL given as URL object - and return either a modified URL object or undefined:
@@ -134,6 +179,30 @@ usePHP({
134
179
 
135
180
  ⚠️ **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!
136
181
 
182
+ #### Error logging
183
+
184
+ Just like in native PHP you can specify what errors you want to see:
185
+
186
+ ```js
187
+ // vite.config.js
188
+ import { defineConfig } from 'vite';
189
+ import usePHP, { EPHPError } from 'vite-plugin-php';
190
+
191
+ export default defineConfig({
192
+ plugins: [
193
+ usePHP({
194
+ dev: {
195
+ errorLevels:
196
+ EPHPError.ERROR | EPHPError.WARNING | EPHPError.STRICT,
197
+ },
198
+ }),
199
+ ],
200
+ });
201
+ ```
202
+
203
+ This log will be printed into your console, just like any other message about what is happening in Vite.\
204
+ For more details about the meaning of the error level constants, visit the original [PHP-documentation](https://www.php.net/manual/en/errorfunc.constants.php).
205
+
137
206
  ## Specific oddities
138
207
 
139
208
  #### Inline modules
@@ -159,9 +228,27 @@ Same applies to other server variables like `$_GET`, `$_POST` and so on - they w
159
228
  Vite won't be able to process PHP-computed styles, scripts or images:
160
229
 
161
230
  ```php
162
- <script src="./src/<?='dynamic_script_name';?>.js" type="module"></script>
231
+ <script src="./src/<?='dynamic_script_name'; ?>.js" type="module"></script>
163
232
  ```
164
233
 
234
+ #### Conditional script and style loading
235
+
236
+ The plugin won't be able to retain the position of some asset tags like `<script>` and `<link>`.
237
+
238
+ ```php
239
+ <?php if($some_condition$) { ?>
240
+ <script src="./src/some_script.js" type="module"></script>
241
+ <?php } ?>
242
+ ```
243
+
244
+ Vite processes these independently and merges/ splits them dynamically.\
245
+ These will be attached to the `<head>` tag or put right in the beginning of the file.
246
+
247
+ If the file contains a PHP **namespace** the assets will be either\
248
+ a) placed after the last closed tag\
249
+ b) placed right before the last `<?` tag\
250
+ c) placed at the end of the file
251
+
165
252
  ## Issues
166
253
 
167
254
  If you encounter any other bugs or need some other features feel free to open an [issue](https://github.com/donnikitos/vite-plugin-php/issues).