https://github.com/donnikitos/vite-plugin-php
Vite's speed and tooling to preprocess PHP-files!
https://github.com/donnikitos/vite-plugin-php
asset-bundling asset-management bundler bundler-plugin php php-loader transpiler vite vite-loader vite-php vite-plugin vitejs
Last synced: 13 days ago
JSON representation
Vite's speed and tooling to preprocess PHP-files!
- Host: GitHub
- URL: https://github.com/donnikitos/vite-plugin-php
- Owner: donnikitos
- License: mit
- Created: 2023-03-13T11:46:48.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2025-03-04T10:00:18.000Z (about 2 months ago)
- Last Synced: 2025-03-30T17:08:22.579Z (21 days ago)
- Topics: asset-bundling, asset-management, bundler, bundler-plugin, php, php-loader, transpiler, vite, vite-loader, vite-php, vite-plugin, vitejs
- Language: TypeScript
- Homepage: https://vite-php.nititech.de/
- Size: 224 KB
- Stars: 55
- Watchers: 2
- Forks: 3
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
- fucking-awesome-vite - vite-plugin-php - Load and process PHP-entry files instead of default index.html. (Plugins / Framework-agnostic Plugins)
- awesome-vite - vite-plugin-php - Load and process PHP-entry files instead of default index.html. (Plugins / Framework-agnostic Plugins)
README
![]()
\
Use Vite's speed and tooling to work with PHP!```js
// vite.config.js
import { defineConfig } from 'vite';
import usePHP from 'vite-plugin-php';export default defineConfig({
plugins: [usePHP()],
});
```
NPM | Wiki | Discussions | Starter-Repo
## ⚡ Latest changes
##### Major Release 2.0.0 !!!
Including full _PHP error logging_ into console, rewritten code for _better performance_, bug fixes, etc.\
[See changelog](https://vite-php.nititech.de/changelog).##### Releases >= 1.0.0
| Version | Feature |
| ------- | ---------------------------------------------------------------- |
| 1.0.71 | Fixed assets prepending for namespaced PHP-files |
| 1.0.70 | Added include path override for relative PHP imports in dev mode |
| 1.0.69 | Using new token format to escape PHP in HTML |
| 1.0.68 | Improved transpiled code evaluation (removed native `eval()`) |
| ... | ... |## Write some PHP code in your `index.php`
```php
="Render some text with PHP!"; ?>
Hello world!
```
The plugin will serve you the processed `index.php` as usual, including all imported and preprocessed files that are supported by Vite and other loaders.
## Configuration
The configuration takes following properties:
```ts
type UsePHPConfig = {
binary?: string;
entry?: string | string[];
rewriteUrl?: (requestUrl: URL) => URL | undefined;
tempDir?: string;
dev?: {
// Takes on either a bitmask, or named constants EPHPError
errorLevels?: number;
cleanup?: boolean;
};
};// Detailed description on https://www.php.net/manual/en/errorfunc.constants.php
const EPHPError = {
ERROR: 1,
WARNING: 2,
PARSE: 4,
NOTICE: 8,
CORE_ERROR: 16,
CORE_WARNING: 32,
COMPILE_ERROR: 64,
COMPILE_WARNING: 128,
USER_ERROR: 256,
USER_WARNING: 512,
USER_NOTICE: 1024,
STRICT: 2048,
RECOVERABLE_ERROR: 4096,
DEPRECATED: 8192,
USER_DEPRECATED: 16384,
ALL: 32767,
};
```By default the plugin is trying to access the system `php`-binary and load the `index.php` file as the main entry point.
#### Alternative entry points
However you have the possibility to use an other binary or even compile multiple entry-points:
```js
usePHP({
binary: '/opt/lampp/bin/php-8.1.10',
entry: ['index.php', 'index_alt.php', 'pages/contact.php'],
});
```Should you have multiple entry-points, you will be able to access each one according to this chart:
| Entry file | Accessible routes | Build file |
| ----------------- | ------------------------------------- | ------------------- |
| index.php | `/` `/index` `/index.php` | `index.php` |
| about.php | `/about` `/about.php` | `about.php` |
| about/details.php | `/about/details` `/about/details.php` | `about/details.php` |
| contact.php | `/contact` `/contact.php` | `contact.php` |
| shop/index.php | `/shop/` `/shop/index.php` | `shop/index.php` |
| ... | ... | ... |You can also specify wildcard entry points:
```js
usePHP({
binary: '/opt/lampp/bin/php-8.1.10',
entry: [
'index.php',
'about.php',
'contact.php',
'pages/**/*.php',
'partials/*.php',
],
});
```These entries will also render according to the routing table above.
#### Rewrite urls
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.
The rewriteUrl function has one parameter - the requested URL given as URL object - and return either a modified URL object or undefined:```js
usePHP({
entry: ['index.php', 'partials/**/*.php'],
rewriteUrl(requestUrl) {
if (['.js', '.css'].some((s) => requestUrl.pathname.includes(s))) {
return;
}requestUrl.search = '_request_=' + requestUrl.pathname;
requestUrl.pathname = 'index.php';return requestUrl;
},
});
```⚠️ **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!
#### Error logging
Just like in native PHP you can specify what errors you want to see:
```js
// vite.config.js
import { defineConfig } from 'vite';
import usePHP, { EPHPError } from 'vite-plugin-php';export default defineConfig({
plugins: [
usePHP({
dev: {
errorLevels:
EPHPError.ERROR | EPHPError.WARNING | EPHPError.STRICT,
},
}),
],
});
```This log will be printed into your console, just like any other message about what is happening in Vite.\
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).## Specific oddities
#### Inline modules
⚠️ PHP will work somehow unintuitive in inlined modules.
E.g. you have a page with some variables:```php
console.log('<?=$var; ?>');
```
This will not work. `$var` will be undefined in the module since the script is being transpiled into a separate file and included separately.
Same applies to other server variables like `$_GET`, `$_POST` and so on - they will not have the same value as the main PHP file.#### Dynamically included asset processing
Vite won't be able to process PHP-computed styles, scripts or images:
```php
```
#### Conditional script and style loading
The plugin won't be able to retain the position of some asset tags like `` and `<link>`.
```php
<?php if($some_condition$) { ?>
<script src="./src/some_script.js" type="module">```
Vite processes these independently and merges/ splits them dynamically.\
These will be attached to the `` tag or put right in the beginning of the file.If the file contains a PHP **namespace** the assets will be either\
a) placed after the last closed tag\
b) placed right before the last `` tag\
c) placed at the end of the file## Issues
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).
## Support
Love open source? Enjoying my project?\
Your support can keep the momentum going! Consider a donation to fuel the creation of more innovative open source software.