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