Skip to content

donnikitos/vite-plugin-php

Repository files navigation

vite-plugin-php logo

License
NPM GitHub Stars Issues Last Commit


Use Vite's speed and tooling to work with PHP!

// vite.config.js
import { defineConfig } from 'vite';
import usePHP from 'vite-plugin-php';

export default defineConfig({
	plugins: [usePHP()],
});

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.

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

<!-- index.php -->
<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=edge" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
	</head>
	<body>
		<div id="root">
			<?="Render some text with PHP!"; ?>
		</div>

		<?php if(isset($_GET['show_hello'])): ?>
			Hello world!
		<?php endif; ?>

		<script src="./src/react-app.tsx" type="module"></script>
	</body>
</html>

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:

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:

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:

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:

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:

// 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.

Specific oddities

Inline modules

⚠️ PHP will work somehow unintuitive in inlined modules. E.g. you have a page with some variables:

<?php
$var = 'foo';
?>

<script type="module">
	console.log('<?=$var; ?>');
</script>

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:

<script src="./src/<?='dynamic_script_name'; ?>.js" type="module"></script>

Conditional script and style loading

The plugin won't be able to retain the position of some asset tags like <script> and <link>.

<?php if($some_condition$) { ?>
	<script src="./src/some_script.js" type="module"></script>
<?php } ?>

Vite processes these independently and merges/ splits them dynamically.
These will be attached to the <head> 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.

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.

via Ko-Fi Buy me a coffee via PayPal
Ko-Fi Buy Me A Coffee PayPal