1 | const pkgRoot = require('./package-root');
|
2 |
|
3 | let rootCache;
|
4 |
|
5 | function isNamed(name) {
|
6 | return fn => name && fn && fn.constructor && fn.constructor.name && fn.constructor.name === name;
|
7 | }
|
8 |
|
9 | module.exports = {
|
10 | appRoot: function() {
|
11 | return rootCache ? rootCache : (rootCache = pkgRoot().path);
|
12 | },
|
13 | injectEntry: function(config, entry, opts = {}) {
|
14 | if (typeof config.entry === 'string') {
|
15 | config.entry = [config.entry];
|
16 | }
|
17 | if (Array.isArray(config.entry)) {
|
18 | if (opts.first) {
|
19 | config.entry.unshift(entry);
|
20 | } else if (opts.last || opts.main) {
|
21 | config.entry.push(entry);
|
22 | } else {
|
23 | config.entry.splice(-1, 0, entry);
|
24 | }
|
25 | } else if (typeof config.entry === 'object') {
|
26 | const o = {entry: config.entry[opts.chunk || 'main']};
|
27 | this.injectEntry(o, entry, opts);
|
28 | config.entry[opts.chunk || 'main'] = o.entry;
|
29 | }
|
30 | },
|
31 | mainEntry: function(config, opts = {}) {
|
32 | if (typeof config.entry === 'string') {
|
33 | return config.entry;
|
34 | } else if (Array.isArray(config.entry)) {
|
35 | return config.entry[config.entry.length - 1];
|
36 | } else if (typeof config.entry === 'object') {
|
37 | return this.mainEntry({entry: config.entry[opts.chunk || 'main']}, opts);
|
38 | }
|
39 | },
|
40 | replaceMain: function(config, replacement, opts = {}) {
|
41 | if (typeof config.entry === 'string') {
|
42 | config.entry = replacement;
|
43 | } else if (Array.isArray(config.entry)) {
|
44 | config.entry[config.entry.length - 1] = replacement;
|
45 | } else if (typeof config.entry === 'object') {
|
46 | this.replaceMain({entry: config.entry[opts.chunk || 'main']}, replacement, opts);
|
47 | }
|
48 | },
|
49 | findPlugin: function({plugins = []} = {}, name) {
|
50 | return plugins.findIndex(isNamed(name));
|
51 | },
|
52 | getPluginByName: function({plugins = []} = {}, name) {
|
53 | return plugins[this.findPlugin({plugins}, name)];
|
54 | },
|
55 | removePlugin: function({plugins = []} = {}, name) {
|
56 | const i = this.findPlugin({plugins}, name);
|
57 | if (i >= 0) plugins.splice(i, 1);
|
58 | },
|
59 | findMinimizer: function({optimization = []} = {}, name) {
|
60 | return (optimization.minimizer || []).findIndex(isNamed(name));
|
61 | },
|
62 | getMinimizerByName: function({optimization = []} = {}, name) {
|
63 | return (optimization.minimizer || [])[this.findMinimizer({optimization}, name)];
|
64 | },
|
65 | removeMinimizer: function({optimization = {}} = {}, name) {
|
66 | const i = this.findMinimizer({optimization}, name);
|
67 | if (i >= 0) optimization.minimizer.splice(i, 1);
|
68 | }
|
69 | };
|