Skip to content
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

CSS-Modules don't work with storybook #2320

Closed
krsnaa opened this issue Nov 15, 2017 · 7 comments
Closed

CSS-Modules don't work with storybook #2320

krsnaa opened this issue Nov 15, 2017 · 7 comments

Comments

@krsnaa
Copy link

krsnaa commented Nov 15, 2017

If you use https://github.com/ro-savage/react-scripts-cssmodules with create-react-app to incorporate CSS-Modules, storybook does not register the styles. The styles work perfectly when running directly on react.

@ndelangen
Copy link
Member

ndelangen commented Nov 15, 2017

That script extends/overrides the webpack config of CRA.
https://github.com/ro-savage/react-scripts-cssmodules/blob/master/config/webpack.config.dev.js

Provide storybook with the relevant webpack config, and it should work too.
https://storybook.js.org/configurations/custom-webpack-config/#full-control-mode

@cdtinney
Copy link

cdtinney commented Dec 14, 2017

Note that you don't have to use Full Control Mode.

You can just set the options for css-loader:

e.g., in .storybook/webpack.config.js:

module.exports = {
  module: {
    rules: [
      {
        test: /\.scss$/,
        loaders: [
          require.resolve('style-loader'),
          {
            loader: require.resolve('css-loader'),
            options: {
              importLoaders: 1,
              modules: true,
              localIdentName: '[name]__[local]___[hash:base64:5]',
            },
          },
          require.resolve('sass-loader')
        ],
      },
    ],
  },
}

@ndelangen
Copy link
Member

Thanks for posting your working solution @cdtinney ♥️

@mkamranhamid
Copy link
Contributor

@cdtinney thanks man you are a lifesaver

@EntityB
Copy link

EntityB commented Dec 9, 2019

I have ecountered that localIndent configuration has changed for css loader. My currently working configuration of webpack.config.js is something like:

const path = require('path');

// Export a function. Accept the base config as the only param.
module.exports = async ({ config, mode }) => {
    // `mode` has a value of 'DEVELOPMENT' or 'PRODUCTION'
    // You can change the configuration based on that.
    // 'PRODUCTION' is used when building the static version of storybook.

    // Make whatever fine-grained changes you need
    config.module.rules.push({
        test: /\.scss$/,
        loaders: [
            require.resolve('style-loader'),
            {
                loader: require.resolve('css-loader'),
                options: {
                    importLoaders: 1,
                    modules: {
                        mode: 'local',
                        localIdentName: '[path][name]__[local]--[hash:base64:5]',
                        // localIdentName: '[sha1:hash:hex:4]',
                        context: path.resolve(__dirname, 'src'),
                        hashPrefix: 'my-custom-hash',
                    },
                },
            },
            require.resolve('sass-loader')
        ],
    });

    // Return the altered config
    return config;
};

@murtazahuzaifa
Copy link

is there a way to use all style formats like ( .css, .scss, .module.css, .module.scss ) in a single project, I tried it to do like the code shown below, but not working.

main.js
`const path = require('path');

module.exports = {
"stories": [
"../src//*.stories.mdx",
"../src/
/*.stories.@(js|jsx|ts|tsx)"
],
"addons": [
"@storybook/addon-links",
"@storybook/addon-essentials",
'@storybook/preset-create-react-app',
],

webpackFinal: async (config, { configType }) => {
// configType has a value of 'DEVELOPMENT' or 'PRODUCTION'
// You can change the configuration based on that.
// 'PRODUCTION' is used when building the static version of storybook.

// Make whatever fine-grained changes you need
config.module.rules.push({
  test: /\.scss$/,
  use: ['style-loader', 'css-loader', 'sass-loader'],
  include: path.resolve(__dirname, '../'),
});

config.module.rules.push({
  test: /\.module.scss$/,
  use: ['style-loader', 'css-loader?modules=true', 'sass-loader?modules=true'],
  include: path.resolve(__dirname, '../'),
});

// Return the altered config
return config;

},
};`

@ksbulgakov
Copy link

If smb still has problem of using css-modules in storybook with cra or nextjs this solved the issue for me - https://github.com/Negan1911/storybook-css-modules-preset

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants