PHPackages                             ngekoding/php-vite - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. ngekoding/php-vite

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

ngekoding/php-vite
==================

a lightweight PHP-backend integration for the Vite frontend build tool

v1.0.0(10mo ago)00MPL-2.0PHPPHP &gt;=5.6CI passing

Since Jun 24Pushed 10mo agoCompare

[ Source](https://github.com/ngekoding/php-vite)[ Packagist](https://packagist.org/packages/ngekoding/php-vite)[ RSS](/packages/ngekoding-php-vite/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

php-vite
========

[](#php-vite)

[![PHP Version](https://camo.githubusercontent.com/1278457af1bbb903975b375013027563a64190960e3bf2d057f697afcdd1be8b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d352e362532422d626c75652e737667)](https://packagist.org/packages/ngekoding/php-vite)[![Build Status](https://github.com/mindplay-dk/php-vite/actions/workflows/ci.yml/badge.svg)](https://github.com/ngekoding/php-vite/actions/workflows/ci.yml)[![License](https://camo.githubusercontent.com/0a34271b130f15aa109c3625a51302a3c6b584e3e1df0d19cd53b9741ac02dd5/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d504c2d2d322e302d677265656e)](https://opensource.org/license/mpl-2-0)

This library provides a lightweight [backend integration](https://vitejs.dev/guide/backend-integration.html) for your PHP-based MPA, SPA, or PWA based on [Vite](https://vitejs.dev/).

It parses the [build manifest](https://vitejs.dev/config/build-options#build-manifest) (the `.vite/manifest.json` file) and produces the required `` and `` tags to load (and preload) scripts, CSS files, and other assets.

PHP 5.6 Compatible Version
--------------------------

[](#php-56-compatible-version)

> ⚠️ This is a customized fork of the original [`mindplay-dk/php-vite`](https://github.com/mindplay-dk/php-vite) package.

This version has been adapted to support **PHP 5.6 and above**, making it suitable for legacy projects that are still running on older PHP versions.

### Key differences from the original:

[](#key-differences-from-the-original)

- Rewritten to remove syntax and language features that require PHP 8.1+
- Compatible with PHP 5.6, 7.x, and newer
- Namespaces changed to `Ngekoding\Vite` to distinguish from the original package
- Test suite migrated to use PHPUnit

If you're working on modern PHP (8.1+), you are encouraged to use the original package: 👉 [mindplay-dk/php-vite](https://github.com/mindplay-dk/php-vite)

Basic Usage
-----------

[](#basic-usage)

A commented MPA example is available [here](https://github.com/mindplay-dk/php-vite-mpa) - please refer to this for examples of configuring Vite, NPM, TypeScript, and Composer.

In the following steps, we'll cover usage of the library API only.

#### 1. Load the `manifest.json` file created by Vite:

[](#1-load-the-manifestjson-file-created-by-vite)

```
use Ngekoding\Vite\Manifest;

$vite = new Manifest(
    false,                      // dev mode (true = development, false = production)
    '/path/to/manifest.json',   // path to manifest.json
    '/dist/'                    // public base path
);
```

The constructor accepts three parameters:

1. `dev` – set to `true` for development mode, or `false` for production mode.
2. `manifestPath` – points to the Vite `manifest.json` file created for the production build.
3. `basePath` – is relative to your public web root, it is the root folder from which Vite's production assets are served, and/or the root folder from which Vite serves assets dynamically in development mode.

Note that, in development mode (when `dev` is set to `true`) the `manifest.json` file is unused, and not required.

> 💡 *For a detailed description of the constructor arguments, please refer to the `Manifest` constructor argument doc-blocks.*

#### 2. Create the `Tags` for an entry point script:

[](#2-create-the-tags-for-an-entry-point-script)

```
$tags = $vite->createTags("index.ts");
```

Your entry point scripts are defined in Vite's [`build.rollupOptions`](https://vitejs.dev/config/build-options#build-rollupoptions) using RollUp's [`input`](https://rollupjs.org/configuration-options/#input) setting.

Note that, if you have **multiple entry point scripts** on **the same page**, you should pass them in a *single* call - for example:

```
$tags = $vite->createTags("index.ts", "consent-banner.ts");
```

Making multiple calls for different entry points *may* result in duplicate tags for any shared static imports - you will most likely need just *one* instance of `Tags` on a single page.

#### 3. Emit from `Tags` in your HTML template:

[](#3-emit-from-tags-in-your-html-template)

Your `Tags` instance contains the preload and CSS tags, which should be emitted in your `` tag, as well as the `js` tags, which should be emitted immediately before the `` end tag.

For example:

```
>

    Vite App
