UNPKG

2.75 kBJavaScriptView Raw
1const pkgRoot = require('./package-root');
2
3let rootCache;
4
5module.exports = {
6 appRoot: function() {
7 return rootCache ? rootCache : (rootCache = pkgRoot().path);
8 },
9 injectEntry: function(config, entry, opts = {}) {
10 if (typeof config.entry === 'string') {
11 config.entry = [config.entry];
12 }
13 if (Array.isArray(config.entry)) {
14 if (opts.first) {
15 config.entry.unshift(entry);
16 } else if (opts.last || opts.main) {
17 config.entry.push(entry);
18 } else {
19 config.entry.splice(-1, 0, entry);
20 }
21 } else if (typeof config.entry === 'object') {
22 const o = {entry: config.entry[opts.chunk || 'main']};
23 this.injectEntry(o, entry, opts);
24 config.entry[opts.chunk || 'main'] = o.entry;
25 }
26 },
27 mainEntry: function(config, opts = {}) {
28 if (typeof config.entry === 'string') {
29 return config.entry;
30 } else if (Array.isArray(config.entry)) {
31 return config.entry[config.entry.length - 1];
32 } else if (typeof config.entry === 'object') {
33 return this.mainEntry({entry: config.entry[opts.chunk || 'main']}, opts);
34 }
35 },
36 replaceMain: function(config, replacement, opts = {}) {
37 if (typeof config.entry === 'string') {
38 config.entry = replacement;
39 } else if (Array.isArray(config.entry)) {
40 config.entry[config.entry.length - 1] = replacement;
41 } else if (typeof config.entry === 'object') {
42 this.replaceMain({entry: config.entry[opts.chunk || 'main']}, replacement, opts);
43 }
44 },
45 findLoader: function(config, name) {
46 let index = -1;
47 if (config && config.module && config.module.rules && name) {
48 for (let i = 0; i < config.module.rules.length; i++) {
49 if (config.module.rules[i].loader) {
50 if (
51 config.module.rules[i].loader === name + '-loader' ||
52 new RegExp('node_modules[\\\\/]' + name + '-loader').test(config.module.rules[i].loader)
53 ) {
54 index = i;
55 break;
56 }
57 }
58 }
59 }
60 return index;
61 },
62 getLoaderByName: function(config, name) {
63 if (config && config.module && config.module.rules && name) {
64 return config.module.rules[this.findLoader(config, name)];
65 }
66 },
67 findPlugin: function(config, name) {
68 let index = -1;
69 if (config && config.plugins && name) {
70 for (let i = 0; i < config.plugins.length; i++) {
71 if (
72 config.plugins[i] &&
73 config.plugins[i].constructor &&
74 config.plugins[i].constructor.name &&
75 config.plugins[i].constructor.name === name
76 ) {
77 index = i;
78 break;
79 }
80 }
81 }
82 return index;
83 },
84 getPluginByName: function(config, name) {
85 if (config && config.plugins && name) {
86 return config.plugins[this.findPlugin(config, name)];
87 }
88 },
89 removePlugin: function(config, name) {
90 const i = this.findPlugin(config, name);
91 if (i >= 0) {
92 config.plugins.splice(i, 1);
93 }
94 }
95};