UNPKG

1.94 kBJavaScriptView Raw
1// replace with the real thing when PR is merged
2// https://github.com/angular/universal/pull/464
3"use strict";
4class PrerenderWebpackPlugin {
5 constructor(options) {
6 this.options = options;
7 // maintain your platform instance
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 // we need to cache the template file to be able to re-serialize it
14 // even when it is not being emitted
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 // delete all app files from cache, but keep libs
35 // this is needed so that the config file can reimport up to date
36 // versions of the app files
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 // console.log('===', key);
42 delete require.cache[key];
43 });
44 }
45}
46exports.PrerenderWebpackPlugin = PrerenderWebpackPlugin;
47;
48//# sourceMappingURL=/Users/hansl/Sources/angular-cli/packages/@angular/cli/utilities/prerender-webpack-plugin.js.map
\No newline at end of file