|
| 1 | +// Load zone.js for the server. |
| 2 | +import 'zone.js/dist/zone-node'; |
| 3 | +import 'reflect-metadata'; |
| 4 | +import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs'; |
| 5 | +import { join } from 'path'; |
| 6 | + |
| 7 | +import { enableProdMode } from '@angular/core'; |
| 8 | +// Import module map for lazy loading |
| 9 | +import { provideModuleMap } from '@nguniversal/module-map-ngfactory-loader'; |
| 10 | +import { renderModuleFactory } from '@angular/platform-server'; |
| 11 | +import { ROUTES } from './static.paths'; |
| 12 | + |
| 13 | +// (global as any).WebSocket = require('ws'); |
| 14 | +// (global as any).XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest; |
| 15 | + |
| 16 | +// Faster server renders w/ Prod mode (dev mode never needed) |
| 17 | +enableProdMode(); |
| 18 | + |
| 19 | +// * NOTE :: leave this as require() since this file is built Dynamically from webpack |
| 20 | +const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require('./dist/server/main'); |
| 21 | + |
| 22 | +const BROWSER_FOLDER = join(process.cwd(), 'browser'); |
| 23 | + |
| 24 | +// Load the index.html file containing referances to your application bundle. |
| 25 | +const index = readFileSync(join(BROWSER_FOLDER, 'index.html'), 'utf8'); |
| 26 | + |
| 27 | +let previousRender = Promise.resolve(); |
| 28 | + |
| 29 | +// Iterate each route path |
| 30 | +ROUTES.forEach(route => { |
| 31 | + const fullPath = join(BROWSER_FOLDER, route); |
| 32 | + |
| 33 | + // Make sure the directory structure is there |
| 34 | + if (!existsSync(fullPath)) { |
| 35 | + mkdirSync(fullPath); |
| 36 | + } |
| 37 | + |
| 38 | + // Writes rendered HTML to index.html, replacing the file if it already exists. |
| 39 | + previousRender = previousRender |
| 40 | + .then(_ => |
| 41 | + renderModuleFactory(AppServerModuleNgFactory, { |
| 42 | + document: index, |
| 43 | + url: route, |
| 44 | + extraProviders: [provideModuleMap(LAZY_MODULE_MAP)] |
| 45 | + }) |
| 46 | + ) |
| 47 | + .then(html => writeFileSync(join(fullPath, 'index.html'), html)); |
| 48 | +}); |
0 commit comments