forked from aze3ma/react-app-rewire-scss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
45 lines (35 loc) · 1.27 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const path = require('path');
const { getLoader, loaderNameMatches } = require('react-app-rewired');
function createRewireSass(sassLoaderOptions = {}) {
return function(config, env) {
const sassExtension = /(\.scss|\.sass)$/;
const devMode = env !== 'production';
const fileLoader = getLoader(config.module.rules, rule => loaderNameMatches(rule, 'file-loader'));
if(!fileLoader.exclude) fileLoader.exclude = [];
fileLoader.exclude.push(sassExtension);
const cssRules = getLoader(config.module.rules, rule => String(rule.test) === String(/\.css$/));
var sassRules;
if (devMode) {
sassRules = {
test: sassExtension,
use: [...cssRules.use, { loader: 'sass-loader', options: sassLoaderOptions }],
};
} else {
sassRules = {
test: sassExtension,
use: [...cssRules.loader, { loader: 'sass-loader', options: sassLoaderOptions }],
};
}
const oneOfRule = config.module.rules.find(rule => rule.oneOf !== undefined);
if (oneOfRule) {
oneOfRule.oneOf.unshift(sassRules);
} else {
// Fallback to previous behaviour of adding to the end of the rules list.
config.module.rules.push(sassRules);
}
return config;
};
}
const rewireSass = createRewireSass();
rewireSass.withLoaderOptions = createRewireSass;
module.exports = rewireSass;