question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Vue with css modules

See original GitHub issue

I’m able to run storybook in my Vue project however the css modules are missing. I’ve also created a new storybook project with the default webpack config and I have the same issue. No styles

i have a component like this

<template>
  <div :class="$style.component">
    red text
  </div>
</template>

<script>
export default {
  name: 'Thing'
}
</script>

<style module>
.component {
  color: red;
}
</style>

and the rendered html is

<div>
  red text
</div>

With no css class. Do I need to do something specific to enable css modules?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
pockacommented, Jan 18, 2020

@chrisjbrown Yeah, you need to configure a webpack rule for .css file in the way Vue Loader Docs describing (if you have files something like SASS, add rules for them too.) I’m not sure vue-cli-plugin handles it or not, though.

Sample Config

// man.js
module.exports = {
  // ...
  webpackFinal(config) {
    const isCssRule = rule =>
      rule.test && rule.test instanceof RegExp && rule.test.test("./file.css");

        return {
      ...config,
      module: {
        ...config.module,
        rules: [
          ...config.module.rules.filter(rule => !isCssRule(rule)),
          {
            test: /\.css$/,
            oneOf: [
              {
                resourceQuery: /module/,
                use: [
                  "style-loader",
                  {
                    loader: "css-loader",
                    options: {
                      modules: true
                    }
                  }
                ]
              },
              {
                use: ["style-loader", "css-loader"]
              }
            ]
          }
        ]
      }
    }
  }
}

CodeSandbox

0reactions
gretchelincommented, Dec 17, 2021

I recently face this problem and unfortunately none of the suggestion above worked for me as is. The idea is correct, but I had to copy the vue-cli actual webpack config implementation for the css rule to make this work instead of using the one shown by pocka or other users above. For people who face the same problem, this is what works for me:

  1. Run vue-cli-service inspect > output.js this will print out the webpack config vue actually uses. Refer to this official guide. Doing this will make sure the rules used by storybook is the one used by vue itself.

  2. Look for the css rule from the file and copy pasted the whole css rule to storybook webpack config module.rule. At the time of this post, this is what the complete css rule look like

/* config.module.rule('css') */
      {
        test: /\.css$/,
        oneOf: [
          /* config.module.rule('css').rule('vue-modules') */
          {
            resourceQuery: /module/,
            use: [
              {
                loader: 'vue-style-loader',
                options: {
                  sourceMap: false,
                  shadowMode: false
                }
              },
              {
                loader: 'css-loader',
                options: {
                  sourceMap: false,
                  importLoaders: 2,
                  modules: {
                    localIdentName: '[name]_[local]_[hash:base64:5]'
                  }
                }
              },
              {
                loader: 'postcss-loader',
                options: {
                  sourceMap: false
                }
              }
            ]
          },
          /* config.module.rule('css').rule('vue') */
          {
            resourceQuery: /\?vue/,
            use: [
              {
                loader: 'vue-style-loader',
                options: {
                  sourceMap: false,
                  shadowMode: false
                }
              },
              {
                loader: 'css-loader',
                options: {
                  sourceMap: false,
                  importLoaders: 2
                }
              },
              {
                loader: 'postcss-loader',
                options: {
                  sourceMap: false
                }
              }
            ]
          },
          /* config.module.rule('css').rule('normal-modules') */
          {
            test: /\.module\.\w+$/,
            use: [
              {
                loader: 'vue-style-loader',
                options: {
                  sourceMap: false,
                  shadowMode: false
                }
              },
              {
                loader: 'css-loader',
                options: {
                  sourceMap: false,
                  importLoaders: 2,
                  modules: {
                    localIdentName: '[name]_[local]_[hash:base64:5]'
                  }
                }
              },
              {
                loader: 'postcss-loader',
                options: {
                  sourceMap: false
                }
              }
            ]
          },
          /* config.module.rule('css').rule('normal') */
          {
            use: [
              {
                loader: 'vue-style-loader',
                options: {
                  sourceMap: false,
                  shadowMode: false
                }
              },
              {
                loader: 'css-loader',
                options: {
                  sourceMap: false,
                  importLoaders: 2
                }
              },
              {
                loader: 'postcss-loader',
                options: {
                  sourceMap: false
                }
              }
            ]
          }
        ]
      },
  1. Re run storybook and now <style module> will work just fine.

NOTE: as the goal is to replace the css rule the storybook uses, make sure if you extend the existing rules, you exclude the original css rule! The code @pocka shown here helped do the job:

const isCssRule = rule =>
  rule.test && rule.test instanceof RegExp && rule.test.test("./file.css");
Read more comments on GitHub >

github_iconTop Results From Across the Web

CSS Modules - Vue Loader
CSS Modules is a popular system for modularizing and composing CSS. vue-loader provides first-class integration with CSS Modules as an alternative for ...
Read more >
CSS Modules in Vue.js - Professional Web Development
In these types of frameworks, it has become popular to use CSS Modules. It is a way of automatically creating unique class names,...
Read more >
CSS Modules: How do they work in Vue? - Mattermost
A CSS Module is a file that, by default, scopes all of your class names and animations locally. It enables you to build...
Read more >
Vue.js - Scoped Styles vs CSS Modules - Netguru
CSS Modules gained their popularity due to the React community that quickly adopted this technology. Vue.js takes it to another level by ...
Read more >
Vue 3 provided an in-build CSS-module feature without ...
Using CSS modules in Vue. Step 1: Write a CSS inside the style of the component like below. Inside the style tag should...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found