humanmade / asset-loader
Utilities to seamlessly consume Webpack-bundled assets in WordPress themes & plugins.
Package info
github.com/humanmade/asset-loader
Type:wordpress-plugin
pkg:composer/humanmade/asset-loader
Requires
- composer/installers: ^1.0 || ^2.0
Requires (Dev)
- 10up/wp_mock: ^1.1.0
- automattic/vipwpcs: ^3.0
- phpunit/phpunit: ^9.5
- staabm/annotate-pull-request-from-checkstyle: ^1.8
- wp-coding-standards/wpcs: ^3.0
- dev-main
- v1.x-dev
- v0.8.0
- v0.7.1
- v0.7.0
- v0.6.4
- v0.6.3
- v0.6.2
- v0.6.1
- v0.6.0
- v0.5.0
- v0.4.1
- v0.4.0
- v0.3.4
- v0.3.3
- v0.3.2
- v0.3.1
- v0.3.0
- v0.2.0
- dev-css-handling
- dev-add-register_block_extension-api
- dev-add-wp-scripts-compatible-api
- dev-register-blocks
- dev-always-load-placeholder-stylesheet
- dev-master
- dev-filter_plugin_or_theme_file_uri
This package is auto-updated.
Last update: 2026-03-23 15:08:42 UTC
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.