milbareu/wordpress-webpack-asset-manager 问题修复 & 功能扩展

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

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

milbareu/wordpress-webpack-asset-manager

Composer 安装命令:

composer require milbareu/wordpress-webpack-asset-manager

包简介

A WordPress asset manager for handling Webpack's manifest.json and enqueuing assets with dependencies.

README 文档

README

Introduction

WPAssets is a PHP class designed to manage assets in WordPress. It integrates with Webpack's manifest.json files, automatically enqueuing CSS, JS, and handling PHP dependencies ( with Dependency Extraction Webpack Plugin) in a structured and scalable way. This streamlines asset management in WordPress themes and plugins.

Features

  • Automatic Asset Enqueuing: Easily enqueue JavaScript, CSS, and PHP files defined in Webpack's manifest.json.
  • Bundle-Based Asset Management: Manage your assets by bundling them into logical entry points.
  • Namespace Support: Add a custom namespace prefix to your assets for better organization.
  • PHP Dependency Support: Automatically includes PHP files as part of the asset dependencies.
  • Child Theme / Parent Theme Awareness: Loads assets from the active stylesheet by default, with an option to force parent-theme manifests.
  • Sage 9 Detection: Automatically detects Sage 9-style theme structures and adjusts asset base paths accordingly.
  • WordPress Filter Hooks: Override the output directory and Sage detection logic without modifying the library.

Install via Composer

You can install WPAssets via Composer. Run the following command in your WordPress, theme or plugin directory:

composer require milbareu/wordpress-webpack-asset-manager

Then import the class where you want to use it:

use MB\WPAssets\WPAssets;

1. Enqueueing Assets in WordPress

To enqueue a bundle (which includes CSS, JS, and optionally PHP files), use the WPAssets::enqueueBundle() function. This function pulls the required assets from the Webpack-generated manifest.json file.

// Example: Enqueue the 'main' bundle with the namespace 'mytheme'
WPAssets::enqueueBundle('main', 'mytheme');

// Example: Enqueue the 'editor' bundle with the default namespace
WPAssets::enqueueBundle('editor');

2. Example Setup in functions.php

Here’s an example of how to use WPAssets in a WordPress theme to manage assets:

<?php

use MB\WPAssets\WPAssets;

// Register the theme assets
add_action('wp_enqueue_scripts', function () {
    WPAssets::enqueueBundle('main', 'mytheme');
}, 100);

// Register assets for the block editor
add_action('enqueue_block_editor_assets', function () {
    WPAssets::enqueueBundle('editor');
}, 100);

2.1 Example Webpack Configuration

The example/ folder contains sample Webpack configuration files that you can adapt for your project. These configurations support both production and development environments, allowing you to build optimized assets for each scenario.

2.2 Environment-Based Build

Make sure your Webpack configuration supports environment-based builds. For example, you can define separate configurations for development and production environments. These will generate different asset versions optimized for each use case.

In your Webpack configuration files (see the example/ folder):

  • Development: Generates source maps and unminified assets.
  • Production: Minifies assets and removes unnecessary comments.

3. Webpack Manifest Structure

Your Webpack configuration should output a manifest.json with an entrypoints object. Below is a sample structure for manifest.json (shown without content hashes for clarity; in production WebpackAssetsManifest will append hashes):

{
  "entrypoints": {
    "main": {
      "assets": {
        "css": [
          "/styles/main.css"
        ],
        "js": [
          "/scripts/main.js"
        ]
      }
    },
    "editor": {
      "assets": {
        "css": [
          "/styles/editor.css"
        ],
        "js": [
          "/scripts/editor.js"
        ]
      }
    }
  },
  "scripts/main.js": "/scripts/main.abc123.js",
  "scripts/main.abc123.asset.php": "/scripts/main.abc123.asset.php",
  "scripts/editor.js": "/scripts/editor.abc123.js",
  "scripts/editor.abc123.asset.php": "/scripts/editor.abc123.asset.php",
  "styles/main.css": "/styles/main.abc123.css",
  "styles/editor.css": "/styles/editor.abc123.css"
}

3.1 Content Hash Support (v1.2.0+)

When using [contenthash] in your Webpack output filenames, the Dependency Extraction Webpack Plugin emits .asset.php files with hashed names (e.g. editor.abc123.asset.php). WPAssets v1.2.0 resolves these automatically:

  1. Looks for the entry in manifest.entrypoints[entry].assets.js[0]/scripts/editor.abc123.js
  2. Derives the .asset.php path: scripts/editor.abc123.asset.php
  3. Finds it in the manifest flat keys or on the filesystem

