vite-plugin-sri4
v3.1.0
Published
A Vite plugin to generate Subresource Integrity (SRI) hashes for output files.
Maintainers
Readme
vite-plugin-sri4
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.
Table of Contents
- Features
- Installation
- Usage
- Plugin Options
- Example Project
- Best Practices
- Troubleshooting
- Contributing
- Inspiration
- License
Features
- Automatic SRI Generation: Computes SRI hashes for assets (chunks and files) using a configurable algorithm (default is
sha384). - HTML Injection: Automatically injects
integrityandcrossoriginattributes into<script>and<link>tags in your HTML. - CORS Support Check: For external resources, a CORS check is performed to verify access via
Access-Control-Allow-Origin. - Bypass Domains: Option to specify domains to bypass SRI injection.
- Missing Asset Handling: Configurable warning suppression for missing assets.
- Robust Content Support: Handles various content types including strings, Buffer, and Uint8Array.
- Vite Compatibility: Compatible with Vite 6.0 and 7.0.
Installation
npm install vite-plugin-sri4 --save-devUsage
Add the plugin to your Vite configuration by updating your vite.config.js or vite.config.ts file:
// vite.config.js
import { defineConfig } from 'vite';
import sri from 'vite-plugin-sri4';
export default defineConfig({
plugins: [
sri({
// Optional. The security hash algorithm. Defaults to "sha384".
algorithm: 'sha384',
// Optional. Domains to bypass SRI injection.
bypassDomains: ['example.com'],
// Optional. Suppress warnings for missing assets.
ignoreMissingAsset: false,
// Optional. Enable debug logging.
debug: false
})
]
});Example HTML Output
Input:
<script src="app.js"></script>
<link rel="stylesheet" href="style.css">Output:
<script src="app.js" integrity="sha384-..." crossorigin="anonymous"></script>
<link rel="stylesheet" href="style.css" integrity="sha384-..." crossorigin="anonymous">Plugin Options
algorithm(string): The hash algorithm used for computing SRI. Default is sha384. You may change it to other supported algorithms like sha256.bypassDomains(Array): 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).ignoreMissingAsset(boolean): When true, suppresses warnings for assets that are not found in the bundle. Default is false.debug(boolean): When true, enables detailed debug logging. Default is false.
Example Project
The plugin includes an example project in the example directory that demonstrates its usage with a simple Vite application. To try it:
- Clone the repository
- Install dependencies:
npm install cd example npm install - Build the example:
npm run build - Check the generated
dist/index.htmlto see the SRI hashes in action
The example project shows:
- Basic setup with Vite
- SRI hash generation for JS and CSS files
- Handling of hashed filenames
- Static file handling
Best Practices
Hash Algorithm Selection
- Use
sha384(default) for a good balance of security and performance - Consider
sha512for maximum security - Avoid
sha1as it's considered cryptographically weak
- Use
CORS Configuration
- Ensure your CDN or hosting service supports CORS
- Set appropriate
Access-Control-Allow-Originheaders - Use
bypassDomainsfor trusted domains that don't support CORS
Performance Optimization
- Enable
ignoreMissingAssetin development for faster builds - Use debug mode only when troubleshooting
- Enable
Security Considerations
- Always use HTTPS for external resources
- Regularly update the plugin for security fixes
- Keep your dependencies up to date
Troubleshooting
Common Issues
Missing Integrity Attributes
- Check if the file is in your build output
- Verify the file path is correct
- Enable debug mode to see detailed logs
CORS Errors
- Ensure the resource supports CORS
- Add the domain to
bypassDomainsif needed - Check network tab for CORS headers
Build Performance
- Use
ignoreMissingAssetif you have many external resources - Disable debug mode in production
- Consider using a CDN for external resources
- Use
Debug Mode
Enable debug mode to see detailed logs:
sri({
debug: true
})This will show:
- Asset processing steps
- SRI hash computation
- CORS checks
- Missing asset warnings
Contributing
We welcome contributions! Here's how you can help:
- Fork the repository
- Create your feature branch:
git checkout -b feature/my-feature - Commit your changes:
git commit -am 'Add some feature' - Push to the branch:
git push origin feature/my-feature - Submit a pull request
Please make sure to:
- Update the documentation
- Add tests for new features
- Follow the existing code style
- Update the CHANGELOG.md
Inspiration
This project was inspired by vite-plugin-sri3, which provides subresource integrity for Vite. We've built upon its foundation to create an enhanced version with additional features and improved compatibility.
Other projects that influenced this work:
License
This project is licensed under the MIT License - see the LICENSE file for details.
Support
- Create an issue for bug reports
- Star the project if you find it useful
- Follow the author for updates
