Skip to content

Commit a622f2a

Browse files
authored
2.0.0 Release (#51)
* improved exit * renamed variable * use shared config * ignore everything in temp dir * plugin rebuild * no need for unescaping code * add temp dir to include path * added path resolves and moved internalParam * implemented php stdout logging * updated import structure * fixed console hijacker * improved shutdown * added timestamp to console logs * added error level configuration + updated config type * moved exit hook * made function synchronous * improved assets injection * rewrote php code functions into class * renamed php server holder * improved regexp * updated version * updated readme * updated styling * added paragraph about error logging * updated styling * fixed formatting
1 parent 69f415e commit a622f2a

25 files changed

Lines changed: 1227 additions & 594 deletions

README.md

Lines changed: 118 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -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!
719

820
```js
921
// vite.config.js
@@ -15,23 +27,28 @@ export default defineConfig({
1527
});
1628
```
1729

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>
2035

2136
## ⚡ Latest changes
2237

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+
| ... | ... |
3552

3653
## Write some PHP code in your `index.php`
3754

@@ -45,11 +62,15 @@ Check out the [starter repo](https://github.com/nititech/php-vite-starter) for a
4562
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
4663
</head>
4764
<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; ?>
4972

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>
5374
</body>
5475
</html>
5576
```
@@ -66,20 +87,44 @@ type UsePHPConfig = {
6687
entry?: string | string[];
6788
rewriteUrl?: (requestUrl: URL) => URL | undefined;
6889
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;
7294
};
7395
};
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+
};
74116
```
75117

76118
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+
77122
However you have the possibility to use an other binary or even compile multiple entry-points:
78123

79124
```js
80125
usePHP({
81126
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'],
83128
});
84129
```
85130

@@ -94,7 +139,7 @@ Should you have multiple entry-points, you will be able to access each one accor
94139
| shop/index.php | `/shop/` `/shop/index.php` | `shop/index.php` |
95140
| ... | ... | ... |
96141

97-
Since version 1.0.6 you can specify wildcard entry points:
142+
You can also specify wildcard entry points:
98143

99144
```js
100145
usePHP({
@@ -111,7 +156,7 @@ usePHP({
111156

112157
These entries will also render according to the routing table above.
113158

114-
##### Rewrite urls
159+
#### Rewrite urls
115160

116161
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.
117162
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({
134179

135180
⚠️ **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!
136181

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+
137206
## Specific oddities
138207

139208
#### Inline modules
@@ -159,9 +228,27 @@ Same applies to other server variables like `$_GET`, `$_POST` and so on - they w
159228
Vite won't be able to process PHP-computed styles, scripts or images:
160229

161230
```php
162-
<script src="./src/<?='dynamic_script_name';?>.js" type="module"></script>
231+
<script src="./src/<?='dynamic_script_name'; ?>.js" type="module"></script>
163232
```
164233

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+
165252
## Issues
166253

167254
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).

0 commit comments

Comments
 (0)