1 |
|
2 |
|
3 | "use strict";
|
4 | class PrerenderWebpackPlugin {
|
5 | constructor(options) {
|
6 | this.options = options;
|
7 |
|
8 | this.bootloader = require(this.options.configPath).getBootloader();
|
9 | }
|
10 | apply(compiler) {
|
11 | compiler.plugin('emit', (compilation, callback) => {
|
12 | if (compilation.assets.hasOwnProperty(this.options.templatePath)) {
|
13 |
|
14 |
|
15 | this.cachedTemplate = compilation.assets[this.options.templatePath].source();
|
16 | }
|
17 | if (this.cachedTemplate) {
|
18 | this.decacheAppFiles();
|
19 | require(this.options.configPath).serialize(this.bootloader, this.cachedTemplate)
|
20 | .then((html) => {
|
21 | compilation.assets[this.options.templatePath] = {
|
22 | source: () => html,
|
23 | size: () => html.length
|
24 | };
|
25 | callback();
|
26 | });
|
27 | }
|
28 | else {
|
29 | callback();
|
30 | }
|
31 | });
|
32 | }
|
33 | decacheAppFiles() {
|
34 |
|
35 |
|
36 |
|
37 | delete require.cache[this.options.configPath];
|
38 | Object.keys(require.cache)
|
39 | .filter(key => key.startsWith(this.options.appPath))
|
40 | .forEach(function (key) {
|
41 |
|
42 | delete require.cache[key];
|
43 | });
|
44 | }
|
45 | }
|
46 | exports.PrerenderWebpackPlugin = PrerenderWebpackPlugin;
|
47 | ;
|
48 |
|
\ | No newline at end of file |