-
Notifications
You must be signed in to change notification settings - Fork 5
/
config-helper.js
88 lines (84 loc) · 2.85 KB
/
config-helper.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const pkgRoot = require('./package-root');
let rootCache;
function isNamed(name) {
return fn => name && fn && fn.constructor && fn.constructor.name && fn.constructor.name === name;
}
function isPolyfill(file) {
return /[\\/]polyfills\.(js|jsx|mjs|ts|tsx)$/.test(file);
}
module.exports = {
appRoot: function () {
return rootCache ? rootCache : (rootCache = pkgRoot().path);
},
injectEntry: function (config, entry, opts = {}) {
if (typeof config.entry === 'string') {
config.entry = [config.entry];
}
if (Array.isArray(config.entry)) {
if (opts.first) {
config.entry.unshift(entry);
} else if (opts.last || opts.main) {
config.entry.push(entry);
} else {
config.entry.splice(-1, 0, entry);
}
} else if (typeof config.entry === 'object') {
const o = {entry: config.entry[opts.chunk || 'main']};
this.injectEntry(o, entry, opts);
config.entry[opts.chunk || 'main'] = o.entry;
}
},
mainEntry: function (config, opts = {}) {
if (typeof config.entry === 'string') {
return config.entry;
} else if (Array.isArray(config.entry)) {
return config.entry[config.entry.length - 1];
} else if (typeof config.entry === 'object') {
return this.mainEntry({entry: config.entry[opts.chunk || 'main']}, opts);
}
},
replaceMain: function (config, replacement, opts = {}) {
if (typeof config.entry === 'string') {
config.entry = replacement;
} else if (Array.isArray(config.entry)) {
config.entry[config.entry.length - 1] = replacement;
} else if (typeof config.entry === 'object') {
this.replaceMain({entry: config.entry[opts.chunk || 'main']}, replacement, opts);
}
},
polyfillFile: function ({entry} = {}) {
if (typeof entry === 'string') {
return isPolyfill(entry) && entry;
} else if (Array.isArray(entry)) {
return entry.find(isPolyfill);
} else if (typeof entry === 'object') {
return Object.keys(entry).reduce(
(result, n) =>
result ||
(typeof entry[n] === 'string' && isPolyfill(entry[n]) && entry[n]) ||
(Array.isArray(entry[n]) && entry[n].find(isPolyfill)),
undefined
);
}
},
findPlugin: function ({plugins = []} = {}, name) {
return plugins.findIndex(isNamed(name));
},
getPluginByName: function ({plugins = []} = {}, name) {
return plugins[this.findPlugin({plugins}, name)];
},
removePlugin: function ({plugins = []} = {}, name) {
const i = this.findPlugin({plugins}, name);
if (i >= 0) plugins.splice(i, 1);
},
findMinimizer: function ({optimization = []} = {}, name) {
return (optimization.minimizer || []).findIndex(isNamed(name));
},
getMinimizerByName: function ({optimization = []} = {}, name) {
return (optimization.minimizer || [])[this.findMinimizer({optimization}, name)];
},
removeMinimizer: function ({optimization = {}} = {}, name) {
const i = this.findMinimizer({optimization}, name);
if (i >= 0) optimization.minimizer.splice(i, 1);
}
};