This repository has been archived by the owner on Jan 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
67 lines (55 loc) · 2 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
61
62
63
64
65
66
67
const fs = require('fs');
const getDLLs = entrypoint => {
const hash = entrypoint.chunks
.reduce((acc, i) => acc.concat(i.getModules()), [])
.reduce((acc, i) => acc.concat([...i.dependencies]), [])
.map(i => i.module)
.filter(i => !!i && i.constructor.name === 'DelegatedModule')
.reduce((acc, i) => Object.assign(acc, { [i.hash]: i }), {});
return Object.values(hash)
.reduce((acc, i) => acc.concat(i.dependencies), [])
.filter(i => i.module)
.map(i => `dll/${i.module.request}.js`);
};
class GeneratePagePlugin {
constructor(config, options = {}) {
this.config = config;
this.options = options;
const engine = config.parser;
const template = fs.readFileSync(config.template, 'utf-8');
this.renderer = engine.compile(template);
}
apply(compiler) {
compiler.hooks.compilation.tap('GeneratePagePlugin', compilation => {
compilation.hooks.additionalChunkAssets.tap('GeneratePagePlugin', () => {
const { entrypoints } = compilation;
Array.from(entrypoints).forEach(([, value]) => {
const dlls = getDLLs(value);
const options = Object.entries(this.options).reduce(
(acc, [k, v]) => Object.assign(acc, {
[k]: typeof v === 'function' ? v(value.name) : v,
}),
{}
);
const filename =
typeof this.config.filename === 'function'
? this.config.filename(value.name)
: this.config.filename || value.name;
const data = Object.assign({}, value, {
options: Object.assign({}, options, value.options ),
compilation,
files: value.getFiles(),
dlls,
});
const html = this.renderer(data);
// eslint-disable-next-line no-param-reassign
compilation.assets[`${filename}.html`] = {
source: () => html,
size: () => html.toString().length,
};
});
});
});
}
}
module.exports = GeneratePagePlugin;