vite-php-manifest
v0.1.1
Published
A simple Vite 6+ plugin generate a PHP manifest.
Readme
Features
- Converts
manifest.jsonto a PHP file. - Supports filtering and grouping of manifest items/chunks.
- Allows customization of PHP file formatting (line breaks, indentation, short array syntax).
Installation
Install it via npm:
npm install vite-php-manifest --save-devOr with yarn:
yarn add -D vite-php-manifestUsage
Add the plugin to your vite.config.js file:
import { defineConfig } from 'vite';
import { VitePhpManifest } from 'vite-php-manifest';
export default defineConfig({
plugins: [VitePhpManifest()],
});or with plugin options (optional)
import { defineConfig } from 'vite';
import { VitePhpManifest } from 'vite-php-manifest';
export default defineConfig({
plugins: [
VitePhpManifest({
filter: (originalFileName, manifestChunk) => {
manifestChunk.myCustomKey = 'my-custom-value';
return manifestChunk;
},
group: {
js: ['js', 'jsx'],
css: ['css'],
images: ['png', 'jpg', 'jpeg', 'gif'],
svg: ['svg'],
},
linebreak: '\n',
indent: ' ',
shortArraySyntax: true,
unlinkOriginManifest: false,
manifestFile: 'vite/manifest.php',
}),
],
});Options
filter (default: false)
Type: false | (originalFileName: string, manifestChunk: ManifestChunk) => ManifestChunk | null
A function to filter chunks in the manifest. If false, no filtering is applied.
Example:
filter: (originalFileName, manifestChunk) => {
if (manifestChunk.isEntry) {
manifestChunk.customEntryKey = 'custom-value';
}
return manifestChunk;
};group (default: false)
Type: false | Record<string, string[]> | (originalFileName: string, manifestChunk: ManifestChunk) => string
A function to group chunks in the PHP manifest. Can be an object mapping group names to file extensions or a callback function.
Example (Object Mapping):
group: {
js: ['js', 'jsx'],
css: ['css'],
images: ['png', 'jpg', 'jpeg', 'gif'],
svg: ['svg']
},Example (Callback Function):
group: (originalFileName, manifestChunk) => {
return originalFileName.endsWith('.css') ? 'styles' : 'scripts';
},defaultGroup (default: 'other')
Type: string
The default group name for chunks that do not match any group when using object mapping.
unlinkOriginManifest (default: true)
Type: boolean
If true, deletes the original manifest.json after generating the PHP file.
manifestFile (default: 'manifest.php')
Type: string
The output path for the generated PHP manifest file relative to the outDir.
linebreak (default: '\n')
Type: string
The line break character(s) to use in the PHP manifest file.
indent (default: ' ')
Type: string
The indentation character(s) to use in the PHP manifest file.
shortArraySyntax (default: false)
Type: boolean
Whether to use PHP's short array syntax ([]).
Example Output
Given the following manifest.json:
{
"main.js": {
"file": "assets/main-1234.js",
"isEntry": true
},
"styles.css": {
"file": "assets/styles-5678.css",
"isEntry": false
}
}The plugin generates the following manifest.php:
<?php
return array(
'main.js' => array(
'file' => 'assets/main-1234.js',
'isEntry' => true,
),
'styles.css' => array(
'file' => 'assets/styles-5678.css',
'isEntry' => false,
),
);