-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
59 lines (47 loc) · 1.42 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
const path = require('path');
/**
* @param {Object} rule
* @return {Array}
*/
const ruleChildren = rule =>
rule.use || rule.oneOf || (Array.isArray(rule.loader) && rule.loader) || [];
const findIndexAndRules = (rulesSource, ruleMatcher) => {
let result;
const rules = Array.isArray(rulesSource) ?
rulesSource :
ruleChildren(rulesSource);
/* eslint no-return-assign: "off" */
rules.some((rule, index) =>
(result = ruleMatcher(rule) ?
{index, rules} :
findIndexAndRules(ruleChildren(rule), ruleMatcher)));
return result;
};
/**
* Given a rule, return if it uses a specific loader.
*/
const createLoaderMatcher = loader => rule =>
rule.loader && rule.loader.indexOf(`${path.sep}${loader}${path.sep}`) !== -1;
/**
* Get the existing file-loader config.
*/
const fileLoaderMatcher = createLoaderMatcher('file-loader');
/**
* Add one rule before another in the list of rules.
*/
const addBeforeRule = (rulesSource, ruleMatcher, value) => {
const {index, rules} = findIndexAndRules(rulesSource, ruleMatcher);
rules.splice(index, 0, value);
};
function rewireBemI18N(config, env, i18nLoaderOptions = {}) {
const i18nExtension = /\.i18n\//;
const i18nRules = {
test: i18nExtension,
loader: 'webpack-bem-i18n-loader',
options: i18nLoaderOptions
};
// Add the BEM I18N rule before the file-loader rule.
addBeforeRule(config.module.rules, fileLoaderMatcher, i18nRules);
return config;
}
module.exports = rewireBemI18N;