-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
60 lines (50 loc) · 1.79 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
const url = require('url');
const qs = require('querystring');
class NovaConsumerPlugin {
constructor(opts = {}) {
this.opts = opts;
}
apply(compiler) {
if (compiler.options.mode === 'development') {
compiler.hooks.entryOption.tap('NovaConsumerPlugin', (context, entry) => {
if (!entry['nova-consumer']) {
entry['nova-consumer'] = []; // eslint-disable-line no-param-reassign
}
const { novas = [] } = this.opts;
novas.forEach((nova) => {
if (nova.entry) {
const { port } = new url.URL(nova.entry);
entry['nova-consumer'].push(`@ara/webpack-nova-consumer/client?port=${port}`);
}
});
});
} else {
compiler.hooks.entryOption.tap('NovaConsumerPlugin', (context, entry) => {
const { novas = [] } = this.opts;
const viewsMap = novas.reduce((acc, { views = [], entry: novaEntry }) => {
return views.reduce((acc2, view) => {
acc2[view] = novaEntry; // eslint-disable-line no-param-reassign
return acc2;
}, acc);
}, {});
entry['nova-lazy-load'] = `@ara/webpack-nova-consumer/lazy?${qs.encode(viewsMap)}`; // eslint-disable-line no-param-reassign
});
}
if (compiler.options.mode === 'development') {
compiler.hooks.compilation.tap('NovaConsumerPlugin', (context) => {
context.hooks.htmlWebpackPluginBeforeHtmlProcessing.tapAsync('NovaConsumerPlugin', (data, cb) => {
const { assets } = data;
const { novas = [] } = this.opts;
novas.forEach((nova) => {
const { entry } = nova;
if (entry) {
assets.js.push(entry);
}
});
cb();
});
});
}
}
}
module.exports = NovaConsumerPlugin;