PHPbuild

Make PHP apps faster!

Getting Started

First, you need to install node.js and npm. Then install pnpm.

$ npm install -g pnpm

Now, create a new directory and navigate to it.

$ mkdir my-app
$ cd my-app

Clone the repository.

$ git clone https://github.com/Posandu/phpbuild.git .

Install the dependencies.

$ pnpm install

Now open 2 terminals and navigate to:

my-app/
my-app/dist/

In the first terminal, run:

pnpm run dev

In the second terminal, run the following command to start the php server:

php -S 0.0.0.0:8624

Now open http://localhost:8624/ in your browser. You should see the following:

image

If you see the following, you have successfully installed PHPbuild. Now try editing the index.php file and see what happens. The page should be reloaded automatically.

Commands

pnpm run dev

Run the build process and hot reload process.

pnpm run build

Build the project. All the build files will be generated in the build directory.

PHP API

Now if you go to the src/php directory, you should see the following:

📦php
 ┣ 📜index.php // The main file
 ┗ 📜utils.php // The utils file (This is generated by PHPbuild)

require_once 'utils.php'; // We require the utils file

//

js_file("index"); // include a javascript file (src/js/index.js)
scss_file("index"); // include a scss file (src/scss/index.scss)
hot_reload(); // Hot reload the page

js_file($filename : string)

Include a javascript file. $filename is the name of the file without the extension. It will be searched in the src/js directory. For example if we have a directory like this:

src/js/
┣ index.js
┣ _ignored.js
┣ index.notjs

Then we can use js_file("index") to include the index.js file. All files starting with _ will be ignored. And if we try to include a file that doesn’t exist, we will get an error.

scss_file($filename : string)

Include a scss file. $filename is the name of the file without the extension. It will be searched in the src/scss directory. For example if we have a directory like this:

src/scss/
┣ index.scss
┣ _ignored.scss
┣ index.notscss

Then we can use scss_file("index") to include the index.scss file. All files starting with _ will be ignored. And if we try to include a file that doesn’t exist, we will get an error.

Debugging

Hot reload failed. Please make sure you have a server running on port 5426.

image

Make sure to run pnpm run dev in the project directory.

Contributing

This project is open source and is hosted on Github. If you have any problems or suggestions, please open an issue or create a pull request.

GitHub

View Github