react-static: CSS Modules no longer work
As far as I can tell, #574 may have broken the usage of CSS Modules in React-Static. I’ve upgraded to the latest version and noticed that import styles from 'Foo.scss' no longer works as expected. If you attempt to reference a class name on the styles object, it results in undefined.
Environment
react-static -v: 5.8.2 (first non-working release, last working release is 5.8.1)node -v: 8.9.3yarn --version or npm -v: 5.6.0- Operating system: macOS 10.13.4
Steps to Reproduce the problem
- Go to examples/sass
npm inpm i react-static@5.8.2(first non-working release)- Open
static.config.jsand addmodules: trueoption oncss-loader - Open
App.jsand replaceimport './app.scss'withimport styles from './app.scss' - Add
console.log(styles.content)below theapp.scssimport - Run
npm stage - Notice that the
console.logcall results inundefined
Expected Behavior
If you did the steps outlined above on 5.8.1, it would work. You’d just need to remove the if condition surrounding ExtractTextPlugin.extract call in static.config.js (which was introduced to conform to changes in #574).
Let me know if I’m missing some trivial configuration that would allow me to use CSS Modules. Otherwise, changes in #574 need to be revisited.
/cc @lottamus
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 2
- Comments: 65 (9 by maintainers)
@tannerlinsley @ScriptedAlchemy Please correct me if I’m wrong on all of this, but it’s late and I’ve been looking at the internals of my node_modules for too long to maintain any sanity at this point. This is going to be a long one.
This issue should be reopened - issue still persists in both 5.x and 6.x branches. The PR that broke CSS modules is indeed https://github.com/nozzle/react-static/pull/574. Explanation following below, but first:
TL;DR: Workaround until there’s an official fix:
v5.x
ExtractCssChunksorExtractTextPluginand always add the plugin:ExtractTextPluginandExtractCssChunks, just different APIs obviously - make sure to tailor this to your webpack config.v6.x
ExtractCssChunksand always add the plugin:Something else?
Explanation & details
Here’s how the CSS module pipeline usually works for JSX:
Say you have these two files (I’m using SCSS to use an unrelated fresh asset pipeline, but the same concepts apply if replacing the default css loader with a css modules enabled one manually):
This is the configuration we’ll be using (slightly adjusted from the sass example one). NOTE: the only difference is the
modules: true,option oncss-loader:Before https://github.com/nozzle/react-static/pull/574
For 5.x, using the example configuration:
css-loaderwithmodules: trueoption, webpack will generate the following webpack result module:ExtractTextPluginorExtractCssChunksto get theexports.localsdirectly. If you add that plugin, this will result in the following:After https://github.com/nozzle/react-static/pull/574
By not including
ExtractTextPluginorExtractCssChunksin thenodestage, during bundle step thestatic.[hash].jsfile does NOT have theExtractTextPluginorExtractChunksPluginapplied and we’re stuck with the result from Step 1 above - hence css modules don’t work. Note that theprodstep result still looks and works fine but since the HTML is prebuilt using thenodestep output, the result is that none of the class names are applied (they’re all accessible to components viastyles.locals.<className>rather thanstyles.<className>6.x ?
There’s essentially the same problem, except we’re using newer versions of everything so the APIs are a bit different. Core of the issue is still the same - we’re not using
ExtractCssChunksplugin for thenodestage which means that css modules built withcss-loaderare not getting properly transformed for HTML generation.Solutions?
Considering we don’t want to be building duplicate CSS files during the node step, we still need a way to generate and extract the correct locals for CSS modules. I’ve posted workarounds at the top but those are fairly dirty and might break when updating React Static. The way I can see it there are a few options here:
nodestage. This seems to work fine and the only issue I could find is the unused css files.style-loaderbut without the actual injecting into the DOM, just this transform basically:CSS modules and React Static
On a side note, considering the adoption rate of CSS modules, it would be great to have more support for them. I can see two options here and I’m willing to work on both:
Which one would align better with the direction of React Static @tannerlinsley?
Sounds like things got a little heated for a bit. Given the conversation I’d like to echo and clarify some things from our contribution guidelines:
Carry on!
With #870 merged, we can now put this to rest 🎉
@JustFly1984 We are not gatsby and if you’re not helping, please don’t be an ass about it as well.
Then give me some feedback dude! You had the author of half this crap working on your private project for free. I was waiting for your review so I could fix the issue once and for all. The fix I was trying to apply for you would have been the PR I open to this project.
Emotions aside. I do apologize that we didn’t pull through for you. If you do end up using either my tools or ones I’m accociated with. feel free to reach out, I am always willing to support our users and care about the success of their projects
It does not support CSS modules, and it will not support it for long time. This is a thread about CSS modules. I’m telling everybody who does need CSS modules as I am that it does not resolving this issue since 5.8.2 version. I was pointing this out for long time, and wasted my time.
You don’t have to apologize. We also need to thank you for helping solve this problem. I hope this framework is getting better and better.
hey guys, sorry for such a delay.
Regarding my css loader
extract-css-chunks-webpack-pluginwhich is part of react static. Im about to release a next tag build on npm which should get HMR and CSS modules working again.This assumes you are on the new RS6
UPDATE:::: its on npm under @next tag
@tannerlinsley sorry bro, I didn’t mean to be considered trash talking. Peace everybody.
Guys don’t waste your time with react-static, I’ve migrated every project I had to gatsby, and gatsby is faster, reliable and does support css modules out of the box without the need to hustle with webpack config, although you can do that too.
Thanx, that means I can try to update! Can’t wait!!!
https://github.com/faceyspacey/extract-css-chunks-webpack-plugin/tree/webpack-4
npm I extract-css-chunks-webpack-plugin@next@Cinamonas @rohmanhm