Closed
Description
You may not even realize this, but your current config supports css modules, it just doesn't use them by default -- i.e. you can use :local
in CSS files and it will generate class names, it just doesn't make all class selectors local by default.
I'm totally fine with global being the default scope for now (much less confusing for beginners) and adding the :local
pseudo-class where I need it, but it would be nice if more human-readable classes were generated. Use something like:
style!css?localIdentName=[path][name]---[local]---[hash:base64:5]!postcss
as the loader config in webpack.
Activity
gaearon commentedon Jul 22, 2016
Can you please explain this on an example? We don’t “officially” support advanced features but they indeed may work by accident 😄 . I don’t know enough about CSS modules to understand what you’re saying, but we’d rather not make them easier to use because we want to be sure that everybody has the same setup. Otherwise can’t just release updates that switch to another CSS solution, for example.
mxstbr commentedon Jul 22, 2016
True, very good catch @modernserf! I'm +1 for this change.
@gaearon
css-loader
supports the:local
wrapper for class names to create localized classes. (i.e. css modules by effort)We didn't specify how it should generate those classes, so they're quite ugly and unreadable – what @modernserf is suggesting is making them more readable.
corbanbrook commentedon Jul 22, 2016
Simply using [local]---[hash:base64:5] is nice solution. This will output classnames like
button---2VzVQ
. Including the path and filename is a tad excessive imo and just makes it difficult to grok when scanning over the outputted markup.gaearon commentedon Jul 22, 2016
I don’t think we want to support this. I would actually like to turn this feature off.
The reason is we might want to migrate away from webpack in the future. We’d need to make our CSS files are as vanilla CSS as possible (aside from minor stuff like relative dependency paths).
I would accept a PR that forbids extra features if possible.
gaearon commentedon Jul 22, 2016
(Btw I totally understand the benefits of local scoping. We just don’t think it’s a good default for people learning React, and it’s way too custom syntax that is incompatible with many other tools. We’d rather move into direction of encouraging CSS in JS for better encapsulation.)
mxstbr commentedon Jul 22, 2016
I think it's not possible to turn off local scope by default (ref webpack-contrib/css-loader#193), but we can simply set the generated name to whatever the name is anyway. Give me 5 minutes to try that.
modernserf commentedon Jul 22, 2016
I regret bringing this to your attention.
gaearon commentedon Jul 22, 2016
@modernserf I understand your frustration but it’s still better than if we change the bundler in a few months and all your styles would break. Having to back out of infrastructure changes (e.g. super fast new bundler) because people relied on webpack in ways like this would be super sad.
mxstbr commentedon Jul 22, 2016
I kind of agree with @modernserf though, this is a nice escape hatch for people to explicitly use CSS modules without needing to eject. It's not like we document or encourage this anywhere…
gaearon commentedon Jul 22, 2016
People will use it whether we document it or not, and then “create-react-app broke my app” will become the next javascript f*****e.
gaearon commentedon Jul 22, 2016
A semver-compatible patch update can literally break people’s builds because of it.
mxstbr commentedon Jul 22, 2016
I still haven't found a way to disable it properly, it'll break peoples builds either way.
gaearon commentedon Jul 22, 2016
We can add a custom loader that throws on non-standard syntax.
26 remaining items
bondz commentedon Jan 14, 2017
@geelen @oscar-b just checked out styled-components really cool. 👍
odigity commentedon Jan 15, 2017
@oscar-b It's on my short-list of study topics for the next two days.
DenisIzmaylov commentedon Mar 27, 2017
Last 3 years we've developed a lot of large projects for commercial and Enterprise segments using React. We've tried everything including LESS, Stylus, CSS modules, styled-components, SASS and we found that JSS have the best DX ever. With using react-jss by Dan.
With CSS you can control your styles by CSS class only while in JSS you can control every property, every value to reduce unused CSS as well as it possible.
But there is only one problem in JSS - is learning curve. It may be hard to switch from CSS notation to JSS in a few days or even couple weeks.
To solve this problem we've made PreJSS - universal CSS to CSS-in-JS adapter which is actually became platform and ecosystem like webpack or babel.
PreJSS have a three basic concepts PreJSS Styles Declarations, CSS Parser, PreJSS adapter.
PreJSS Styles Declarations - is CSS styles in Literal Template Strings like we love:
CSS parser is an optional package with function which takes CSS/SCSS/any code and transforms it to JSS format. By default PreJSS uses PostCSS, but there is a lot of other nice CSS parsers.
CSS adapter is main abstraction, set of 3 independent functions -
prepare
(e.g. remove JS comments from your styles),parse
(by CSS parser) andfinalize
to adopt JSS to any custom JSS library such as Radium, React Native, React Desktop, etc.That's how PreJSS makes CSS world simpler:
cr101 commentedon Mar 27, 2017
@DenisIzmaylov Since styled-components was your inspiration for PreJSS why didn't you make improvements to styled-components instead of creating yet another CSS plugin to style your components?
Also, what are the benefits of using PreJSS over styled-components?
ro-savage commentedon May 19, 2017
@gaearon @Timer
I know there has been gone through before, I personally use a fork of react-scripts to handle css modules.
However now v1.0.0 is out and using webpack 2.0 (e.g. not likely to change soon). I suggest revisiting CSS Modules.
My current config supports both
/\.modules.css$/
) then it's loaded as a CSS module,/[^\.modules]\.css$/
).This allows both, its an extra only people who choose to use get, doesn't interfere with loading CSS from libs, with webpack2 now in place I don't think CSS Module support is going to die any time soon, and of course CSS Modules rocks according to stateofjs with 97% satisfaction.
Thoughts?
gaearon commentedon May 19, 2017
Can you file a new issue about this? Let’s say I’m open to supporting it with an explicit filename suffix.
czebe commentedon May 19, 2017
@ro-savage thanks for your input, this sounds good, I'll try your config.
ro-savage commentedon May 20, 2017
@gaearon @czebe
CSS Module Support: Issue #2278 and PR #2285
faceyspacey commentedon Jun 29, 2017
my 2 cents: to me the biggest thing is having cacheable traditional stylesheets and avoiding the render/runtime overhead of calculating styles in every render (which you need to do on both the client and server if doing SSR--not good). I avoid true css-in-js solutions for this precise reason.
So a solution that could be great for CRA--and one it doesn't need to implement itself--is a loader that removes css-in-js and converts it to stylesheets. That can be done far down the line in webpack land, long after you have ejected. Someone just needs to make this.
That way during development CRA doesn't need to support anything new. And that way developers can have the option to achieve optimum performance if they ever want it.
Static css-in-js--the new hotness lol