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

How to subscribe css modules rule? #41

Closed
guilhermeutzig opened this issue Sep 15, 2018 · 3 comments
Closed

How to subscribe css modules rule? #41

guilhermeutzig opened this issue Sep 15, 2018 · 3 comments
Labels
question Item is a question, not an issue

Comments

@guilhermeutzig
Copy link

Hello, I need to remove the css generating with a hash on the end of each element.

How can I do that? I already tried just pushing into the rules but still not subscribe, the classes are still generating with hashes.

Code:

config.module.rules.push({
    test: /\.css$/,
    loader: 'css-loader',
    query: {
      modules: true,
      localIdentName: '[name]'
    }
  })

Evidence:
image

@lukeed lukeed added the question Item is a question, not an issue label Sep 15, 2018
@lukeed
Copy link
Owner

lukeed commented Sep 15, 2018

Hey, are you sure you want to do this? 😸 CSS modules are actually pretty amazing – the ugly hash only shows up for development. For production, the selectors are compressed to much shorter, unique selectors. This will often save bytes on the classnames alone 👍 You can inspect the classNames on https://pwa.cafe for a production example.

As for actually disabling these, you have two options:

  1. Wrap everything in :global() modifier. This will tell css-loader to leave these selector as is and not modify the name.

    :global(.gs-header) {
      background: blue;
    }

See here for more info

  1. You have to modify the current config & disable modules – instead of adding a new CSS rule that still uses modules (modules: true) but with a different naming convention. The one PWA includes runs first, so yours never gets to run.

Also, simply running unshift() instead of push() won't work either because it adds css-loader without any style-loader or postcss-loader chaining.

 config.module.rules.forEach(rule => {
   if (Array.isArray(rule.use)) {
     rule.use.forEach(obj => {
       if (obj.loader && obj.loader === 'css-loader') {
         obj.options.modules = false; // DISABLE
       }
     });
   }
 });

Hope that helps!

@lukeed lukeed closed this as completed Sep 15, 2018
@guilhermeutzig
Copy link
Author

I don't really like to work with hashes on my CSS, but it's a personal thing, nothing more than this hahaha.

Thank you very much for this, solved my problem!!

@lukeed
Copy link
Owner

lukeed commented Sep 15, 2018

Yeah, I can relate 😆 I didn't like CSS modules at all at first, but I've definitely warmed up to them since.

Great! 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Item is a question, not an issue
Projects
None yet
Development

No branches or pull requests

2 participants