Skip to content
This repository was archived by the owner on Aug 9, 2025. It is now read-only.

Commit c7fb756

Browse files
feat: add support for different public paths
1 parent 888da96 commit c7fb756

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ interface PluginOptions {
3838
* @default true
3939
*/
4040
active?: boolean
41+
42+
/**
43+
* Commonly assets will be prefixed with a public path, such as "/" or "/assets".
44+
* Setting this option to the public path allows plugin-sri to resolve those imports.
45+
* @default ""
46+
*/
47+
publicPath?: string
4148
}
4249

4350
const invalidHashAlgorithms = ['sha1', 'md5']
@@ -47,6 +54,7 @@ export default (options?: PluginOptions): Plugin => {
4754
const hashAlgorithms = options?.algorithms || ['sha384']
4855
const crossorigin = options?.crossorigin || 'anonymous'
4956
const active = options?.active ?? true
57+
const publicPath = options?.publicPath ?? ''
5058

5159
return {
5260
name: 'subresource-integrity',
@@ -69,6 +77,7 @@ export default (options?: PluginOptions): Plugin => {
6977
const url = $(el).attr('href') || $(el).attr('src')
7078
if (!url) return
7179

80+
const url = ($(el).attr('href') || $(el).attr('src'))?.replace(publicPath, '')
7281
let buf: Buffer
7382
if (url.startsWith('http:')) {
7483
buf = await (await fetch(url)).buffer()

0 commit comments

Comments
 (0)