-
Notifications
You must be signed in to change notification settings - Fork 0
/
prerender.ts
42 lines (33 loc) · 1.17 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
import 'reflect-metadata';
import 'zone.js/dist/zone-node';
import { enableProdMode } from '@angular/core';
import { renderModuleFactory } from '@angular/platform-server';
import { applyDomino } from '@nestjs/ng-universal';
import { provideModuleMap } from '@nguniversal/module-map-ngfactory-loader';
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
import { join } from 'path';
import { ROUTES } from './static.paths';
enableProdMode();
const BROWSER_FOLDER = join(process.cwd(), 'client');
const indexPath = join('client', 'index.html');
applyDomino(global, indexPath);
const {
AppServerModuleNgFactory,
LAZY_MODULE_MAP,
} = require('./dist/server/main');
let previousRender = Promise.resolve();
ROUTES.forEach(route => {
const fullPath = join(BROWSER_FOLDER, route);
if (!existsSync(fullPath)) {
mkdirSync(fullPath);
}
previousRender = previousRender
.then(_ =>
renderModuleFactory(AppServerModuleNgFactory, {
document: readFileSync(indexPath, 'utf8'),
url: route,
extraProviders: [provideModuleMap(LAZY_MODULE_MAP)],
}),
)
.then(html => writeFileSync(join(fullPath, 'index.html'), html));
});