gebruederheitz/wp-module-scripts
Composer 安装命令:
composer require gebruederheitz/wp-module-scripts
包简介
Simple setup for module and nomodule script tags in Wordpress
README 文档
README
Simple setup for module and nomodule script tags in Wordpress
Most of the credit goes to kylereicks, who demonstrated in principle how a module/nomodule setup can be realized with Wordpress' script loading mechanism in https://github.com/kylereicks/wp-script-module-nomodule.
I've wrapped it all in a reusable class, improved some of the regexes and logic, and added the convenience method for registering / enqueueing the scripts with the required data declared in the first place.
Installation
composer require gebruederheitz/wp-module-scripts
Usage
The simplest usage involves instantiating the handler (so it can register its action hook callback) and adding the required extra data to a script:
// functions.php or similar use GebruederHeitz\Wordpress\ModuleScriptHandler; // Initialize once so the action can be hooked ModuleScriptHandler::getInstance(); // The script that has been registered (or enqueued) with the handle // 'my-legacy-script' will now have the 'nomodule' attribute added to it. wp_script_add_data('my-legacy-script', 'nomodule', true); // This will be "type='module'": wp_script_add_data('my-esm-script', 'type', 'module');
Using the utility method for registering / enqueueing
The utility method register() will handle both the registration / enqueueing
of the scripts and the addition of the required attributes in one go.
Note
All scripts are loaded asynchronously in the "footer". The script paths are
passed through get_theme_file_uri().
// functions.php or similar use GebruederHeitz\Wordpress\ModuleScriptHandler; // Initialize once so the action can be hooked ModuleScriptHandler::getInstance(); add_action('wp_enqueue_scripts', function() { // ModuleScriptHandler is a Singleton, so this will retrieve the instance // initialized above $handler = ModuleScriptHandler::getInstance(); // Simple usage: register your hybrid build // Paths are passed through `get_theme_file_uri()` $handler->register('my-hybrid-script', 'path/to/module.js', 'path/to/nomodule.js'); // Adds cache busting to the nomodule script (because version is supplied), // immediately enqueues both scripts, and returns the handles. [$moduleHandle, $nomoduleHandle] = $handler->register( 'my-hybrid-script', modulePath: 'js/main.esm.v4.0.1.js', nomodulePath: 'js/main.legacy.js', dependencies: ['jquery'], enqueue: true, version: '4.0.1', ); });
<script type="module" async src="$theme/path/to/module.js" id="my-hybrid-script-module" ></script> <script nomodule type="text/javascript" async src="$theme/path/to/nomodule.js" id="my-hybrid-script-nomodule" ></script> <script type="module" async src="$theme/js/main.esm.v4.0.1.js" id="my-hybrid-script-module" ></script> <script nomodule type="text/javascript" async src="$theme/js/main.legacy.js?ver=4.0.1" id="my-hybrid-script-nomodule" ></script>
Advanced Usage
[$moduleHandle, $nomoduleHandle] = $handler->register( 'my-hybrid-script', modulePath: 'js/main.esm.v4.0.1.js', nomodulePath: 'js/main.legacy.js', version: '4.0.1', ); wp_localize_script($moduleHandle, 'myData', ['foo' => 'bar']); if ($condition) { wp_enqueue_script($moduleHandle); } // You can also register/enqueue only a module script by passing only the // modulePath... [$moduleHandle, $nomoduleHandle] = $handler->register( 'my-module-only-script', modulePath: 'js/main.esm.v4.0.1.js', version: '4.0.1', enqueue: true, ); // ...or a "generic" script with no special attributes whatsoever. [$moduleHandle, $nomoduleHandle] = $handler->register( 'my-generic-script', nomodulePath: 'js/main.js', version: '4.0.1', enqueue: true, );
<script type="text/javascript" id="my-hybrid-scripts-module-js-extra"> /* <![CDATA[ */ var myData = {"foo":"bar"}; /* ]]> */ </script> <script type="module" async src="$theme/js/main.esm.v4.0.1.js" id="my-hybrid-script-module" ></script> <!-- nomodule script has not been enqueued --> <script type="module" async src="$theme/js/main.esm.v4.0.1.js" id="my-module-only-script-module" ></script> <script type="text/javascript" async src="$theme/js/main.js?ver=4.0.1" id="my-generic-script-nomodule" ></script>
gebruederheitz/wp-module-scripts 适用场景与选型建议
gebruederheitz/wp-module-scripts 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 292 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 03 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 gebruederheitz/wp-module-scripts 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 gebruederheitz/wp-module-scripts 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 292
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0-only
- 更新时间: 2025-03-25