2018-01-23 11:07:39 +01:00
|
|
|
// Load zone.js for the server.
|
|
|
|
import 'zone.js/dist/zone-node';
|
|
|
|
import 'reflect-metadata';
|
|
|
|
import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'fs';
|
|
|
|
import { join } from 'path';
|
|
|
|
|
|
|
|
import { enableProdMode } from '@angular/core';
|
|
|
|
// Faster server renders w/ Prod mode (dev mode never needed)
|
|
|
|
enableProdMode();
|
|
|
|
|
|
|
|
// Express Engine
|
|
|
|
import { ngExpressEngine } from '@nguniversal/express-engine';
|
|
|
|
// Import module map for lazy loading
|
|
|
|
import { provideModuleMap } from '@nguniversal/module-map-ngfactory-loader';
|
|
|
|
import { renderModuleFactory } from '@angular/platform-server';
|
|
|
|
import { ROUTES } from './static.paths';
|
|
|
|
|
|
|
|
// * NOTE :: leave this as require() since this file is built Dynamically from webpack
|
2019-06-03 15:21:49 +02:00
|
|
|
const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require('./dist/server/main');
|
2018-01-23 11:07:39 +01:00
|
|
|
|
|
|
|
const BROWSER_FOLDER = join(process.cwd(), 'browser');
|
|
|
|
|
|
|
|
// Load the index.html file containing referances to your application bundle.
|
|
|
|
const index = readFileSync(join('browser', 'index.html'), 'utf8');
|
|
|
|
|
|
|
|
let previousRender = Promise.resolve();
|
|
|
|
|
|
|
|
// Iterate each route path
|
|
|
|
ROUTES.forEach(route => {
|
2019-06-03 15:21:49 +02:00
|
|
|
const fullPath = join(BROWSER_FOLDER, route);
|
2018-01-23 11:07:39 +01:00
|
|
|
|
|
|
|
// Make sure the directory structure is there
|
|
|
|
if(!existsSync(fullPath)){
|
|
|
|
mkdirSync(fullPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Writes rendered HTML to index.html, replacing the file if it already exists.
|
|
|
|
previousRender = previousRender.then(_ => renderModuleFactory(AppServerModuleNgFactory, {
|
|
|
|
document: index,
|
|
|
|
url: route,
|
|
|
|
extraProviders: [
|
|
|
|
provideModuleMap(LAZY_MODULE_MAP)
|
|
|
|
]
|
|
|
|
})).then(html => writeFileSync(join(fullPath, 'index.html'), html));
|
|
|
|
});
|