idleberg / wordpress-vite-assets
Injects assets from a Vite manifest to the Wordpress head, supports themes and plugins
Installs: 7 729
Dependents: 0
Suggesters: 0
Security: 0
Stars: 117
Watchers: 4
Forks: 11
Open Issues: 3
Requires
- php: >=8.1
- idleberg/vite-manifest: ^1.0.0
Requires (Dev)
- brainmaestro/composer-git-hooks: ^3.0@alpha
- codeception/codeception: ^5.0.4
- codeception/module-asserts: ^3.0.0
- friendsofphp/php-cs-fixer: ^3.11
- icanhazstring/composer-unused: ^0.8.10
- phpstan/extension-installer: ^1.2
- phpstan/phpstan: ^1.9
- szepeviktor/phpstan-wordpress: ^1.1
README
Adds assets from a Vite manifest to the WordPress head, supports themes and plugins
Table of contents
Installation
composer require idleberg/wordpress-vite-assets
Usage
To get you going, first instantiate the class exposed by this library
new Assets(string $manifestPath, string $baseUri, string $algorithm = "sha256");
Example
// functions.php use Idleberg\WordPress\ViteAssets\Assets; $baseUrl = get_stylesheet_directory_uri(); $manifest = "path/to/manifest.json"; $entryPoint = "index.ts"; $viteAssets = new Assets($manifest, $baseUrl); $viteAssets->inject($entryPoint);
Methods
inject()
Usage: inject(array|string $entrypoints, array $options = [])
Injects tags for entries specified in the manifest to the page header
- script entrypoint
- preloads for imported scripts
- style tags
getScriptTag()
Usage: getScriptTag(string $entrypoint, array $options = [])
Returns the script tag for an entry in the manifest
getStyleTags()
Usage: getStyleTags(string $entrypoint, array $options = [])
Returns the style tags for an entry in the manifest
getPreloadTags()
Usage: getPreloadTags(string $entrypoint)
Returns the preload tags for an entry in the manifest
Options
option.action
Type: null | string
Allows overriding the default action for the inject()
method.
Example
// plugin.php $viteAssets->inject("index.ts", [ "action" => "admin_head" ]);
⚠️ It's unlikely that you want to change the default action, so don't override unless you know what you're doing!
option.crossorigin
Type: boolean | "anonymous" | "use-credentials"
Toggles crossorigin
attribute on script and style tags, or assigns a value
option.integrity
Type: boolean
Toggles integrity
attribute on script and style tags
option.priority
Type: int | array
Allows overriding the priority for the inject()
method. It allows granular control when provided as an array:
Example
// functions.php $viteAssets->inject("index.ts", [ "priority" => [ "scripts" => 10, "preloads" => 0, "styles" => 20 ] ]);
License
This work is licensed under The MIT License