-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a81cc2e
commit a984ccb
Showing
2 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Load zone.js for the server. | ||
import 'zone.js/dist/zone-node'; | ||
import 'reflect-metadata'; | ||
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs'; | ||
import { join } from 'path'; | ||
|
||
import { enableProdMode } from '@angular/core'; | ||
// 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'; | ||
|
||
// (global as any).WebSocket = require('ws'); | ||
// (global as any).XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest; | ||
|
||
// Faster server renders w/ Prod mode (dev mode never needed) | ||
enableProdMode(); | ||
|
||
// * NOTE :: leave this as require() since this file is built Dynamically from webpack | ||
const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require('./dist/server/main'); | ||
|
||
const BROWSER_FOLDER = join(process.cwd(), 'browser'); | ||
|
||
// Load the index.html file containing referances to your application bundle. | ||
const index = readFileSync(join(BROWSER_FOLDER, 'index.html'), 'utf8'); | ||
|
||
let previousRender = Promise.resolve(); | ||
|
||
// Iterate each route path | ||
ROUTES.forEach(route => { | ||
const fullPath = join(BROWSER_FOLDER, route); | ||
|
||
// 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)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const ROUTES = [ | ||
'/', | ||
]; |