-
Notifications
You must be signed in to change notification settings - Fork 1
/
prerender.ts
49 lines (37 loc) · 1.32 KB
/
prerender.ts
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
import 'reflect-metadata';
import 'zone.js/dist/zone-node';
import { enableProdMode } from '@angular/core';
import { renderModuleFactory } from '@angular/platform-server';
import { provideModuleMap } from '@nguniversal/module-map-ngfactory-loader';
import * as fs from 'fs-extra';
import { join, resolve } from 'path';
import { ROUTES } from './static.paths';
// DOM libs required for Firebase
(global as any).WebSocket = require('ws');
(global as any).XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
const APP_NAME = 'jmw-site';
// leave this as require(), imported via webpack
const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require(`./dist/${APP_NAME}-server/main`);
enableProdMode();
async function prerender() {
// Get the app index
const browserBuild = `dist/${APP_NAME}`;
const index = await fs.readFile(join(browserBuild, 'index.html'), 'utf8');
// Loop over each route
for (const route of ROUTES) {
const pageDir = join(browserBuild, 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);
}
console.log('Prerender Completed.');
process.exit();
}
prerender();