wpackio/enqueue
Composer 安装命令:
composer require wpackio/enqueue
包简介
API to enqueue assets generated by @wpackio/scripts into your WordPress plugin or theme.
README 文档
README
This is the PHP companion of @wpackio/scripts.
It gives you all the APIs you will need to properly consume assets generated from
@wpackio/scripts from your WordPress plugins or themes.
Detailed Documentation
This README only covers the very basics and a quick start guide, without explaining the overall usage.
Please visit our official documentation site for detailed instruction.
Installation
Using Composer
We recommend using composer for using this library.
composer require wpackio/enqueue
Then in your plugin main file or functions.php file of your theme, load
composer auto-loader.
<?php // Require the composer autoload for getting conflict-free access to enqueue require_once __DIR__ . '/vendor/autoload.php'; // Instantiate $enqueue = new \WPackio\Enqueue( 'appName', 'outputPath', '1.0.0', 'plugin', __FILE__ );
Manual
If you do not wish to use composer, then download the file Enqueue.php.
Remove the namespace line namespace WPackio; and rename the classname from
Enqueue to something less generic, like MyPluginEnqueue. This ensures
conflict-free loading.
Then require the file in your plugin entry-point or functions.php file of your theme.
<?php // Require the file yourself require_once __DIR__ . '/inc/MyPluginEnqueue.php'; // Instantiate $enqueue = new MyPluginEnqueue( 'appName', 'outputPath', '1.0.0', 'plugin', __FILE__ );
Getting Started
Which ever way, you choose to install, you have to make sure to instantiate the class early during the entry-point of your plugin or theme.
This ensures that we hava necessary javascript in our website frontend and admin end to make webpack code-splitting and dynamic import work.
A common pattern may look like this.
<?php // Assuming this is the main plugin file. // Require the composer autoload for getting conflict-free access to enqueue require_once __DIR__ . '/vendor/autoload.php'; // Do stuff through this plugin class MyPluginInit { /** * @var \WPackio\Enqueue */ public $enqueue; public function __construct() { // It is important that we init the Enqueue class right at the plugin/theme load time $this->enqueue = new \WPackio\Enqueue( 'wpackplugin', 'dist', '1.0.0', 'plugin', __FILE__ ); // Enqueue a few of our entry points add_action( 'wp_enqueue_scripts', [ $this, 'plugin_enqueue' ] ); } public function plugin_enqueue() { $this->enqueue->enqueue( 'app', 'main', [] ); $this->enqueue->enqueue( 'app', 'mobile', [] ); $this->enqueue->enqueue( 'foo', 'main', [] ); } } // Init new MyPluginInit();
Default configuration when calling enqueue
[ 'js' => true, 'css' => true, 'js_dep' => [], 'css_dep' => [], 'in_footer' => true, 'media' => 'all', 'main_js_handle' => null, 'runtime_js_handle' => null, ];
main_js_handle is added in 3.3 and can predictably set the handle of primary
JavaScript file. Useful for translations etc.
runtime_js_handle is added in 3.4 and can predictably set the handle of the
common runtime JavaScript. This is useful to localize/translate dependent script
handles in the same files entry. By calling wp_set_script_translations on the
runtime you can collectively enqueue translate json for all the dependencies on
the entries.
For information on usage and API, please visit official documentation site wpack.io.
Avoid conflict in multiple WordPress Plugins
Always require the latest version of Wpackio\Enqueue. The autoloader is set
to load only one instance and will not conflict with existing class.
However, if you want to load conflict free, kindly use Strauss.
Actions and Filters
Filter wpackio_print_public_path
Accepts 3 parameters:
$publichPathUrlThe URL that is used for the publicPath$appNameApplication Name$outputPathOutput path relative to the root of this plugin/theme.
Using this you can dynamically change the public path that is used for code splitting. This can be used to change the public path to a CDN.
Example Code to replace all wpack.io public path with a cdn url
add_filter( 'wpackio_print_public_path', 'set_public_path_to_cdn' ); function set_public_path_to_cdn( $publichPathUrl ) { $home_url = get_home_url(); // WordPress home url $cdn_url = 'https://cdn.example.com'; // CDN url // replace wordpress home url with cdn url return str_replace($home_url, $cdn_url, $publichPathUrl); }
Example Code to the change the public path url only for a specific instance of wpack.io
add_filter( 'wpackio_print_public_path', 'set_public_path_to_cdn', 10, 2 ); function set_public_path_to_cdn( $publichPathUrl, $appName ) { // check for our plugin if( 'myPlugin' !== $appName ) return $publichPathUrl; $home_url = get_home_url(); // WordPress home url $cdn_url = 'https://cdn.example.com'; // CDN url // replace WordPress home url with cdn url return str_replace($home_url, $cdn_url, $publichPathUrl); }
wpackio/enqueue 适用场景与选型建议
wpackio/enqueue 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 79.16k 次下载、GitHub Stars 达 12, 最近一次更新时间为 2018 年 10 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 wpackio/enqueue 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 wpackio/enqueue 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 79.16k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 12
- 点击次数: 10
- 依赖项目数: 6
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-10-13