vite-plugin-sri4 4.0.0 → 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/README.md +51 -2
  2. package/dist/index.cjs +379 -135
  3. package/dist/index.js +379 -135
  4. package/package.json +10 -9
package/README.md CHANGED
@@ -12,6 +12,8 @@ A Vite plugin to generate Subresource Integrity (SRI) hashes for your assets dur
12
12
  - [Installation](#installation)
13
13
  - [Usage](#usage)
14
14
  - [Plugin Options](#plugin-options)
15
+ - [Dynamic Routes](#dynamic-routes)
16
+ - [When SRI Actually Helps](#when-sri-actually-helps)
15
17
  - [Example Project](#example-project)
16
18
  - [Best Practices](#best-practices)
17
19
  - [Troubleshooting](#troubleshooting)
@@ -27,7 +29,8 @@ A Vite plugin to generate Subresource Integrity (SRI) hashes for your assets dur
27
29
  - **Bypass Domains:** Option to specify domains to bypass SRI injection.
28
30
  - **Missing Asset Handling:** Configurable warning suppression for missing assets.
29
31
  - **Robust Content Support:** Handles various content types including strings, Buffer, and Uint8Array.
30
- - **Vite Compatibility:** Compatible with Vite 7.0 and Vite 8.0 (including the Rolldown-based native build path).
32
+ - **Dynamic Routes:** Optional import map integrity and an SRI manifest cover `import()`-loaded chunks and SSR builds, which have no build-time HTML tag to rewrite.
33
+ - **Vite Compatibility:** Compatible with Vite 6.4, 7.0 and 8.0 (including the Rolldown-based native build path). `6.4` is the floor because it is the only Vite 6 line still receiving upstream security patches.
31
34
 
32
35
  ## Installation
33
36
 
@@ -54,7 +57,12 @@ export default defineConfig({
54
57
  // Optional. Suppress warnings for missing assets.
55
58
  ignoreMissingAsset: false,
56
59
  // Optional. Log verbosity: 'silent' | 'error' | 'warn' | 'info' | 'debug'. Defaults to 'warn'.
57
- logLevel: 'warn'
60
+ logLevel: 'warn',
61
+ // Optional. Inject an import map carrying integrity for every JS chunk,
62
+ // covering dynamically imported routes. Defaults to false.
63
+ importmap: false,
64
+ // Optional. Emit dist/sri-manifest.json for SSR to read. Defaults to false.
65
+ manifest: false
58
66
  })
59
67
  ]
60
68
  });
@@ -84,6 +92,47 @@ Output:
84
92
  When true, suppresses warnings for assets that are not found in the bundle. Default is `false`.
85
93
  * `logLevel` (string):
86
94
  Log verbosity. One of `silent`, `error`, `warn`, `info`, `debug`. Default is `warn`. Use `debug` to see per-resource decisions during the build.
95
+ * `importmap` (boolean):
96
+ Inject a `<script type="importmap">` containing an `integrity` map for every JS chunk in the build. Default is `false`. See [Dynamic routes](#dynamic-routes).
97
+ * `manifest` (boolean):
98
+ Emit `sri-manifest.json` alongside the build, mapping every non-HTML output file to its SRI hash. Default is `false`. See [Dynamic routes](#dynamic-routes).
99
+
100
+ ## Dynamic routes
101
+
102
+ Rewriting HTML tags can only protect resources that have a tag at build time. A route loaded with `import()` has none - Vite's preload helper creates the `<link rel="modulepreload">` at runtime - and an SSR build emits no HTML at all. Two options cover those cases.
103
+
104
+ ### `importmap: true` (client-side dynamic imports)
105
+
106
+ Emits an import map whose `integrity` key covers every JS chunk, including chunks only ever reached through `import()`:
107
+
108
+ ```html
109
+ <script type="importmap">{"integrity":{"/assets/about-a1b2c3.js":"sha384-..."}}</script>
110
+ ```
111
+
112
+ The map is injected before the first `<script>` so it applies to every module. Engines without support ignore the `integrity` key rather than failing, so this degrades safely - but check current browser support before relying on it as your only protection.
113
+
114
+ ### `manifest: true` (SSR / server-rendered HTML)
115
+
116
+ Emits `sri-manifest.json` mapping output file names to hashes, which a server rendering HTML per request can read:
117
+
118
+ ```json
119
+ {
120
+ "assets/index-a1b2c3.js": "sha384-...",
121
+ "assets/index-d4e5f6.css": "sha384-..."
122
+ }
123
+ ```
124
+
125
+ This is the only mechanism available when the build produces no HTML asset.
126
+
127
+ ### Caveat for both
128
+
129
+ Hashes are computed during the build. A plugin that mutates chunk contents after this one (`@vitejs/plugin-legacy`, compression plugins that rewrite in place) would invalidate them, so the plugin re-hashes every file it touched in `writeBundle` and fails the build if anything drifted. You get a build error rather than a page that only breaks in the browser.
130
+
131
+ ## When SRI Actually Helps
132
+
133
+ SRI is worth the most when your HTML and your assets have **different trust boundaries** - typically HTML served from your own origin and JS/CSS served from a CDN (`base: 'https://cdn.example.com/'`). If the CDN is compromised or a cache is poisoned, the integrity attribute in your origin-served HTML is what stops the browser from running the tampered file. That is the case this plugin is built for.
134
+
135
+ If everything is served from a single origin, SRI buys much less than it appears to: an attacker who can rewrite `/assets/index-abc123.js` on your server can usually rewrite the `index.html` carrying its hash just as easily. It is not useless - it narrows some deploy and cache-layer mistakes - but for same-origin builds, a Content Security Policy and Vite's default hashed, immutable filenames do more for you than SRI does. Enable it because it is cheap, not because it closes the hole you think it closes.
87
136
 
88
137
  ## Example Project
89
138