Skip to content

Conversation

@LingyuCoder
Copy link
Contributor

@LingyuCoder LingyuCoder commented Nov 19, 2025

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 the chunkId. So, if a chunk contains multiple asset files, similar issues may occur. I tested both experiments.css and the mini-css-extract-plugin in webpack-subresource-integrity plugin 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.css and mini-css-extract-plugin can be used at the same time.

Since the overall processing of SRI can be roughly divided into three steps (for dynamically loaded chunks):

  1. Add a runtime module and inject SRI variables and hash placeholders for chunks in its code generation.
  2. In the processAssets hook, calculate the hash of the chunk assets based on the asset source and replace the placeholders.
  3. Insert code that modify the SRI attribute in each runtime where <script> and <link> tags are created

Therefore:

  1. Regarding step 1, if a chunk contains multiple asset files, different placeholders need to be generated for each chunk asset to prevent hashes from overwriting each other. Thus, it is necessary to handle them separately according to the sourceType of the chunk's modules.
  2. Regarding step 2, since only the asset.source and asset.info are available when processing assets, and it is no longer possible to tell which sourceType it was generated from, it is necessary to add an assetType in each render_manifest for differentiation. When calculating the hash and replacing, use the assetType to generate the placeholder, so as to correctly index the placeholder generated in step 1.
  3. Regarding step 3, since createLink/linkPrefetch/linkPreload hooks 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:
    • For createLink hook, it is necessary to distinguish between experiments css and css extract according to the original code.
    • For linkPreload hook, since js also needs preloading, additional handling for js is also required according to link.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

  • Tests updated (or not required).
  • Documentation updated (or not required).

@netlify
Copy link

netlify bot commented Nov 19, 2025

Deploy Preview for rspack ready!

Name Link
🔨 Latest commit 6ffe18c
🔍 Latest deploy log https://app.netlify.com/projects/rspack/deploys/691ec5464e17780008870c4d
😎 Deploy Preview https://deploy-preview-12239--rspack.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@github-actions github-actions bot added release: feature release: feature related release(mr only) team The issue/pr is created by the member of Rspack. labels Nov 19, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Nov 19, 2025

📦 Binary Size-limit

Comparing 6ffe18c to fix(create-rspack): use current package manager in templates (#12248) by neverland

❌ Size increased by 10.63KB from 47.62MB to 47.63MB (⬆️0.02%)

@codspeed-hq
Copy link

codspeed-hq bot commented Nov 19, 2025

CodSpeed Performance Report

Merging #12239 will not alter performance

Comparing feat/sri-css-chunks (6ffe18c) with main (92f28c7)

Summary

✅ 17 untouched

@LingyuCoder LingyuCoder changed the title feat: support sri in experiments css and extract css feat: support SRI in experiments css and extract css Nov 20, 2025
@LingyuCoder LingyuCoder changed the title feat: support SRI in experiments css and extract css feat: support SRI in experiments css and css extract Nov 20, 2025
@LingyuCoder LingyuCoder marked this pull request as ready for review November 20, 2025 07:37
Copilot AI review requested due to automatic review settings November 20, 2025 07:37
Copy link
Contributor

Copilot AI left a 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 ManifestAssetType enum 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.

@LingyuCoder LingyuCoder requested a review from JSerFeng November 20, 2025 08:00
@LingyuCoder LingyuCoder changed the title feat: support SRI in experiments css and css extract feat: support SRI with experiments.css and CssExtractRspackPlugin Nov 20, 2025
@LingyuCoder LingyuCoder merged commit 480f74a into main Nov 20, 2025
55 checks passed
@LingyuCoder LingyuCoder deleted the feat/sri-css-chunks branch November 20, 2025 10:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release: feature release: feature related release(mr only) team The issue/pr is created by the member of Rspack.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: support SubResourceIntegrity plugin with CssExtractRspackPlugin

3 participants