humanmade / asset-loader
提供在WordPress主题和插件中无缝消费Webpack打包资源的功能。
v0.7.1
2024-08-21 19:48 UTC
Requires
- composer/installers: ^1.0 || ^2.0
Requires (Dev)
- 10up/wp_mock: ^1.0.0
- automattic/vipwpcs: ^3.0
- dealerdirect/phpcodesniffer-composer-installer: ^0.7
- phpcompatibility/php-compatibility: dev-develop as 9.99.99
- phpcompatibility/phpcompatibility-wp: ^2.1
- phpcsstandards/php_codesniffer: ^3.5
- phpunit/phpunit: ^9.5
- staabm/annotate-pull-request-from-checkstyle: ^1.8
- wp-coding-standards/wpcs: ^3.0
- dev-main
- 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-dependabot/bundler/docs/google-protobuf-3.25.5
- dev-register-blocks
- dev-always-load-placeholder-stylesheet
- dev-load-single-runtime
- dev-master
- dev-filter_plugin_or_theme_file_uri
This package is auto-updated.
Last update: 2024-09-21 20:10:39 UTC
README
此插件暴露了可以在其他WordPress主题或插件中使用的一些函数,以帮助检测和加载Webpack生成的资源,包括从本地 webpack-dev-server
实例提供的服务。
用法
此库旨在与Webpack配置(例如使用@humanmade/webpack-helpers中的预设创建的配置)一起工作,该配置生成一个资源清单文件。该清单将资源包名称与指向运行中的DevServer实例上的资源包的URI或磁盘上的本地文件路径相关联。
Asset_Loader
提供了一套方法来读取此清单文件并在其中注册特定的资源以在您的WordPress网站中加载。此插件提供的公共接口主要是两个方法,Asset_Loader\register_asset()
和 Asset_Loader\enqueue_asset()
。要注册清单资产,请在类似 wp_enqueue_scripts
或 enqueue_block_editor_assets
的动作中调用这些方法,就像您会调用标准的WordPress wp_register_script
或 wp_enqueue_style
函数一样。
<?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. * * @return void */ function enqueue_block_editor_assets() { Asset_Loader\enqueue_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_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' ], ] ); }
文档
有关完整文档,包括贡献过程,请访问文档网站。
许可证
此插件是免费软件。您可以在自由软件基金会的GNU通用公共许可证(第2版或任何更新版本)的条款下重新分发或修改它。