-
Notifications
You must be signed in to change notification settings - Fork 423
/
index.js
61 lines (50 loc) · 1.48 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const path = require('path');
const paths = require('./scripts/utils/paths');
const loaderNameMatches = function(rule, loader_name) {
return rule && rule.loader && typeof rule.loader === 'string' &&
(rule.loader.indexOf(`${path.sep}${loader_name}${path.sep}`) !== -1 ||
rule.loader.indexOf(`@${loader_name}${path.sep}`) !== -1);
}
const babelLoaderMatcher = function(rule) {
return loaderNameMatches(rule, 'babel-loader');
};
const getLoader = function(rules, matcher) {
var loader;
rules.some(rule => {
return (loader = matcher(rule)
? rule
: getLoader(rule.use || rule.oneOf || [], matcher));
});
return loader;
};
const getBabelLoader = function(rules) {
return getLoader(rules, babelLoaderMatcher);
};
const injectBabelPlugin = function(pluginName, config) {
const loader = getBabelLoader(config.module.rules);
if (!loader) {
console.log('babel-loader not found');
return config;
}
// Older versions of webpack have `plugins` on `loader.query` instead of `loader.options`.
const options = loader.options || loader.query;
options.plugins = [pluginName].concat(options.plugins || []);
return config;
};
const compose = function(...funcs) {
if (funcs.length === 0) {
return config => config;
}
if (funcs.length === 1) {
return funcs[0];
}
return funcs.reduce((a, b) => (config, env) => a(b(config, env), env));
};
module.exports = {
getLoader,
loaderNameMatches,
getBabelLoader,
injectBabelPlugin,
compose,
paths
};