|
| 1 | +import 'angular2-universal-preview/dist/server/universal-polyfill.js'; |
| 2 | +import * as ngCore from 'angular2/core'; |
| 3 | +import * as ngRouter from 'angular2/router'; |
| 4 | +import * as ngUniversal from 'angular2-universal-preview'; |
| 5 | +import { BASE_URL } from 'angular2-universal-preview/dist/server/src/http/node_http'; |
| 6 | +import * as ngUniversalRender from 'angular2-universal-preview/dist/server/src/render'; |
| 7 | + |
| 8 | +// TODO: Make this ugly code go away, e.g., by somehow loading via Webpack |
| 9 | +function loadAsString(module, filename) { |
| 10 | + module.exports = require('fs').readFileSync(filename, 'utf8'); |
| 11 | +} |
| 12 | +(require as any).extensions['.html'] = loadAsString; |
| 13 | +(require as any).extensions['.css'] = loadAsString; |
| 14 | +let App: any = require('./components/app/app').App; |
| 15 | + |
| 16 | +export default function (params: any): Promise<{ html: string }> { |
| 17 | + return new Promise<{ html: string, globals: { [key: string]: any } }>((resolve, reject) => { |
| 18 | + const serverBindings = [ |
| 19 | + ngRouter.ROUTER_BINDINGS, |
| 20 | + ngUniversal.HTTP_PROVIDERS, |
| 21 | + ngCore.provide(BASE_URL, { useValue: params.absoluteUrl }), |
| 22 | + ngCore.provide(ngUniversal.REQUEST_URL, { useValue: params.url }), |
| 23 | + ngCore.provide(ngRouter.APP_BASE_HREF, { useValue: '/' }), |
| 24 | + ngUniversal.SERVER_LOCATION_PROVIDERS |
| 25 | + ]; |
| 26 | + |
| 27 | + ngUniversalRender.renderToString(App, serverBindings).then( |
| 28 | + html => resolve({ html, globals: {} }), |
| 29 | + reject // Also propagate any errors back into the host application |
| 30 | + ); |
| 31 | + }); |
| 32 | +} |
0 commit comments