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

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

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

mrottow/vite-wordpress

Composer 安装命令:

composer require mrottow/vite-wordpress

包简介

A collection of back-end utilities for ViteWordpress.

README 文档

README

vite logo

Vite PHP utilities for WordPress

Backend Utilities for vite-wordpress to integrate Vite's development server and HMR into WordPress, as well as manage the manifest file.

GitHub release Packagist Version GitHub last commit Packagist License

Features

  • Manifest Resolver: Parse and manage Vite's manifest file for registering assets in WordPress for production.
  • Dev Server & HMR Integration: Integrates Vite's HMR (Hot Module Replacement) and dev server into WordPress during development.
    • Supports frontend block development
    • Supports Elementor editor development
  • Automatic Script Injection: Automatically injects Vite client scripts into WordPress during development.
  • Automatic Source Files Injection:
    • Automatically changes src urls from enqueued scripts/styles to source files for HMR during development.
    • Automatically changes render template paths from dynamic blocks in block.json to source files for HMR during development.

Requirements

  • PHP 7.4 or higher
  • WordPress 6.0 or higher
  • A valid ViteJS configuration using vite-wordpress

Installation

  1. Add the library to your project:
    composer require mrottow/vite-wordpress
  2. Include the autoloader in your WordPress theme or plugin:
    require_once __DIR__ . '/vendor/autoload.php';

Usage

Dev Server

The DevServer class integrates the Vite development server with WordPress for HMR.

Example (uses default settings):

(new ViteWordPress\DevServer())->register();

With custom settings:

(new ViteWordPress\DevServer())
    ->set_server_port(5173)
    ->set_server_host('localhost')
    ->register();

When using the vite-wordpress ViteJS plugin, if files have been built using vite build, are enqueued and the development server is running via the vite command:

  1. DevServer automatically detects all enqueued scripts & templates from the project through hooks & vite-wordpress server configurations.
  2. It resolves scripts to source files served by the development server.
  3. It resolves templates to source files from the srcDir folder.
  4. It updates script tags from the project to use as modules and injects Vite's client to enable HMR (Hot Module Replacement).
  5. Using Vite's client and module support, it can also include CSS source files that are imported into JavaScript files.

It works for both JS and CSS entries.

It's compatible with a traditional setup with vite-wordpress without the need for a manifest or hashed files.

However, when using a manifest, the Manifest Resolver will assist in resolving the files through the manifest offering better precision for a more complex assets folder & file structure.

Manifest Resolver

The ManifestResolver class handles reading and accessing the Vite manifest file and additionally integrates into the dev server.

You can use the ManifestResolver instance, which needs to be wrapped in a function or helper class to work within hooks. Alternatively, you can use the Manifest facade, which handles this for you.

Example using the facade:

use ViteWordPress\DevServer;
use ViteWordPress\Manifest;

$manifest = Manifest::create( 'absolute/path/to/manifest.json' ); // Also works with a PHP manifest file.

// When using the dev server you need to include the manifest.
( new DevServer( $manifest ) )->register();

// Enqueue scripts hook.
add_action( 'wp_enqueue_scripts', function () {
	$file_name = Manifest::get_file( 'app.js' );

	wp_enqueue_script( 'my-app', get_stylesheet_directory() . "build/{$file_name}" );
} );

Example using the instance:

use ViteWordPress\DevServer;
use ViteWordPress\ManifestResolver;

function manifest() {
	static $manifest;

	if ( ! isset( $manifest ) ) {
		$manifest = new ManifestResolver();
		$manifest->set_manifest( 'absolute/path/manifest.json' ); // Also works with a PHP manifest file.
	}

	return $manifest;
}

// When using the dev server you need to include the manifest.
( new DevServer( manifest() ) )->register();

// Enqueue scripts hook.
add_action( 'wp_enqueue_scripts', function () {
	$file_name = manifest()->get( 'app.js' )['file'];

	wp_enqueue_script( 'my-app', get_stylesheet_directory() . "build/{$file_name}" );
} );

The manifest resolver is built off an interface so you're able to create your own implementation to include into the dev server.

Configuration

ManifestResolver

Method Default Description
set_manifest($path) `` Sets the path to the manifest.json or manifest.php file.
set_src($dir) 'src' (optional) Sets the source directory for the assets.

DevServer

Method Default Description
set_server_host($host) get_site_url() (optional) Sets the Vite dev server host.
set_server_port($port) 5173 (optional) Sets the Vite dev server port.
set_client_hook($priority) 5 (optional) Sets the priority for the Vite client hook.

Example

An example within a WordPress plugin can be found here: https://github.com/mrOttoW/vite-wordpress-plugin-block-example

mrottow/vite-wordpress 适用场景与选型建议

mrottow/vite-wordpress 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 95 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 12 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「wordpress」 「vite」 「vitejs」 「wordpress vite」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 mrottow/vite-wordpress 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 mrottow/vite-wordpress 我们能提供哪些服务?
定制开发 / 二次开发

基于 mrottow/vite-wordpress 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 95
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 16
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-12-15