npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vite-plugin-php

v1.0.67

Published

Process PHP-files with the speed and tools of Vite

Downloads

680

Readme

vite-plugin-php

npm GitHub Repo stars GitHub GitHub last commit Issues

Use Vite's speed and tooling to process PHP-files!

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

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

Check out the starter repo for an easy and convenient start:

⚡ Latest changes

| Version | Feature | | ------- | ----------------------------------------------------------------------------------------------------------- | | 1.0.67 | Removed whitespaces from PHP responses in dev mode | | 1.0.66 | Fixed file monitoring on Windows | | 1.0.65 | Fixed request body forwarding for all request methods | | 1.0.62 | HTML transforms are now only applied to HTML contents during dev | | 1.0.60 | Fixed inline module transpiling -> PHP code is being properly inserted into transpiled inline module chunks | | 1.0.55 | Fixed pure PHP file processing | | 1.0.50 | Using native Rollup pipeline to generate bundle -> proper error messages during build | | ... | ... |

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>
		<?="Render some text with PHP!";?>

		<?php if(isset($_GET['dont_load'])) { ?>
			<script src="./src/some_script.js" type="module"></script>
		<?php } ?>
	</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;
	cleanup?: {
		dev?: boolean;
		build?: boolean;
	};
};

By default the plugin is trying to access the system php-binary and load the index.php file as the main entry point. 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', 'about.php', '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 | | ... | ... | ... |

Since version 1.0.6 you can 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!

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>

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.