subresourceIntegrity(options?)
对最终本地 JavaScript/CSS 计算 Subresource Integrity 摘要,并更新 HTML 中的 script、stylesheet、preload 和 modulepreload 标签。
ts
subresourceIntegrity({
algorithms: ["sha384", "sha512"],
crossorigin: "anonymous",
manifest: "meta/integrity.json",
strict: false,
});| 选项 | 默认值 | 说明 |
|---|---|---|
algorithms | "sha384" | sha256 / sha384 / sha512,支持多个摘要 |
filter | JavaScript 与 CSS | 正则或 (fileName, type) => boolean |
crossorigin | "anonymous" | anonymous / use-credentials;false 不自动添加 |
overwrite | true | 更新本地标签已有的 integrity/crossorigin |
manifest | false | true 输出 integrity-manifest.json,字符串指定安全相对路径 |
strict | false | 本地脚本/样式无法对应 bundle 产物时中止构建 |
完整 URL 只有在与绝对 base 同源且位于其路径下时才作为本地产物;其他远程资源不会被下载或计算。publicDir 文件不在 bundle 中,启用 strict 前应自行纳入构建图或单独处理。独立使用时必须把本插件放在预压缩插件之前。
subresourceIntegrity 完整性
为 Vite 最终 JavaScript/CSS 产物生成 SRI,并更新构建后的 HTML 入口。
插件在输出阶段末尾运行;若同时使用预压缩插件,应把本插件放在压缩插件之前,使 .gz / .br HTML 与注入后的原文件保持一致。
签名
ts
export function subresourceIntegrity(options: SubresourceIntegrityPluginOptions = {}): Plugin;示例
ts
import { subresourceIntegrity } from "fast-vite-plugins";
const plugin = subresourceIntegrity({ algorithms: ["sha384"], manifest: "meta/integrity.json" });
export default { plugins: [plugin] };输入
| 输入值 | 输入值类型 | 必填/默认值 | 输入值说明 |
|---|---|---|---|
options | SubresourceIntegrityPluginOptions | 否,默认 {} | 摘要算法、资源过滤、CORS、覆盖、清单和严格模式配置。 |
返回
| 返回值 | 返回值类型 | 返回值说明 |
|---|---|---|
plugin | Plugin | 可直接加入 Vite plugins 的 Subresource Integrity 插件。 |