This means you can always reference assets by their logical name (getAssetDependencies('editor')) regardless of whether content hashes are present.

4. Function Reference

getVersion(): string

Returns the current library version.

Example:

$version = WPAssets::getVersion();

enqueueBundle(string $entry, string $namespace = 'wpa') Enqueues the CSS, JS, and PHP files for a specified entry point.

  • $entry: The name of the entry point (e.g., 'main', 'editor').
  • $namespace (optional): A prefix for the asset handles (default is 'wpa').

Example:

WPAssets::enqueueBundle('main', 'mytheme');

getAsset(string $assetName, bool $getContents = false): ?string

Retrieves the URL or content of a single asset based on its name.

  • $assetName: The name of the asset (e.g., 'main.css', 'main.js').
  • $getContents: Whether to return the content of the file (true) or the URL (false).

Example:

$mainCssUrl = WPAssets::getAsset('main.css'); // Get URL of main.css

getAssetDependencies(string $entry, ?array $manifest = null): array

Retrieves the asset dependencies from the corresponding .asset.php file for a given entry.

Supports content-hashed filenames (v1.2.0+). Resolution order:

  1. entrypoints[entry].assets.php[0] — PHP file listed in the entrypoint.
  2. entrypoints[entry].assets.js[0] → derive .asset.php path from the JS output.
  3. Filesystem fallback: {baseDir}/{entry}.asset.php.
  • $entry: The entry name (e.g., 'main.js').
  • $manifest (optional): Pre-loaded manifest array to avoid re-reading the file.

Example:

$deps = WPAssets::getAssetDependencies('editor');

isSage9(): bool

Detects whether the currently active theme appears to be a Sage 9 theme.

Detection uses the first matching heuristic:

  • the App\Sage class exists,
  • config/theme.php exists in the active theme,
  • resources/views exists in the active theme.

The detected value is cached in the global WPASSETS_IS_SAGE9 constant on first use and can be overridden with the wpassets_is_sage9 filter.

Example:

if (WPAssets::isSage9()) {
    // Load additional Sage 9-specific integrations.
}

5. Advanced Usage

If your Webpack configuration includes a php key under an entrypoint, those files will be included using include_once when the bundle is enqueued. This is optional — .asset.php dependency files emitted by @wordpress/dependency-extraction-webpack-plugin are resolved automatically from the JS entry even when not listed in entrypoints (see section 3.1).

5.1 Sage 9 theme support

When WPAssets::isSage9() returns true, the library adjusts the base URL and base directory resolution to match Sage 9-style theme structures before appending the configured output directory.

If the automatic detection is not correct for your project, you can override it:

add_filter('wpassets_is_sage9', '__return_true');
// or
add_filter('wpassets_is_sage9', '__return_false');

This is especially useful for custom Sage-based themes or migration scenarios where the directory structure does not fully match the default heuristics.

5.2 Force loading assets from the parent theme

By default, WPAssets reads manifests and assets from the active stylesheet directory (child theme if one is active). To always use the parent theme instead:

add_filter('wpassets_use_parent_theme_manifest', '__return_true');

6. Overriding the Output Directory

The default output directory for assets is public. You can customize this by adding a wpassets_output_dir filter in your theme or plugin. For example:

add_filter('wpassets_output_dir', function($dir) {
    // Override the default output directory 'public'
    return 'my-custom-assets';
});

WPAssets will then use your specified directory when generating URLs and file paths via getBaseUrl() and getBaseDir().

7. Available WordPress filters

WPAssets exposes a few filters so you can adapt it to your theme structure:

  • wpassets_output_dir: Changes the asset output directory. Default: public.
  • wpassets_is_sage9: Overrides automatic Sage 9 detection.
  • wpassets_use_parent_theme_manifest: Forces lookup to use the parent theme instead of the active stylesheet directory.

Contribution

Feel free to contribute by submitting issues or pull requests. Your contributions help improve this project and make it more useful for the community.

License

This project is licensed under the MIT License.

Disclaimer

This Webpack configuration has been tailored to meet my specific needs and preferences. While it does what I need for my projects, please note that I am not an expert in Webpack, and this setup may not be suitable for all projects or environments.

Feel free to use it, modify it, or adapt it to your own needs, but please do so at your own risk. I cannot guarantee that it will work perfectly for every situation, so always ensure you test it thoroughly in your own development and production environments.

milbareu/wordpress-webpack-asset-manager 适用场景与选型建议

milbareu/wordpress-webpack-asset-manager 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 591 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 09 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 milbareu/wordpress-webpack-asset-manager 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-09-22