How darcas/rollup-sub-resource-integrity works
Main Components
• Rollup/Vite Build Process: Represent the overall bundling environment where the plugin is invoked.
• Rollup Plugin (SubResourceIntegrity): This is your core component that hooks into the build process, particularly during the writeBundle phase.
• HTML File Processor: A component within the plugin that is responsible for scanning HTML files, locating <script> and <link> tags, and identifying the resources (via “src” and “href” attributes).
• Hashing Module: This component handles the calculation of SRI hashes using the chosen algorithm (sha256, sha384, or sha512).
• File I/O Module: Manages the reading of resource files and re-writing updated HTML files with the new integrity attributes.
• Configuration: Although minimal in the file tree, note that configuration elements (like the algorithm parameter in the plugin call) guide the plugin behavior.
Relationships and Interactions
• The Build Process (Rollup/Vite) calls the plugin as part of its bundling lifecycle.
• During the writeBundle phase, the plugin triggers the HTML File Processor.
• The HTML File Processor reads the output HTML files from the file system (via the File I/O Module).
• For each resource found in the HTML, the Hashing Module calculates the integrity hash.
• The plugin then updates the HTML by embedding the calculated ‘integrity’ attribute and writes the file back.
• Configuration settings (like the chosen hash algorithm) feed into the Hashing Module.
Architectural Patterns and Design Principles
• Modular Design: The plugin separates concerns by dividing responsibilities among scanning, hashing, and file I/O.
• Integration via Hooks: It leverages the Rollup build hooks (writeBundle) to integrate smoothly with the build pipeline.
• Configurability: Through a simple parameter (e.g., choosing the hash algorithm), the plugin showcases a plug-and-play design suitable for various build environments.
Relevant Technologies and Frameworks
• TypeScript: The code is written in TypeScript (see src/index.ts and tsconfig.json), which enhances type safety and maintainability.
• Rollup (and integration into Vite): The plugin is intended for these ecosystems.
• NPM: The package is managed via npm and is available as @darcas/rollup-sub-resource-integrity.
Putting It All Together
• To the left/top, show the Rollup/Vite Build Process triggering the plugin.
• To the right/bottom, illustrate the file system representing the source HTML file input and the output after injection of the SRI attributes.
Happy diagramming!