React - Using SASS Modules with includePaths fail
See original GitHub issueThe 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:
- Created 3 years ago
- Comments:10 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@bekos I think it still doesn’t work for
*.module.scss
files. When I manually add theincludePaths
to the config oftest: /\.module\.(scss|sass)$/,
inweb.config.js
it works fine.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 thetest: /\.module\.(scss|sass)$/,
@nrwl/web - web.config.ts
I was able to work around it by using my own WebPack config to Modify the Rule which seems work.
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 thesass
deps which is currently1.22.9
and the latest1.26.8
. Thank you and have a nice day.P.S. I have a fork with suggested changes to resolve this issue here