3

I am locally scoping my css files by using

modules: true,
localIdentName: "[name]__[local]_[hash:base64:5]"

in webpack.config.dev and prod.js. I am able to use the styles i have defined for the components using import style from './Component.css'. Now i am trying to use the react-select third party component. Now the component has its predefined style which i want to use. To use it i import the style as follows

import 'react-select/dist/react-select.css'

But the style doesn't gets applied. How to enable the predefined style for the imported component.

1 Answer 1

5

You could use a resource query to treat CSS imports differently depending on the query.

This way you could import your regular CSS with the ?external query parameter.

module.exports = {
  //...
  module: {
    rules: [
      {
        test: /.css$/,
        oneOf: [
          {
            // import 'react-select/dist/react-select.css?external'
            resourceQuery: /external/, 
            use: ['style-loader', 'css-loader']
          },
          {
            use: ['style-loader', 'css-loader'],
            options: {
              modules: true,
              localIdentName: '[name]__[local]_[hash:base64:5]'
            }
          }
        ]
      }
    ]
  }
};
Sign up to request clarification or add additional context in comments.

5 Comments

I am new to this so my questions might seem silly. In the webpack.config.dev.js i am going to add the following rule-'{ // import 'react-select/dist/react-select.css?external' resourceQuery: /external/, use: ['style-loader', 'css-loader'] },' Now in the <Select /> component how to use the ?external query parameter? Or do i have to use the ?external in my own build components?
I think i got your hint. In external component <Select/> i have to use import 'react-select/dist/react-select.css?external' after adding the above rule. Let me know if i am incorrect and i am going to try it. Ignore my previous comment.
@Braj The comment in my answer, import 'react-select/dist/react-select.css?external', was just an example of how to use it in your app. If you import a CSS file with ?external at the end of the file name, Webpack will treat that as regular CSS and not CSS modules. Just write import 'react-select/dist/react-select.css?external' once in your App, and the react-select CSS will be part of your CSS as expected.
I am gonna try it and update you. Also will covering the webpack topic give me insight into how webpack works in relation with css and other files?
Works like a charm. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.