0

I have a project on Vue.js and I plan to use CSS modules for my local styles (for sass and scss), but I keep on getting a 'Failed to compile error' on the CSS Loader validation part.

I already tried a lot of solutions on Github by configuring my Webpack in a lot of ways, but I keep on getting the same error.

Here's my webpack.config.js file under module.rules:

    {
        test: /\.scss$/,
        use: [
          'vue-style-loader',
          {
            loader: "css-loader",
            options: {
              modules: {
                localIdentName: 'MyApp__[local]--[hash:base64:5]',
              },
            }
          },
          'sass-loader'
        ],
      },
      {
        test: /\.sass$/,
        use: [
          'vue-style-loader',
          {
            loader: "css-loader",
            options: {
              modules: {
                localIdentName: 'MyApp__[local]--[hash:base64:5]',
              }
            }
          },
          'sass-loader?indentedSyntax'
        ],
      },

And this is how I use CSS modules on a child component in Vue:

<style lang="scss" module>
.subtitle{
    font-size: 1rem !important;
}
.box{
    padding: 20% 0 !important;
    box-shadow: 0 2px 2px 0 rgba(0,0,0,0.14), 0 3px 1px -2px rgba(0,0,0,0.12), 0 1px 5px 0 rgba(0,0,0,0.20);
}
</style>

Here's the complete error that I'm getting:

ERROR in ./node_modules/css-loader/dist/cjs.js?{"localIdentName":"[hash:base64]_0","importLoaders":true,"modules":true}!./node_modules/vue-loader/lib/style-compiler?{"vue":true,"id":"data-v-68c39e04","scoped":false,"hasInlineConfig":false}!./node_modules/sass-loader/lib/loader.js!./node_modules/vue-loader/lib/selector.js?type=styles&index=0!
Module build failed: ValidationError: Invalid options object. CSS Loader has been initialised using an options object that does not match the API schema.
 - options has an unknown property 'localIdentName'. These properties are valid:
   object { url?, import?, modules?, sourceMap?, importLoaders?, localsConvention?, onlyLocals? }
    at validate

I hope I could resolve this error.

2 Answers 2

1

Unfortunately, getLocalIdent option from css-loader doesn’t work with CSS processed by vue-loader.

As the author of Vue.js, Evan You, said:

"This is because vue-loader loaders' query have to be stringified, so the function option is dropped in the process"

You can read more about a workaround that may work for you here: https://medium.com/@my_own_grave/reducing-css-generated-by-vue-loader-by-using-classnames-shorten-trick-aa1d25d77473?

(!) However, I've just found a better workaround and successfully implemented it on our project! If you use Webpack 4, you can easily pass localIdentName inside modules as an object like this:

use: [
          'vue-style-loader',
          {
            loader: 'css-loader',
            options: {
              modules: { localIdentName: '[name]_[local]_[hash:base64:5]' }
            }
          },
          'sass-loader'
        ]
Sign up to request clarification or add additional context in comments.

Comments

0

Accoridng to documentation, it should be:

  options: {
    modules: true,
    localIdentName: 'MyApp__[local]--[hash:base64:5]'
  }

1 Comment

I already tried that. But I'm still getting the same error. :(

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.