juster/wordpress-vite
最新稳定版本:v1.0.4
Composer 安装命令:
composer require juster/wordpress-vite
包简介
A library to properly integrate a Vite workflow into WordPress.
关键字:
README 文档
README
A library to properly integrate a Vite workflow into WordPress.
Requirements
- PHP 7.4 or higher;
- WordPress 6.5 or higher;
- A valid Vite configuration using the npm module vite-plugin-wordpress.
How it works
All the logic for resolving paths to assets generated by Vite is located in the core of the library.
All you have to do is instantiate the module by passing two parameters:
- the path to the
vite.config.phpconfiguration file generated by the previously installedvite-plugin-wordpressnpm module; - the current Wordpress environment type (production or whatever else).
use Juster\WordpressVite\App; function vite(): App { return App::get(ABSPATH, WP_ENVIRONMENT_TYPE); } vite();
You may need an autoloader for this. Composer can help you with that.
Then, you can include your CSS & JavaScript files in this way:
function theme(): void { vite()->assets()->enqueueStyle('theme', 'src/styles/main.css'); vite()->assets()->enqueueScript('theme', 'src/scripts/main.js'); } add_action('wp_enqueue_scripts', 'theme');
For other static files that need to be included in templates, such as images, you can proceed as follows:
<img src="<?= vite()->assets()->resolve('src/assets/images/placeholder.webp') ?>" alt="A wonderful image" />
And if you prefer to avoid this verbosity, you can declare a small utility function, such as this one, for example:
function asset($path): string { return vite()->assets()->resolve("src/assets/$path"); }
This results in the following in the templates:
<img src="<?= asset('images/placeholder.webp') ?>" alt="A wonderful image" />
统计信息
- 总下载量: 20
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-08-19