vite-plugin-php 1.0.651 → 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,22 +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.65 | Fixed request body forwarding for all request methods |
26
- | 1.0.62 | HTML transforms are now only applied to HTML contents during dev |
27
- | 1.0.60 | Fixed inline module transpiling -> PHP code is being properly inserted into transpiled inline module chunks |
28
- | 1.0.55 | Fixed pure PHP file processing |
29
- | 1.0.50 | Using native Rollup pipeline to generate bundle -> proper error messages during build |
30
- | 1.0.40 | Vite's "HTML Env Replacement" feature in transpiled PHP files |
31
- | 1.0.30 | Proper PHP header forwarding during development |
32
- | 1.0.20 | URL rewrite functionality to mimic mod_rewrite & friends |
33
- | 1.0.11 | Improved Windows support |
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
+ | ... | ... |
34
52
 
35
53
  ## Write some PHP code in your `index.php`
36
54
 
@@ -44,11 +62,15 @@ Check out the [starter repo](https://github.com/nititech/php-vite-starter) for a
44
62
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
45
63
  </head>
46
64
  <body>
47
- <?="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; ?>
48
72
 
49
- <?php if(isset($_GET['dont_load'])) { ?>
50
- <script src="./src/some_script.js" type="module"></script>
51
- <?php } ?>
73
+ <script src="./src/react-app.tsx" type="module"></script>
52
74
  </body>
53
75
  </html>
54
76
  ```
@@ -65,20 +87,44 @@ type UsePHPConfig = {
65
87
  entry?: string | string[];
66
88
  rewriteUrl?: (requestUrl: URL) => URL | undefined;
67
89
  tempDir?: string;
68
- cleanup?: {
69
- dev?: boolean;
70
- build?: boolean;
90
+ dev?: {
91
+ // Takes on either a bitmask, or named constants EPHPError
92
+ errorLevels?: number;
93
+ cleanup?: boolean;
71
94
  };
72
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
+ };
73
116
  ```
74
117
 
75
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
+
76
122
  However you have the possibility to use an other binary or even compile multiple entry-points:
77
123
 
78
124
  ```js
79
125
  usePHP({
80
126
  binary: '/opt/lampp/bin/php-8.1.10',
81
- entry: ['index.php', 'about.php', 'contact.php'],
127
+ entry: ['index.php', 'index_alt.php', 'pages/contact.php'],
82
128
  });
83
129
  ```
84
130
 
@@ -93,7 +139,7 @@ Should you have multiple entry-points, you will be able to access each one accor
93
139
  | shop/index.php | `/shop/` `/shop/index.php` | `shop/index.php` |
94
140
  | ... | ... | ... |
95
141
 
96
- Since version 1.0.6 you can specify wildcard entry points:
142
+ You can also specify wildcard entry points:
97
143
 
98
144
  ```js
99
145
  usePHP({
@@ -110,7 +156,7 @@ usePHP({
110
156
 
111
157
  These entries will also render according to the routing table above.
112
158
 
113
- ##### Rewrite urls
159
+ #### Rewrite urls
114
160
 
115
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.
116
162
  The rewriteUrl function has one parameter - the requested URL given as URL object - and return either a modified URL object or undefined:
@@ -133,6 +179,30 @@ usePHP({
133
179
 
134
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!
135
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
+
136
206
  ## Specific oddities
137
207
 
138
208
  #### Inline modules
@@ -158,9 +228,27 @@ Same applies to other server variables like `$_GET`, `$_POST` and so on - they w
158
228
  Vite won't be able to process PHP-computed styles, scripts or images:
159
229
 
160
230
  ```php
161
- <script src="./src/<?='dynamic_script_name';?>.js" type="module"></script>
231
+ <script src="./src/<?='dynamic_script_name'; ?>.js" type="module"></script>
162
232
  ```
163
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
+
164
252
  ## Issues
165
253
 
166
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).