juster/wordpress-vite 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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:

  1. the path to the vite.config.php configuration file generated by the previously installed vite-plugin-wordpress npm module;
  2. 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

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-08-19