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.

React - Using SASS Modules with includePaths fail

See original GitHub issue

The merge request https://github.com/nrwl/nx/pull/2719 brought support for SASS Module.

I tested it with the latest version of Nx (9.2.4). Everything seems to work fine but the option stylePreprocessorOptions and the includePaths attribute do not seem to be taken into account with files of the type .module.scss.

When I remove the extension .module and use a basic Sass file, paths indicated in the includePaths property are taken into account and everything works fine.

To test, I just created a new React application and added a stylePreprocessorOptions option at the build level of the application.

"build": {
          "builder": "@nrwl/web:build",
          "options": {
            "outputPath": "dist/apps/test-app",
            "index": "apps/test-app/src/index.html",
            "main": "apps/test-app/src/main.tsx",
            "polyfills": "apps/test-app/src/polyfills.ts",
            "tsConfig": "apps/test-app/tsconfig.app.json",
            "assets": [
              "apps/test-app/src/favicon.ico",
              "apps/test-app/src/assets"
            ],
            "stylePreprocessorOptions": {
              "includePaths": ["apps/test-app/src"]
            },
            "styles": ["apps/test-app/src/styles.scss"],
            "scripts": [],
            "webpackConfig": "@nrwl/react/plugins/webpack"
          },

I tried to do a specific webpack configuration to work around the problem but it doesn’t work either.

const path = require('path');
const getConfig = require('@nrwl/react/plugins/webpack');
const cssModuleRegex = /\.module\.scss$/;

module.exports = (config) => {
  config = getConfig(config);

  config.module.rules.forEach((rule, idx) => {
    // Find rule tests for SCSS.
    // Then make sure it excludes .module.scss files.
    if (rule.test.test('foo.scss')) {
      rule.exclude = rule.exclude
        ? Array.isArray(rule.exclude)
          ? [...rule.exclude, cssModuleRegex]
          : [rule.exclude, cssModuleRegex]
        : cssModuleRegex
    }
  });

  // Add new rule to handle .module.scss files by using sass-loader
  // with modules on.
  config.module.rules.push({
    test: /\.module\.scss$/,
    use: [
      { loader: 'style-loader' },
      { loader: 'css-modules-typescript-loader' },
      {
        loader: 'css-loader',
        options: {
          modules: true,
          importLoaders: 1
        }
      },
      {
        loader: 'sass-loader',
        options: {
          sassOptions: {
            precision: 8,
            includePaths: [
              path.resolve(__dirname, 'apps/test-app/src'),
            ],
          },
        },
      },
    ]
  });

  return config;
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
mboettchercommented, Dec 21, 2020

@bekos I think it still doesn’t work for *.module.scss files. When I manually add the includePaths to the config of test: /\.module\.(scss|sass)$/, in web.config.js it works fine.

2reactions
Pekes317commented, Jun 8, 2020

Hi All, this seems to be similar issue I was having where the SCSS Modules weren’t compiling. After some investigation it looks like the Webpack config is missing the sass-loader from the rule for the test: /\.module\.(scss|sass)$/,

@nrwl/web - web.config.ts

 {
     test: /\.module\.(scss|sass)$/,
      use: [
           {
              loader: options.extractCss
                ? MiniCssExtractPlugin.loader
                : 'style-loader',
            },
            {
              loader: 'css-loader',
              options: {
                modules: true,
                importLoaders: 1,
              },
            },
          ],
        },

I was able to work around it by using my own WebPack config to Modify the Rule which seems work.

/* eslint-disable */
const reactConfig = require('@nrwl/react/plugins/webpack');
const mergeWebpack = require('webpack-merge');

module.exports = (config, _options) => {
  const rule = { ...config.module.rules[1] };
  const sassModule = rule.oneOf[1];
  const cssLoader = sassModule.use[1];
  const { options } = cssLoader;
  const sassLoader = {
    loader: 'sass-loader',
    options: {
      implementation: require('sass'),
    }
  }

  cssLoader.options = { ...options, importLoaders: 2 };
  sassModule.use.push(sassLoader);
  config.module.rules.splice(1, 1);

  return mergeWebpack(reactConfig(config), {
    module: {
      rules: [rule]
    }
  });
};

Also, I was having trouble using @use './some-file.scss' syntax which seems to be a newer add to SCSS which could be fixed by updating the sass deps which is currently 1.22.9 and the latest 1.26.8. Thank you and have a nice day.

P.S. I have a fork with suggested changes to resolve this issue here

Read more comments on GitHub >

github_iconTop Results From Across the Web

Multiple errors using sass-loader in webpack + react
Have you tried to add the path to the resources that are not found to sass-loader 's includePaths ? includePaths: [path.resolve(SRC_PATH, ...
Read more >
sass-loader - npm
Start using sass-loader in your project by running `npm i sass-loader`. ... Thus you can import your Sass modules from node_modules .
Read more >
Documentation | U.S. Web Design System (USWDS)
USWDS 3.0 load paths must include a path to the /packages directory in the USWDS package, typically by updating an IncludePaths setting to...
Read more >
Adding a Sass Stylesheet - Create React App
Generally, we recommend that you don't reuse the same CSS classes across different components. For example, instead of using a .
Read more >
css-loader | webpack - JS.ORG
Using both CSS Module functionality as well as SCSS variables directly in JavaScript. import svars from "variables.scss"; import styles from "Component.module.
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