humanmade/asset-loader
Composer 安装命令:
composer require humanmade/asset-loader
包简介
Utilities to seamlessly consume Webpack-bundled assets in WordPress themes & plugins.
README 文档
README
This plugin exposes functions which may be used within other WordPress themes or plugins to register and enqueue bundled script assets. It supports both wp-scripts-generated builds and custom Webpack configurations that output a JSON asset manifest.
Usage
Script Asset API (wp-scripts)
This plugin's primary public interface is for loading scripts built with wp-scripts. Use these functions for any wp-scripts-generated bundle that is not already auto-registered by register_block_type_from_metadata(). Dependencies and version information are read automatically from the .asset.php file generated alongside each bundle.
<?php namespace My_Plugin\Scripts; use Asset_Loader; add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\\enqueue_block_editor_assets' ); function enqueue_block_editor_assets() { // Register and enqueue a wp-scripts bundle in one step. Asset_Loader\enqueue_script_asset( 'my-plugin-editor', plugin_dir_path( __DIR__ ) . 'build/editor/index.js' ); } add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\\enqueue_frontend_assets' ); function enqueue_frontend_assets() { // You can pass additional dependencies beyond those in asset.php. Asset_Loader\enqueue_script_asset( 'my-plugin-frontend', plugin_dir_path( __DIR__ ) . 'build/frontend/index.js', [ 'some-other-script-handle' ] ); }
The wp-scripts build does not output an asset file for CSS assets or hash their filenames, so we recommend to use the standard wp_register_style and wp_enqueue_style functions directly for style assets unrelated to a particular block.
Manifest Asset API (custom Webpack)
For projects using a custom Webpack configuration that outputs a JSON asset manifest (such as those created with the presets in @humanmade/webpack-helpers), use Asset_Loader\register_manifest_asset() and Asset_Loader\enqueue_manifest_asset(). The manifest associates asset bundle names with either URIs pointing to asset bundles on a running DevServer instance, or else local file paths on disk.
<?php namespace My_Theme\Scripts; use Asset_Loader; add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\\enqueue_block_editor_assets' ); /** * Enqueue the JS and CSS for blocks in the editor. */ function enqueue_block_editor_assets() { Asset_Loader\enqueue_manifest_asset( // In a plugin, this would be `plugin_dir_path( __FILE__ )` or similar. get_stylesheet_directory() . '/build/asset-manifest.json', // The handle of a resource within the manifest. For static file fallbacks, // this should also match the filename on disk of a build production asset. 'editor.js', [ 'handle' => 'optional-custom-script-handle', 'dependencies' => [ 'wp-element', 'wp-editor' ], ] ); Asset_Loader\enqueue_manifest_asset( // In a plugin, this would be `plugin_dir_path( __FILE__ )` or similar. get_stylesheet_directory() . '/build/asset-manifest.json', // Enqueue CSS for the editor. 'editor.css', [ 'handle' => 'custom-style-handle', 'dependencies' => [ 'some-style-dependency' ], ] ); }
Block Extensions API
Use register_block_extension() to attach additional scripts and styles to an already-registered block type (core or third-party) via a block.json file, without registering a new block.
<?php use Asset_Loader; add_action( 'init', function() { // Extend core/paragraph with a custom viewScript and style. // The block.json at this path should have "name": "core/paragraph" // and declare assets using file:./relative paths. Asset_Loader\register_block_extension( plugin_dir_path( __FILE__ ) . 'build/blocks/core/paragraph/block.json' ); } );
Documentation
For complete documentation, including contributing process, visit the docs site.
License
This plugin is free software. You can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
humanmade/asset-loader 适用场景与选型建议
humanmade/asset-loader 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 340.8k 次下载、GitHub Stars 达 26, 最近一次更新时间为 2019 年 05 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 humanmade/asset-loader 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 humanmade/asset-loader 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 340.8k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 27
- 点击次数: 23
- 依赖项目数: 5
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0-or-later
- 更新时间: 2019-05-02