Closed OliverJAsh closed 4 months ago
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 css-loader
loaders with different localIdentName
s, another solution is using - https://webpack.js.org/configuration/module/#rulelayer, so you can setup different layers, so you can split your application on layers.
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 composes
(https://github.com/webpack-contrib/css-loader/blob/master/test/fixtures/modules/composes/composes.css#L44C3-L44C11)
Also my recomendation is avoid such situations and give unique name for the each class - i.e. have button-collection
/button-photo
/etc
Feel free to feedback
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
composes
(https://github.com/webpack-contrib/css-loader/blob/master/test/fixtures/modules/composes/composes.css#L44C3-L44C11)Also my recomendation is avoid such situations and give unique name for the each class - i.e. have
button-collection
/button-photo
/etc
Unfortunately neither of these suggestions solve the problem.
anyway we have https://webpack.js.org/configuration/module/#ruleissuer, so you can setup multiple
css-loader
loaders with differentlocalIdentName
s, another solution is using - https://webpack.js.org/configuration/module/#rulelayer, so you can setup different layers, so you can split your application on layers.
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 getLocalIdent
like a function and get issuer
from LoaderContext
(i.e. loaderContext._module
), it is hacky, but will work, and you can see which things you can use to achive your behaviour.
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 information