idleberg / wordpress-vite-assets
从 Vite 清单中注入资源到 WordPress 头部,支持主题和插件
Requires
- php: >=8.1
- idleberg/vite-manifest: ^1.1.0
Requires (Dev)
- brainmaestro/composer-git-hooks: ^3.0@alpha
- codeception/codeception: ^5.0.4
- codeception/module-asserts: ^3.0.0
- friendsofphp/php-cs-fixer: ^3.11
- icanhazstring/composer-unused: ^0.8.10
- phpstan/extension-installer: ^1.2
- phpstan/phpstan: ^1.9
- szepeviktor/phpstan-wordpress: ^1.1
README
从 Vite 清单中添加资源到 WordPress 头部,支持主题和插件。
目录
安装
composer require idleberg/wordpress-vite-assets
使用
要开始使用,首先实例化此库公开的类
new Assets(string $manifestPath, string $baseUri, string $algorithm = "sha256");
参数
$manifestPath
类型: string
指定清单的路径。
$baseUri
类型: string
指定清单中资源的基准 URI。
$algorithm
类型: "sha256"
|"sha384"
|"sha512"
| ":manifest:"
默认: "sha256"
指定用于散列资源的算法。这将用于打印脚本或样式标签时的子资源完整性。您可以使用 ":manifest:"
与 vite-plugin-manifest-sri 插件一起使用,该插件在构建时计算散列并将其添加到清单中。
示例
// functions.php use Idleberg\WordPress\ViteAssets\Assets; $baseUrl = get_stylesheet_directory_uri(); $manifest = "path/to/manifest.json"; $entryPoint = "index.ts"; $viteAssets = new Assets($manifest, $baseUrl); $viteAssets->inject($entryPoint);
方法
inject()
用法: inject(array|string $entrypoints, array $options = [])
将清单中指定的条目标签注入到页面头部
- 脚本入口点
- 导入脚本的预加载
- 样式标签
getScriptTag()
用法: getScriptTag(string $entrypoint, array $options = [])
返回清单中条目的脚本标签
getStyleTags()
用法: getStyleTags(string $entrypoint, array $options = [])
返回清单中条目的样式标签
getPreloadTags()
用法: getPreloadTags(string $entrypoint)
返回清单中条目的预加载标签
选项
option.action
类型: null | string
允许覆盖 inject()
方法的默认行为。
示例
// plugin.php $viteAssets->inject("index.ts", [ "action" => "admin_head" ]);
⚠️ 您可能不想更改默认行为,除非您知道自己在做什么,否则不要覆盖!
option.crossorigin
类型: boolean | "anonymous" | "use-credentials"
切换脚本和样式标签上的 crossorigin
属性,或分配一个值
option.integrity
类型: boolean
切换脚本和样式标签上的 integrity
属性
option.priority
类型: int | array
允许覆盖 inject()
方法的优先级。当提供为数组时,允许细粒度控制
示例
// functions.php $viteAssets->inject("index.ts", [ "priority" => [ "scripts" => 10, "preloads" => 0, "styles" => 20 ] ]);
许可证
本作品根据 MIT 许可证 许可。