-
-
Notifications
You must be signed in to change notification settings - Fork 754
feat: support SRI with experiments.css and CssExtractRspackPlugin
#12239
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
✅ Deploy Preview for rspack ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
📦 Binary Size-limit
❌ Size increased by 10.63KB from 47.62MB to 47.63MB (⬆️0.02%) |
CodSpeed Performance ReportMerging #12239 will not alter performanceComparing Summary
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds Subresource Integrity (SRI) support for CSS assets in both experiments.css and the CSS Extract plugin. Previously, CSS chunk hashes would overwrite JavaScript chunk hashes because they shared the same storage variable, causing SRI validation failures for JS files.
Key Changes:
- Introduced
ManifestAssetTypeenum to differentiate between asset types (JavaScript, CSS, extract-css, etc.) - Added separate SRI hash variables (
sriCssHashes,sriExtractCssHashes) for different asset types - Enhanced placeholder generation to include asset type information, preventing hash collisions
Reviewed Changes
Copilot reviewed 22 out of 23 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
crates/rspack_core/src/compilation/mod.rs |
Added ManifestAssetType enum and asset_type field to AssetInfo |
crates/rspack_plugin_sri/src/util.rs |
Updated placeholder generation to include asset type in the placeholder string |
crates/rspack_plugin_sri/src/runtime.rs |
Added separate hash variable generation for JS, CSS, and extract-css; implemented create_link hook |
crates/rspack_plugin_sri/src/lib.rs |
Registered the new create_link hook |
crates/rspack_plugin_sri/src/asset.rs |
Updated asset processing to use asset type when matching placeholders |
crates/rspack_plugin_javascript/src/plugin/impl_plugin_for_js_plugin.rs |
Set ManifestAssetType::JavaScript for JS assets |
crates/rspack_plugin_css/src/plugin/impl_plugin_for_css_plugin.rs |
Set ManifestAssetType::Css for CSS assets |
crates/rspack_plugin_extract_css/src/plugin.rs |
Set ManifestAssetType::Custom("extract-css") for extract-css assets |
crates/rspack_plugin_asset/src/lib.rs |
Set ManifestAssetType::Asset for asset module files |
crates/rspack_plugin_wasm/src/wasm_plugin.rs |
Set ManifestAssetType::Wasm for wasm files |
crates/rspack_binding_api/src/asset.rs |
Added asset_type field mapping for JS bindings |
crates/node_binding/napi-binding.d.ts |
Added TypeScript type definition for assetType |
tests/rspack-test/configCases/sri/css-chunk/* |
Test case for SRI with experiments.css |
tests/rspack-test/configCases/sri/css-chunk-extract/* |
Test case for SRI with CSS extract plugin |
tests/rspack-test/configCases/sri/css-chunk-mix/* |
Test case for SRI with both experiments.css and CSS extract plugin |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
experiments.css and CssExtractRspackPlugin
Summary
close #11907
This is not a issue only affect the css extract plugin. In the runtime, when adding the SRI attribute to
<link>/<script>tags, the only parameter is thechunkId. So, if a chunk contains multiple asset files, similar issues may occur. I tested bothexperiments.cssand themini-css-extract-plugininwebpack-subresource-integrityplugin and encountered the same issue: the hash of the css chunk overrides the hash of the js chunk, causing the js to fail to load and report an SRI error. And also,experiments.cssandmini-css-extract-plugincan be used at the same time.Since the overall processing of SRI can be roughly divided into three steps (for dynamically loaded chunks):
processAssetshook, calculate the hash of the chunk assets based on the asset source and replace the placeholders.<script>and<link>tags are createdTherefore:
sourceTypeof the chunk's modules.asset.sourceandasset.infoare available when processing assets, and it is no longer possible to tell whichsourceTypeit was generated from, it is necessary to add anassetTypein eachrender_manifestfor differentiation. When calculating the hash and replacing, use theassetTypeto generate the placeholder, so as to correctly index the placeholder generated in step 1.createLink/linkPrefetch/linkPreloadhooks have been implemented in the css loading runtime module and css extract loading runtime module in the previous PR, the code can be modified through them. However, the hook of the css extract plugin is not exposed separately in rspack. Therefore, when handling links, the following judgments are needed:createLinkhook, it is necessary to distinguish between experiments css and css extract according to the original code.linkPreloadhook, since js also needs preloading, additional handling for js is also required according tolink.as.As for the way to store hashes, they can be placed in a map and differentiated by using an id prefix, or they can be placed in different maps. Essentially, there is no difference. Here, multiple different maps for storage is chosen.
Related links
Checklist