vite-plugin-sri4 1.8.5 → 1.8.6

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.
package/README.md CHANGED
@@ -2,27 +2,39 @@
2
2
 
3
3
  ![NPM Version](https://img.shields.io/npm/v/vite-plugin-sri4)
4
4
  [![codecov](https://codecov.io/gh/7a6163/vite-plugin-sri4/graph/badge.svg?token=GOVB4J3D19)](https://codecov.io/gh/7a6163/vite-plugin-sri4)
5
+ ![License](https://img.shields.io/npm/l/vite-plugin-sri4)
5
6
 
6
7
  A Vite plugin to generate Subresource Integrity (SRI) hashes for your assets during the build process. This plugin computes SRI hashes for JavaScript and CSS files and injects them as `integrity` and `crossorigin="anonymous"` attributes into your HTML, ensuring your resources have not been tampered with when loaded by browsers.
7
8
 
9
+ ## Table of Contents
10
+
11
+ - [Features](#features)
12
+ - [Installation](#installation)
13
+ - [Usage](#usage)
14
+ - [Plugin Options](#plugin-options)
15
+ - [Example Project](#example-project)
16
+ - [Best Practices](#best-practices)
17
+ - [Troubleshooting](#troubleshooting)
18
+ - [Contributing](#contributing)
19
+ - [License](#license)
20
+
8
21
  ## Features
9
22
 
10
23
  - **Automatic SRI Generation:** Computes SRI hashes for assets (chunks and files) using a configurable algorithm (default is `sha384`).
11
24
  - **HTML Injection:** Automatically injects `integrity` and `crossorigin` attributes into `<script>` and `<link>` tags in your HTML.
12
25
  - **CORS Support Check:** For external resources, a CORS check is performed to verify access via `Access-Control-Allow-Origin`.
13
26
  - **Bypass Domains:** Option to specify domains to bypass SRI injection.
27
+ - **Missing Asset Handling:** Configurable warning suppression for missing assets.
28
+ - **Robust Content Support:** Handles various content types including strings, Buffer, and Uint8Array.
14
29
 
15
30
  ## Installation
16
31
 
17
- If the plugin has been published to npm:
18
-
19
32
  ```bash
20
33
  npm install vite-plugin-sri4 --save-dev
21
34
  ```
22
35
 
23
- Alternatively, if you're developing locally, you can use npm link or install via a relative path.
24
-
25
36
  ## Usage
37
+
26
38
  Add the plugin to your Vite configuration by updating your vite.config.js or vite.config.ts file:
27
39
 
28
40
  ```javascript
@@ -36,23 +48,142 @@ export default defineConfig({
36
48
  // Optional. The security hash algorithm. Defaults to "sha384".
37
49
  algorithm: 'sha384',
38
50
  // Optional. Domains to bypass SRI injection.
39
- bypassDomains: ['example.com']
51
+ bypassDomains: ['example.com'],
52
+ // Optional. Suppress warnings for missing assets.
53
+ ignoreMissingAsset: false,
54
+ // Optional. Enable debug logging.
55
+ debug: false
40
56
  })
41
57
  ]
42
58
  });
43
59
  ```
44
60
 
45
- Now, when you run the build command:
61
+ ### Example HTML Output
46
62
 
47
- ```bash
48
- npm run build
63
+ Input:
64
+ ```html
65
+ <script src="app.js"></script>
66
+ <link rel="stylesheet" href="style.css">
49
67
  ```
50
68
 
51
- The plugin will process the generated bundles, compute SRI hashes, and inject the attributes into the HTML.
69
+ Output:
70
+ ```html
71
+ <script src="app.js" integrity="sha384-..." crossorigin="anonymous"></script>
72
+ <link rel="stylesheet" href="style.css" integrity="sha384-..." crossorigin="anonymous">
73
+ ```
52
74
 
53
75
  ## Plugin Options
54
76
 
55
- * algorithm (string):
56
- The hash algorithm used for computing SRI. Default is sha384. You may change it to other supported algorithms like sha256.
57
- * bypassDomains (Array<string>):
58
- Array of domain names where SRI injection should be skipped. This allows external resources from specified domains to be excluded from SRI checks (for example, when they may not support CORS).
77
+ * `algorithm` (string):
78
+ The hash algorithm used for computing SRI. Default is sha384. You may change it to other supported algorithms like sha256.
79
+ * `bypassDomains` (Array<string>):
80
+ Array of domain names where SRI injection should be skipped. This allows external resources from specified domains to be excluded from SRI checks (for example, when they may not support CORS).
81
+ * `ignoreMissingAsset` (boolean):
82
+ When true, suppresses warnings for assets that are not found in the bundle. Default is false.
83
+ * `debug` (boolean):
84
+ When true, enables detailed debug logging. Default is false.
85
+
86
+ ## Example Project
87
+
88
+ The plugin includes an example project in the `example` directory that demonstrates its usage with a simple Vite application. To try it:
89
+
90
+ 1. Clone the repository
91
+ 2. Install dependencies:
92
+ ```bash
93
+ npm install
94
+ cd example
95
+ npm install
96
+ ```
97
+ 3. Build the example:
98
+ ```bash
99
+ npm run build
100
+ ```
101
+ 4. Check the generated `dist/index.html` to see the SRI hashes in action
102
+
103
+ The example project shows:
104
+ - Basic setup with Vite
105
+ - SRI hash generation for JS and CSS files
106
+ - Handling of hashed filenames
107
+ - Static file handling
108
+
109
+ ## Best Practices
110
+
111
+ 1. **Hash Algorithm Selection**
112
+ - Use `sha384` (default) for a good balance of security and performance
113
+ - Consider `sha512` for maximum security
114
+ - Avoid `sha1` as it's considered cryptographically weak
115
+
116
+ 2. **CORS Configuration**
117
+ - Ensure your CDN or hosting service supports CORS
118
+ - Set appropriate `Access-Control-Allow-Origin` headers
119
+ - Use `bypassDomains` for trusted domains that don't support CORS
120
+
121
+ 3. **Performance Optimization**
122
+ - Enable `ignoreMissingAsset` in development for faster builds
123
+ - Use debug mode only when troubleshooting
124
+
125
+ 4. **Security Considerations**
126
+ - Always use HTTPS for external resources
127
+ - Regularly update the plugin for security fixes
128
+ - Keep your dependencies up to date
129
+
130
+ ## Troubleshooting
131
+
132
+ ### Common Issues
133
+
134
+ 1. **Missing Integrity Attributes**
135
+ - Check if the file is in your build output
136
+ - Verify the file path is correct
137
+ - Enable debug mode to see detailed logs
138
+
139
+ 2. **CORS Errors**
140
+ - Ensure the resource supports CORS
141
+ - Add the domain to `bypassDomains` if needed
142
+ - Check network tab for CORS headers
143
+
144
+ 3. **Build Performance**
145
+ - Use `ignoreMissingAsset` if you have many external resources
146
+ - Disable debug mode in production
147
+ - Consider using a CDN for external resources
148
+
149
+ ### Debug Mode
150
+
151
+ Enable debug mode to see detailed logs:
152
+
153
+ ```javascript
154
+ sri({
155
+ debug: true
156
+ })
157
+ ```
158
+
159
+ This will show:
160
+ - Asset processing steps
161
+ - SRI hash computation
162
+ - CORS checks
163
+ - Missing asset warnings
164
+
165
+ ## Contributing
166
+
167
+ We welcome contributions! Here's how you can help:
168
+
169
+ 1. Fork the repository
170
+ 2. Create your feature branch: `git checkout -b feature/my-feature`
171
+ 3. Commit your changes: `git commit -am 'Add some feature'`
172
+ 4. Push to the branch: `git push origin feature/my-feature`
173
+ 5. Submit a pull request
174
+
175
+ Please make sure to:
176
+ - Update the documentation
177
+ - Add tests for new features
178
+ - Follow the existing code style
179
+ - Update the CHANGELOG.md
180
+
181
+ ## License
182
+
183
+ This project is licensed under the MIT License - see the LICENSE file for details.
184
+
185
+ ## Support
186
+
187
+ - Create an issue for bug reports
188
+ - Star the project if you find it useful
189
+ - Follow the author for updates
package/dist/index.cjs CHANGED
@@ -9,7 +9,8 @@ const DEFAULT_OPTIONS = {
9
9
  algorithm: 'sha384',
10
10
  bypassDomains: [],
11
11
  crossorigin: 'anonymous',
12
- debug: false
12
+ debug: false,
13
+ ignoreMissingAsset: false
13
14
  };
14
15
 
15
16
  function log(message, options) {
@@ -54,12 +55,22 @@ async function externalResourceIsCorsEnabled(url, options) {
54
55
  function isBypassDomain(url, bypassDomains = []) {
55
56
  if (!bypassDomains.length) return false;
56
57
  try {
57
- const parsedUrl = url.startsWith('//')
58
- ? new URL(`http:${url}`)
59
- : new URL(url, 'http://dummy');
60
- return bypassDomains.some(
61
- (domain) => parsedUrl.hostname === domain ||
62
- parsedUrl.hostname.endsWith(`.${domain}`)
58
+ let hostname = url;
59
+
60
+ if (hostname.startsWith('http://')) {
61
+ hostname = hostname.slice(7);
62
+ } else if (hostname.startsWith('https://')) {
63
+ hostname = hostname.slice(8);
64
+ } else if (hostname.startsWith('//')) {
65
+ hostname = hostname.slice(2);
66
+ }
67
+
68
+ hostname = hostname.split('/')[0];
69
+
70
+ hostname = hostname.split(':')[0];
71
+
72
+ return bypassDomains.some(domain =>
73
+ hostname === domain || hostname.endsWith(`.${domain}`)
63
74
  );
64
75
  } catch (e) {
65
76
  return false;
@@ -153,8 +164,14 @@ async function processTag(tag, url, options, bundle, base = '') {
153
164
  }
154
165
 
155
166
  if (!bundleItem) {
156
- log(`Available bundle keys:`, options);
157
- Object.keys(bundle).forEach(key => log(`- ${key}`, options));
167
+ log(`Bundle item not found for ${url}`, options);
168
+ if (!options.ignoreMissingAsset) {
169
+ console.warn(`${LOG_PREFIX} Asset not found in bundle: ${url}`);
170
+ log(`Available bundle keys:`, options);
171
+ Object.keys(bundle).forEach(key => log(`- ${key}`, options));
172
+ } else {
173
+ log(`Ignoring missing asset due to ignoreMissingAsset option`, options);
174
+ }
158
175
  return tag;
159
176
  }
160
177
 
@@ -180,6 +197,9 @@ async function processTag(tag, url, options, bundle, base = '') {
180
197
  }
181
198
  } catch (error) {
182
199
  log(`Error processing bundle item: ${error}`, options);
200
+ if (!options.ignoreMissingAsset) {
201
+ console.error(`${LOG_PREFIX} Failed to process asset: ${url}`, error);
202
+ }
183
203
  }
184
204
 
185
205
  return tag;
@@ -201,6 +221,7 @@ function sri(userOptions = {}) {
201
221
  base = config.base || '';
202
222
  log('Plugin configured in ' + (isBuild ? 'build' : 'dev') + ' mode', options);
203
223
  log(`Base URL: ${base}`, options);
224
+ log(`ignoreMissingAsset: ${options.ignoreMissingAsset}`, options);
204
225
  },
205
226
 
206
227
  async transformIndexHtml(html, ctx) {
package/dist/index.js CHANGED
@@ -7,7 +7,8 @@ const DEFAULT_OPTIONS = {
7
7
  algorithm: 'sha384',
8
8
  bypassDomains: [],
9
9
  crossorigin: 'anonymous',
10
- debug: false
10
+ debug: false,
11
+ ignoreMissingAsset: false
11
12
  };
12
13
 
13
14
  function log(message, options) {
@@ -52,12 +53,22 @@ async function externalResourceIsCorsEnabled(url, options) {
52
53
  function isBypassDomain(url, bypassDomains = []) {
53
54
  if (!bypassDomains.length) return false;
54
55
  try {
55
- const parsedUrl = url.startsWith('//')
56
- ? new URL(`http:${url}`)
57
- : new URL(url, 'http://dummy');
58
- return bypassDomains.some(
59
- (domain) => parsedUrl.hostname === domain ||
60
- parsedUrl.hostname.endsWith(`.${domain}`)
56
+ let hostname = url;
57
+
58
+ if (hostname.startsWith('http://')) {
59
+ hostname = hostname.slice(7);
60
+ } else if (hostname.startsWith('https://')) {
61
+ hostname = hostname.slice(8);
62
+ } else if (hostname.startsWith('//')) {
63
+ hostname = hostname.slice(2);
64
+ }
65
+
66
+ hostname = hostname.split('/')[0];
67
+
68
+ hostname = hostname.split(':')[0];
69
+
70
+ return bypassDomains.some(domain =>
71
+ hostname === domain || hostname.endsWith(`.${domain}`)
61
72
  );
62
73
  } catch (e) {
63
74
  return false;
@@ -151,8 +162,14 @@ async function processTag(tag, url, options, bundle, base = '') {
151
162
  }
152
163
 
153
164
  if (!bundleItem) {
154
- log(`Available bundle keys:`, options);
155
- Object.keys(bundle).forEach(key => log(`- ${key}`, options));
165
+ log(`Bundle item not found for ${url}`, options);
166
+ if (!options.ignoreMissingAsset) {
167
+ console.warn(`${LOG_PREFIX} Asset not found in bundle: ${url}`);
168
+ log(`Available bundle keys:`, options);
169
+ Object.keys(bundle).forEach(key => log(`- ${key}`, options));
170
+ } else {
171
+ log(`Ignoring missing asset due to ignoreMissingAsset option`, options);
172
+ }
156
173
  return tag;
157
174
  }
158
175
 
@@ -178,6 +195,9 @@ async function processTag(tag, url, options, bundle, base = '') {
178
195
  }
179
196
  } catch (error) {
180
197
  log(`Error processing bundle item: ${error}`, options);
198
+ if (!options.ignoreMissingAsset) {
199
+ console.error(`${LOG_PREFIX} Failed to process asset: ${url}`, error);
200
+ }
181
201
  }
182
202
 
183
203
  return tag;
@@ -199,6 +219,7 @@ function sri(userOptions = {}) {
199
219
  base = config.base || '';
200
220
  log('Plugin configured in ' + (isBuild ? 'build' : 'dev') + ' mode', options);
201
221
  log(`Base URL: ${base}`, options);
222
+ log(`ignoreMissingAsset: ${options.ignoreMissingAsset}`, options);
202
223
  },
203
224
 
204
225
  async transformIndexHtml(html, ctx) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-sri4",
3
- "version": "1.8.5",
3
+ "version": "1.8.6",
4
4
  "description": "A Vite plugin to generate Subresource Integrity (SRI) hashes for output files.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",