table icon indicating copy to clipboard operation
table copied to clipboard

Issue with css modules and styling

Open stphnlngdncoding opened this issue 9 years ago • 6 comments

I am using webpack 2 with the '['style-loader', 'css-loader?modules']' value for the 'use' property for .css files. When I remove the modules portion of the string, the table is styled. When I add the modules string back in, the table is unstyled. Can anyone explain what's going on?

stphnlngdncoding avatar Apr 11 '17 00:04 stphnlngdncoding

It appears this is used in the inline require style: https://webpack.js.org/concepts/loaders/#via-require. I have not yet personally upgraded to Webpack 2, so I won't be much help here. Do other css imports work fine without the modules portion?

tannerlinsley avatar Apr 11 '17 14:04 tannerlinsley

I think this is less webpack related and more of a css-loader related problem. I'm not too familiar with the modules option but it appears to locally scope css includes.

https://github.com/css-modules/css-modules

When you include the react-table.css file in your project, the css-modules is probably mangling the css names to keep them locally scoped but react-table isn't using the locally scoped css names in it's code.

Are you taking advantage of CSS-Modules in your project?

Do you have any other third party packages that include a global css that are working correctly?

It looks like there have been some discussions around this: https://github.com/css-modules/css-modules/pull/65

You might have to test for the react-table.css file name and exclude it from the module css loader and have a separate css loader that is specifically for globally scoped css packages.

p.s Sorry I accidentally closed the issue when I posted.

aaronschwartz avatar Apr 11 '17 15:04 aaronschwartz

Thank you for your help. To get it working, I altered my webpack config file to match style files named *.global.css to use the css-loader without the modules param. Then I copied the react-table.css file from the node_modules folder, renamed it to react-table.global.css, and imported it in to the jsx file where the react-table component is rendering. In case anyone else is interested in the same approach, my webpack rules look like this:

{
        test: /\.global.css$/,
        use: [{
          loader: 'style-loader',
        }, {
          loader: 'css-loader',
        }],
      },
      {
        test: /^((?!\.global).)*\.css/,
        use: [{
          loader: 'style-loader',
        }, {
          loader: 'css-loader?modules',
        }],
      },
      {
        test: /\.scss$/,
        use: [{
          loader: 'style-loader',
        }, {
          loader: 'css-loader?modules',
        }, {
          loader: 'sass-loader',
        }],
      },

stphnlngdncoding avatar Apr 11 '17 17:04 stphnlngdncoding

@stphnlngdncoding's solution worked for me, except I just added his /\.global.css$/ test block and subbed in /^((?!\.global).)*\.css/ for my already existing /\.css$/ regex. worked like a charm!

ghost avatar May 15 '18 17:05 ghost

You can exclude "node_modules"

            {
                test: /\.css$/,
                include: [/node_modules/],
                use: [
                    {
                        loader: "style-loader",
                    },
                    {
                        loader: "css-loader",
                        options: {
                            modules: false,
                        },
                    },
                ],
            },
            {
                test: /\.css$/,
                exclude: [/node_modules/],
                use: [
                    { loader: "style-loader" },
                    {
                        loader: "css-loader",
                        options: {
                            modules: true,
                            localIdentName: "[local]__[hash:base64:5]",
                            importLoaders: 1,
                        },
                    },
                ],
            },

flibustier7seas avatar Jun 01 '18 04:06 flibustier7seas

Hi,

I am using Webpack 4.3.x. It looks like this below. Do you have an example of how to change this configuration to accommodate the above mentioned changes?

Thanks

        {
          test: cssRegex,
          exclude: cssModuleRegex,
          use: getStyleLoaders({
            importLoaders: 1,
            modules: true,
            localIdentName: '[name]__[local]__[hash:base64:5]',
            sourceMap: isEnvProduction && shouldUseSourceMap,
          }),
          // Don't consider CSS imports dead code even if the
          // containing package claims to have no side effects.
          // Remove this when webpack adds a warning or an error for this.
          // See https://github.com/webpack/webpack/issues/6571
          sideEffects: true,
        },
        // Adds support for CSS Modules (https://github.com/css-modules/css-modules)
        // using the extension .module.css
        {
          test: cssModuleRegex,
          use: getStyleLoaders({
            importLoaders: 1,
            sourceMap: isEnvProduction && shouldUseSourceMap,
            modules: true,
            getLocalIdent: getCSSModuleLocalIdent,
          }),
        },

ghost avatar Mar 29 '19 16:03 ghost