-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
Storybook default webpack config does not support CRA v2 w/ CSS modules #4306
Comments
For anyone else who created a CRA v2 project that uses CSS modules, I extended the default webpack config by doing the following:
// ./.storybook/webpack.config.js
const cssModuleRegex = /\.module\.css$/;
module.exports = {
module: {
rules: [
{
test: cssModuleRegex,
loaders: [
require.resolve('style-loader'),
{
loader: require.resolve('css-loader'),
options: {
importLoaders: 1,
modules: true,
localIdentName: '[name]__[local]___[hash:base64:5]'
}
}
]
}
]
}
}; Now CSS modules should work for your CRA + Storybook project. |
If we are talking about compatibility with CRA2, I think we need to support as much as possible by default. For example, in Do you want to PR the better CRA2 compatibility for css modules? |
That will be cool 👍
I would say yes |
Thanks for the snippet @matthamil For anybody who wants to use SCSS modules, you can modify it slightly to use:
|
@chadfawcett please join our discord: https://discord.gg/sMFvFsG So we can help you faster. |
@igor-dv @chadfawcett and I discussed and he's on board what we think should be done. We could use your advice and expertise about presets!
|
I like the preset idea. The similar preset for angular is located in How can we detect that somebody is using cra ? |
@igor-dv Since the webpack used to be solely based on CRA's webpack config, would it be okay to just narrow down that scope to apply to all Alternatively we could do something similar to the angular check and load a file, like What are your thoughts? Edit: The react |
Similarly, the new |
@robcaldecottvelo You've tried the alpha? I thought the fragment syntax should be supported there. We're releasing 4.0.0 rc today |
Hello, I am trying out the latest version of storybook (
Looks like the following custom webpack resolves the problem:
Maybe it only supports the extension |
Hmm, now it complains about |
@weyert you'll need to do something special for now. Per the conversation earlier in this thread, it was decided that compatibility with CRA2 (which includes support for sass & css modules out of the box) will be included in the future. |
Sorry, somehow I thought it was included as part of today's release! |
for anyone else who comes across this I had issues with sass-loader not liking being used twice, so i came up with this solution
|
This is how I ended up getting css modules to work // .storybook/webpack.config.js
const setCssModulesRule = rule => {
const nextRule = rule;
const cssLoader = rule.use[1]; // admittedly lazy, but the array was mixed types 🤷♀️
const nextOptions = { ...cssLoader.options, modules: true }
cssLoader.options = nextOptions;
return nextRule;
}
module.exports = async ({ config, mode }) => {
const rules = config.module.rules.map(rule => {
const isCssRule = rule.test.toString().indexOf('css') !== -1;
let nextRule = rule;
if (isCssRule) {
nextRule = setCssModulesRule(rule);
}
return nextRule;
});
config.module.rules = rules;
return config;
}; |
Bug or support request summary
From https://storybook.js.org/configurations/custom-webpack-config/:
I am assuming that this means that Storybook's default webpack config should be able to handle a new project created by CRA. CRA v2 now has opt-in support for CSS modules. When I created a new CRA project using CRA v2.0.4, I used CSS modules to style a component; however, the component styles do not appear when displaying the component in Storybook.
If this means that I need to extend Storybook's webpack config to add CSS module support, please close this issue. However, it's unclear if Storybook is meant to work out-of-the-box with CRA if this is the case. I can make a PR to add documentation around this if Storybook will not add CSS module support out of the box like CRA v2 has.
Steps to reproduce
I have a reproducible example in this repo.
npx create-react-app my-app
getstorybook
If you import the component that uses CSS modules into
App.js
and runyarn run start
, you can see that the styles are applied correctly. However, if you run Storybook withyarn run storybook
, the styles do not apply.Please specify which version of Storybook and optionally any affected addons that you're running
package.json
Affected platforms
Tested in Chrome v69
Screenshots / Screencast / Code Snippets (Optional)
Repo: https://github.com/matthamil/storybook-css-module-CRA2
Text component when running
yarn run start
:Text component when running
yarn run storybook
:Work summary
Add CSS module support to Storybook's webpack config.
Where to start
Take a look at
./src/Text.js
and./src/Text.module.css
. The CSS module includes one class.text
which applies a red color and 2rem font-size to whatever element is using.text
.Acceptance criteria
Default storybook webpack config can handle CSS modules out of the box.
Who to contact
anyone familiar with Storybook's default webpack config.
The text was updated successfully, but these errors were encountered: