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.

Possibility to pass options to css-loader and sass-loader when using (s)css modules

See original GitHub issue

Feature request

Is your feature request related to a problem? Please describe.

I’d like to pass further options (like includePaths or localIdentName) to sass-loader/css-loader when using the new built in css modules.

Additional context

Maybe hashing the css classnames via localIdentName: [hash:base64] in production mode could be a default.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:18
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

21reactions
joafeldmanncommented, Feb 19, 2020

I would go with A) since people might be used to it from the old next-plugins, e.g. https://github.com/zeit/next-plugins/blob/master/packages/next-sass/readme.md

Something like:

// next.config.js

const isProd = process.env.NODE_ENV === 'production'

module.exports = {

  sassLoaderOptions: { 
    includePaths: ['absolute/path/a', 'absolute/path/b'] 
  },

  cssLoaderOptions: {  
    modules: {
      // This could be a next.js default?
      localIdentName: isProd ? '[hash:base64]' : '[path][name]__[local]--[hash:base64:5]',
    },
  }

}
15reactions
OscarBarrettcommented, Mar 10, 2020

As a workaround for now, you can do the following to get your custom options in there. It’s not pretty but it works…

let rule, moduleRules, cssLoader, scssRules, sassLoader;
if (rule = config.module.rules.find(rule => Object.keys(rule).includes('oneOf'))) {
  // Locate css-loader config for css modules
  if (moduleRules = rule.oneOf.filter(r => ('test.module.scss'.match(r.test) || 'test.module.css'.match(r.test)) && Array.isArray(r.use))) {
    for (const moduleRule of moduleRules) {
      if (cssLoader = moduleRule.use.find(u => u.loader.match('css-loader'))) {
        cssLoader.options = {
          ...cssLoader.options,
          // Any custom css loader options here
          modules: {
            ...cssLoader.options.modules,
            // Your custom css-modules options below.
            getLocalIdent: () => false, // Fall back to default getLocalIdent function
            localIdentName: process.env.NODE_ENV === 'production' ? '[hash:base64:8]' : '[name]__[local]___[hash:base64:5]',
          }
        }
      }
    }
  }

  // Locate sass-loader config
  if (scssRules = rule.oneOf.filter(r => ('test.scss'.match(r.test) || 'test.module.scss'.match(r.test)) && Array.isArray(r.use))) {
    for (const scssRule of scssRules) {
      if (sassLoader = scssRule.use.find(u => u.loader.match('sass-loader'))) {
        sassLoader.options = {
          ...sassLoader.options,
          // Your custom sass-loader options below.
          prependData: '@import "~styles/variables.scss";',
        }
      }
    }
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

sass-loader - webpack - JS.ORG
To enable CSS source maps, you'll need to pass the sourceMap option to the sass-loader and the css-loader. webpack.config.js module.exports = { devtool:...
Read more >
sass-loader
Loads a SASS/SCSS file and compiles it to CSS. Use the css-loader or the raw-loader to turn it into a JS module and...
Read more >
How to configure SCSS modules for Webpack
First, SCSS is converted to CSS ( sass-loader ), then run through css-loader to process @import() , url() etc, then style-loader (to be...
Read more >
How do I pass options to SASS loader for style within .vue files?
You can pass it as includePaths which is a sass option like following: module.exports = { ... module: { loaders: [ { test:...
Read more >
Setting up CSS and Sass with Webpack!! - DEV Community ‍ ‍
scss, etc we need the help of loaders to compile it. Let see how to do it. Setting up CSS with webpack. At...
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