Project

General

Profile

1
// Load zone.js for the server.
2
import 'zone.js/dist/zone-node';
3
import 'reflect-metadata';
4
import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'fs';
5
import { join } from 'path';
6

    
7
import { enableProdMode } from '@angular/core';
8
// Faster server renders w/ Prod mode (dev mode never needed)
9
enableProdMode();
10

    
11
// Express Engine
12
import { ngExpressEngine } from '@nguniversal/express-engine';
13
// Import module map for lazy loading
14
import { provideModuleMap } from '@nguniversal/module-map-ngfactory-loader';
15
import { renderModuleFactory } from '@angular/platform-server';
16
import { ROUTES } from './static.paths';
17

    
18
// * NOTE :: leave this as require() since this file is built Dynamically from webpack
19
const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require('./dist/server/main');
20

    
21
const BROWSER_FOLDER = join(process.cwd(), 'browser');
22

    
23
// Load the index.html file containing referances to your application bundle.
24
const index = readFileSync(join('browser', 'index.html'), 'utf8');
25

    
26
let previousRender = Promise.resolve();
27

    
28
// Iterate each route path
29
ROUTES.forEach(route => {
30
  const fullPath = join(BROWSER_FOLDER, route);
31

    
32
  // Make sure the directory structure is there
33
  if(!existsSync(fullPath)){
34
    mkdirSync(fullPath);
35
  }
36

    
37
  // Writes rendered HTML to index.html, replacing the file if it already exists.
38
  previousRender = previousRender.then(_ => renderModuleFactory(AppServerModuleNgFactory, {
39
    document: index,
40
    url: route,
41
    extraProviders: [
42
      provideModuleMap(LAZY_MODULE_MAP)
43
    ]
44
  })).then(html => writeFileSync(join(fullPath, 'index.html'), html));
45
});
(4-4/8)