Skip to content

Commit 7d70cb7

Browse files
committed
add docs
1 parent 940949f commit 7d70cb7

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

README.md

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# PHP-Vite
2+
3+
[![PHP Version](https://img.shields.io/badge/php-8.1%2B-blue.svg)](https://packagist.org/packages/mindplay/php-vite)
4+
[![License](https://img.shields.io/badge/license-MS--RL-green)](https://opensource.org/license/ms-rl-html)
5+
6+
This library provides a lightweight [backend integration](https://vitejs.dev/guide/backend-integration.html)
7+
for your PHP-based MPA, SPA or PWA based on [Vite](https://vitejs.dev/).
8+
9+
It parses the [build manifest](https://vitejs.dev/config/build-options#build-manifest) (the `.vite/manifest.json` file)
10+
and produces the required `<script>` and `<link>` tags to load (and preload) scripts, CSS files, and other assets.
11+
12+
## Basic Usage
13+
14+
A commented MPA example is available [here](https://github.com/mindplay-dk/php-vite-mpa) -
15+
please refer to this for examples of configuring Vite, NPM, TypeScript and Composer.
16+
17+
In the following steps, we'll cover usage of the library API only.
18+
19+
#### 1. Load the `manifest.json` file created by Vite:
20+
21+
```php
22+
$vite = new Manifest(
23+
manifest_path: $your_root_dir . '/public/dist/.vite/manifest.json',
24+
base_path: '/dist/',
25+
dev: false
26+
);
27+
```
28+
29+
Teh `manifest_path` points to the Vite `manifest.json` file created for the production build.
30+
31+
In this example, `dev` is `false`, so we'll be creating tags for the production assets.
32+
33+
The `base_path` 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.
34+
35+
Note that, in development mode (when `dev` set to `false`) the `manifest.json` file is unused, and not required.
36+
37+
> 💡 *For a detailed description of the constructor arguments, please refer to the `Manifest` constructor argument doc-blocks.*
38+
39+
#### 2. Create the `Tags` for an entry point script:
40+
41+
```php
42+
$tags = $vite->createTags("index.ts");
43+
```
44+
45+
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.
46+
47+
#### 3. Emit from `Tags` in your HTML template:
48+
49+
Your `Tags` instance contains the `preload` and `css` tags, which should be emitted in
50+
your `<head>` tag, as well as the `js` tags, which should be emitted immediately before
51+
the `</body>` end tag.
52+
53+
For example:
54+
55+
```html
56+
<!DOCTYPE html>
57+
<html lang="en">
58+
<head>
59+
<title>Vite App</title>
60+
<link rel="icon" href="<?= $vite->getURL("php.svg") ?>" />
61+
<?= $tags->preload ?>
62+
<?= $tags->css ?>
63+
</head>
64+
<body>
65+
<div id="app"></div>
66+
<?= $tags->js ?>
67+
</body>
68+
</html>
69+
```
70+
71+
## Preloading Assets
72+
73+
The service preloads any statically imported scripts and CSS files by default.
74+
75+
In addition, you can configure it to preload other statically imported assets as well -
76+
for convenience, there are two methods to automatically configure preloading of all
77+
common image and font asset types:
78+
79+
```php
80+
$manifest->preloadImages();
81+
$manifest->preloadFonts();
82+
```
83+
84+
You can also configure it to preload any other asset types - for example, to configure
85+
preloading of `.json` assets, you could add the following:
86+
87+
```php
88+
$manifest->preload(
89+
ext: "json",
90+
mime_type: "application/json",
91+
preload_as: "fetch"
92+
);
93+
```
94+
95+
Then create your tags as covered in the documentation above.
96+
97+
## Creating URLs
98+
99+
For advanced use-cases, you can also directly get the URL for an asset published by Vite:
100+
101+
```php
102+
$my_url = $manifest->getURL("consent-banner.ts");
103+
```
104+
105+
You can use this feature to, for example:
106+
107+
* Create your own custom preload tags (e.g. with media-queries)
108+
* Conditionally load a script based on user interactions or user state, etc.

0 commit comments

Comments
 (0)