-
-
Notifications
You must be signed in to change notification settings - Fork 608
Output unique class names for duplicated classes across multiple chunks #1602
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
Comments
Sorry, we can't get chunk name/chunk hash/etc while we are building modules, anyway we have https://webpack.js.org/configuration/module/#ruleissuer, so you can setup multiple Also CSS modules support - https://github.com/webpack-contrib/css-loader?tab=readme-ov-file#separating-interoperable-css-only-and-css-module-features, so you can create basic button component and use Also my recomendation is avoid such situations and give unique name for the each class - i.e. have Feel free to feedback |
Unfortunately neither of these suggestions solve the problem.
I don't think it would feasible to do this for all of the places where this issue may occur in a large application, such as the one we have at Unsplash. |
I understand, I’m just offering solutions, we simply don’t have chunks available at the time of building the modules Another solution - use Feel free to feedback |
Related issues:
Feature Proposal
When CSS Modules is used in combination with code splitting, webpack may duplicate CSS across multiple chunks.
This is problematic because CSS files may be loaded in an arbitrary order resulting in unpredictable behaviour.
I believe we could mitigate this issue by ensuring duplicated classes have unique class names for each chunk they appear within. This could be achieved by adding a new template string to
localIdentName
that represents the current chunk. This would be similar to[chunkhash]
as supported byoutput
.Feature Use Case
The reduced test case outlined below can be found here: https://github.com/OliverJAsh/webpack-css-order-test/tree/code-splitting
Let's say we have a project with this structure:
We have a
Button
component:src/Button.js
:src/Button.css
:Both
Collections.js
andPhotos.js
importButton.js
as a component and compose its styles to overridecolor
:src/Photos.js
:src/Photos.css
:src/Collections.js
:src/Collections.css
:Within
main.js
, we dynamically importPhotos.js
andCollections.js
. Pretend for this test-case that these imports are only triggered on page navigation:Because
Button.js
is only used within 2 dynamic imports, and not directly in themain.js
, the contents ofButton.css
get duplicated and prepended toPhotos.css
andCollections.css
.The resulting CSS files:
dist/Photos.css
:dist/Collections.css
:Having the
.src-Button__button
rule duplicated across chunks makes it possible to undo the override set inPhotos.css
if the chunks are loaded in a specific order. The load order may vary depending on which components are rendered on the page, and how the user navigates around the application.We can illustrate the problem by having a user navigate our app as follows:
The CSS chunk for
Collections
is loaded afterPhotos
, causing the.src-Button__button
rule to be loaded a second time later in the cascade order, undoing the override we've set inPhotos.css
.Please paste the results of
npx webpack-cli info
here, and mention other relevant informationThe text was updated successfully, but these errors were encountered: