Skip to content

Commit 4613977

Browse files
committed
Don't duplicate loaders if already present in webpack config
1 parent 4920235 commit 4613977

File tree

1 file changed

+40
-4
lines changed

1 file changed

+40
-4
lines changed

src/configure.js

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,42 @@ export default function configure(options) {
5353

5454
webpackConfig = webpackConfig || {};
5555

56+
let loaders = [].concat(delve(webpackConfig, 'module.loaders') || [], delve(webpackConfig, 'module.rules') || []);
57+
58+
function evaluateCondition(condition, filename, expected) {
59+
if (typeof condition==='function') {
60+
return condition(filename)==expected;
61+
}
62+
else if (condition instanceof RegExp) {
63+
return condition.test(filename)==expected;
64+
}
65+
if (Array.isArray(condition)) {
66+
for (let i=0; i<condition.length; i++) {
67+
if (evaluateCondition(condition[i], filename)) return expected;
68+
}
69+
}
70+
return !expected;
71+
}
72+
73+
function getLoader(predicate) {
74+
if (typeof predicate==='string') {
75+
let filename = predicate;
76+
predicate = loader => {
77+
let { test, include, exclude } = loader;
78+
if (exclude && evaluateCondition(exclude, filename, false)) return false;
79+
if (include && !evaluateCondition(include, filename, true)) return false;
80+
if (test && evaluateCondition(test, filename, true)) return true;
81+
return false;
82+
};
83+
}
84+
for (let i=0; i<loaders.length; i++) {
85+
if (predicate(loaders[i])) {
86+
return { index: i, loader: loaders[i] };
87+
}
88+
}
89+
return false;
90+
}
91+
5692
function webpackProp(name, value) {
5793
let configured = delve(webpackConfig, 'resolve.alias');
5894
if (Array.isArray(value)) {
@@ -111,10 +147,10 @@ export default function configure(options) {
111147
webpack: {
112148
devtool: 'cheap-module-eval-source-map',
113149
module: {
114-
loaders: [
115-
babelLoader(options),
116-
cssLoader(options)
117-
]
150+
loaders: loaders.concat(
151+
!getLoader( rule => `${rule.use},${rule.loader}`.match(/\bbabel-loader\b/) ) && babelLoader(options),
152+
!getLoader('foo.css') && cssLoader(options)
153+
).filter(Boolean)
118154
},
119155
resolve: webpackProp('resolve', {
120156
modules: webpackProp('resolve.modules', [

0 commit comments

Comments
 (0)