vite-php-asset-callers
v0.2.6
Published
A Vite 6+ plugin that scans PHP code and emits asset files from PHP callers/functions.
Downloads
8
Maintainers
Readme
Features
- The plugin scans PHP code for asset references, such as images or external files, and identifies them for bundling.
- Allows customization of file extensions, asset paths, and parser options.
- Prevents emitting duplicate assets by keeping track of previously emitted files.
- It supports partial matching of file paths, ensuring assets are correctly identified within PHP .
Installation
Install it via npm:
npm install vite-php-asset-callers --save-devOr with yarn:
yarn add -D vite-php-asset-callersUsage
Add the plugin to your vite.config.js file:
import { defineConfig } from 'vite';
import VitePhpAssetCallers from 'vite-php-asset-callers';
export default defineConfig({
plugins: [VitePhpAssetCallers()],
});or with plugin options (optional)
import { defineConfig } from 'vite';
import VitePhpAssetCallers from 'vite-php-asset-callers';
export default defineConfig({
plugins: [
VitePhpAssetCallers({
assetPath: 'src/images',
extensions: ['png', 'jpg', 'jpeg', 'gif', 'webp', 'woff', 'woff2', 'svg'],
phpFiles: ['my-absolute-path/my-class.php', 'my-absolute-path/other-classes/**/*.php'],
}),
],
});Options
| Option | Type | Description |
| --------------- | ---------- | ---------------------------------------------------------------------------------------------------------------------------------- |
| assetPath | string | Relative path from the root where assets are located to match in PHP files, by default it will use Vite's root path. |
| phpFiles | string[] | An array of PHP files or glob patterns to scan in addition to PHP entries. These files will also be watched in HMR and watch mode. |
| extensions | string[] | Asset extensions to search for in PHP code (default: ['png', 'jpg', 'jpeg', 'gif', 'webp', 'woff', 'woff2', 'svg']). |
| parserOptions | Object | Options passed directly to the php-parser. |
How It Works
- The plugin automatically scans PHP entries and the specified PHP files in
phpFiles. - It resolves paths to asset files based on the arguments in PHP function calls.
- Matches assets based in Vite's root or on the provided
assetPathandextensions. - Emits the matched assets into the Vite output directory for compilation.
Example
Given the following folder structure for source files.
src/
└── assets/
├── svg/
│ └── coffee.svg
├── images/
│ └── logo.png
└── fonts/
└── arial.woffGiven the following PHP code with different type of callers:
<?php
echo getImage('logo.png');
echo Utils::getSvg('coffee.svg');
echo $fonts->getFont($someValue, 'arial.woff')The plugin will:
- Locate all asset files in the root (
src) folder based on givenextensions. - Match asset with the string arguments provided in the PHP caller.
- Emit matched asset file for compilation if it hasn't been emitted before.
