Reputation: 71
I am trying to use Storybook v5.0 with my project and I am using React + Webpack4 + CSS Modules. ( I am not using CreateReactApp)
My setup is quite simple, and I was able to setup Storybook without CSS modules fine.
However if I try to edit the custom storybook webpack config to support CSS modules, it errors.
When I run npm run storybook
the error I get is:
ERROR in ./src/components/Test/index.css (./node_modules/css-loader/dist/cjs.js??ref--6-1!./node_modules/postcss-loader/src??postcss!./node_modules/style-loader!./node_modules/css-loader/dist/cjs.js??ref--9-1!./node_modules/postcss-loader/src!./src/components/Test/index.css)
Module build failed (from ./node_modules/postcss-loader/src/index.js):
SyntaxError
(2:1) Unknown word
1 |
> 2 | var content = require("!!../../../node_modules/css-loader/dist/cjs.js??ref--9-1!../../../node_modules/postcss-loader/src/index.js!./index.css");
| ^
3 |
4 | if(typeof content === 'string') content = [[module.id, content, '']];
In my package.json
:
"@storybook/addon-actions": "^5.0.0",
"@storybook/addons": "^5.0.0",
"@storybook/react": "^5.0.0",
My .storybook/webpack.config.js
follows the example on their web site, and looks like this:
const path = require("path");
const stylesLoaders = [
"style-loader",
{
loader: "css-loader",
options: {
modules: true,
localIdentName: "[path]__[local]--[hash:base64:5]"
}
},
"postcss-loader"
];
module.exports = {
module: {
rules: [
{
test: /\.css$/,
loaders: stylesLoaders,
include: path.resolve(__dirname, "../")
}
]
}
};
Any help would be appreciated, as I'd like to use the latest version of Storybook!
Upvotes: 7
Views: 12922
Reputation: 456
How to use css-modules with storybook 6
npm install -D storybook-css-modules
Next, update .storybook/main.js to the following:
// .storybook/main.js
module.exports = {
stories: [
// ...
],
addons: [
// Other Storybook addons
"storybook-css-modules", // 👈 The addon registered here
],
};
Upvotes: 1
Reputation: 74
I had the same issue with the new storybook (v6). Finally, I am able to resolve the error when I applied this solution
Many thanks to him!
Upvotes: 1
Reputation: 23795
This is what worked for me:
// ./.storybook/main.js
module.exports = {
//
webpackFinal: (config, {configType}) => {
const path = require('path');
config.module.rules.push({
test: /\.css$/,
use: ['style-loader', 'css-loader?modules=true'],
include: path.resolve(__dirname, '../'),
});
return config;
}
//
}
CSS
files from <filename>.css
to <filename>.module.css
.Good Luck...
Upvotes: 5
Reputation: 5890
I had the same issue with the new storybook ( v5 ). Finally i made it work after i had to spread config and config.module in the .storybook/webpack.config.js
Accordingly with Storybook's documentation, i used a function for webpack rather than an object: https://storybook.js.org/docs/configurations/custom-webpack-config/
Here is an exemple :
module.exports = ({ config }) => ({
...config,
module: {
...config.module,
rules: [
{
test: /\.(ts|tsx)$/,
exclude: /node_modules/,
use:[
{
loader: require.resolve("babel-loader"),
options: {
presets: [["react-app", { flow: false, typescript: true }]]
}
}
],
include: path.resolve(__dirname, "../")
},
{
test: /\.css$/,
use: [
{
loader: "style-loader",
options: { sourceMap: true }
},
{
loader: "css-loader",
options: {
importLoaders: 1,
modules: true,
localIdentName: "[name]__[local]__[hash:base64:5]"
}
},
{
loader: require.resolve("postcss-loader"),
options: {
ident: "postcss",
plugins: () => [
require("postcss-flexbugs-fixes"),
require("postcss-inline-svg"),
require("postcss-svgo"),
autoprefixer({
browsers: [
">1%",
"last 4 versions",
"Firefox ESR",
"not ie < 9"
],
flexbox: "no-2009"
})
]
}
}
],
include: path.resolve(__dirname, "../")
}
]
}
});
I hope it will help you :)
Upvotes: 1