forked from codediodeio/angular-firestarter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prerender.js
58 lines (46 loc) · 1.38 KB
/
prerender.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
require('zone.js/dist/zone-node');
require('reflect-metadata');
const { join } = require('path');
const { enableProdMode } = require('@angular/core');
// Import module map for lazy loading
const {
provideModuleMap
} = require('@nguniversal/module-map-ngfactory-loader');
const { renderModuleFactory } = require('@angular/platform-server');
const fs = require('fs-extra');
// Must manually define routes to prerender
const ROUTES = [
'/',
'/customers',
'/customers/78asJMXvM8q7f87cpVEF',
'/customers/Wu2BRnrAxnizSgGaJXhN',
'/customers/qe7EtWu4UWiWfZgtmP3C',
'/kanban',
'/login',
];
// leave this as require(), imported via webpack
const {
AppServerModuleNgFactory,
LAZY_MODULE_MAP
} = require(`./dist/server/main`);
// START prerender script
(async function() {
enableProdMode();
// Get the app index
const views = 'dist/browser';
const index = await fs.readFile(join(views, 'index.html'), 'utf8');
// Loop over each route
for (const route of ROUTES) {
const pageDir = join(views, route);
await fs.ensureDir(pageDir);
// Render with Universal
const html = await renderModuleFactory(AppServerModuleNgFactory, {
document: index,
url: route,
extraProviders: [provideModuleMap(LAZY_MODULE_MAP)]
});
await fs.writeFile(join(pageDir, 'index.html'), html);
}
process.exit();
console.log('prerendering complete');
})();