Skip to content

Commit e34f36f

Browse files
committed
Allow custom includePaths for sass
1 parent eca0ec0 commit e34f36f

File tree

3 files changed

+48
-15
lines changed

3 files changed

+48
-15
lines changed

packages/react-scripts/config/env.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ dotenvFiles.forEach(dotenvFile => {
4848
}
4949
});
5050

51+
const appDirectory = fs.realpathSync(process.cwd());
52+
5153
// We support resolving modules according to `NODE_PATH`.
5254
// This lets you use absolute paths in imports inside large monorepos:
5355
// https://github.com/facebook/create-react-app/issues/253.
@@ -57,13 +59,18 @@ dotenvFiles.forEach(dotenvFile => {
5759
// Otherwise, we risk importing Node.js core modules into an app instead of Webpack shims.
5860
// https://github.com/facebook/create-react-app/issues/1023#issuecomment-265344421
5961
// We also resolve them to make sure all tools using them work consistently.
60-
const appDirectory = fs.realpathSync(process.cwd());
6162
process.env.NODE_PATH = (process.env.NODE_PATH || '')
6263
.split(path.delimiter)
6364
.filter(folder => folder && !path.isAbsolute(folder))
6465
.map(folder => path.resolve(appDirectory, folder))
6566
.join(path.delimiter);
6667

68+
process.env.SASS_INCLUDEPATHS = (process.env.SASS_INCLUDEPATHS || '')
69+
.split(path.delimiter)
70+
.filter(folder => folder)
71+
.map(folder => path.resolve(appDirectory, folder))
72+
.join(path.delimiter);
73+
6774
// Grab NODE_ENV and REACT_APP_* environment variables and prepare them to be
6875
// injected into the application via DefinePlugin in Webpack configuration.
6976
const REACT_APP = /^REACT_APP_/i;

packages/react-scripts/config/webpack.config.dev.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ const getStyleLoaders = (cssOptions, preProcessor) => {
7878
},
7979
];
8080
if (preProcessor) {
81-
loaders.push(require.resolve(preProcessor));
81+
loaders.push(preProcessor);
8282
}
8383
return loaders;
8484
};
@@ -315,9 +315,7 @@ module.exports = {
315315
{
316316
test: cssRegex,
317317
exclude: cssModuleRegex,
318-
use: getStyleLoaders({
319-
importLoaders: 1,
320-
}),
318+
use: getStyleLoaders({ importLoaders: 1 }),
321319
},
322320
// Adds support for CSS Modules (https://github.com/css-modules/css-modules)
323321
// using the extension .module.css
@@ -337,7 +335,17 @@ module.exports = {
337335
{
338336
test: sassRegex,
339337
exclude: sassModuleRegex,
340-
use: getStyleLoaders({ importLoaders: 2 }, 'sass-loader'),
338+
use: getStyleLoaders(
339+
{ importLoaders: 2 },
340+
{
341+
loader: require.resolve('sass-loader'),
342+
options: {
343+
includePaths: process.env.SASS_INCLUDEPATHS.split(
344+
path.delimiter
345+
).filter(Boolean),
346+
},
347+
}
348+
),
341349
},
342350
// Adds support for CSS Modules, but using SASS
343351
// using the extension .module.scss or .module.sass
@@ -349,7 +357,14 @@ module.exports = {
349357
modules: true,
350358
getLocalIdent: getCSSModuleLocalIdent,
351359
},
352-
'sass-loader'
360+
{
361+
loader: require.resolve('sass-loader'),
362+
options: {
363+
includePaths: process.env.SASS_INCLUDEPATHS.split(
364+
path.delimiter
365+
).filter(Boolean),
366+
},
367+
}
353368
),
354369
},
355370
// "file" loader makes sure those assets get served by WebpackDevServer.

packages/react-scripts/config/webpack.config.prod.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,7 @@ const getStyleLoaders = (cssOptions, preProcessor) => {
103103
},
104104
];
105105
if (preProcessor) {
106-
loaders.push({
107-
loader: require.resolve(preProcessor),
108-
options: {
109-
sourceMap: shouldUseSourceMap,
110-
},
111-
});
106+
loaders.push(preProcessor);
112107
}
113108
return loaders;
114109
};
@@ -422,7 +417,15 @@ module.exports = {
422417
importLoaders: 2,
423418
sourceMap: shouldUseSourceMap,
424419
},
425-
'sass-loader'
420+
{
421+
loader: require.resolve('sass-loader'),
422+
options: {
423+
sourceMap: shouldUseSourceMap,
424+
includePaths: process.env.SASS_INCLUDEPATHS.split(
425+
path.delimiter
426+
).filter(Boolean),
427+
},
428+
}
426429
),
427430
// Don't consider CSS imports dead code even if the
428431
// containing package claims to have no side effects.
@@ -441,7 +444,15 @@ module.exports = {
441444
modules: true,
442445
getLocalIdent: getCSSModuleLocalIdent,
443446
},
444-
'sass-loader'
447+
{
448+
loader: require.resolve('sass-loader'),
449+
options: {
450+
sourceMap: shouldUseSourceMap,
451+
includePaths: process.env.SASS_INCLUDEPATHS.split(
452+
path.delimiter
453+
).filter(Boolean),
454+
},
455+
}
445456
),
446457
},
447458
// "file" loader makes sure assets end up in the `build` folder.

0 commit comments

Comments
 (0)