-
Notifications
You must be signed in to change notification settings - Fork 9.8k
/
angular-rendering.js
53 lines (49 loc) · 2.33 KB
/
angular-rendering.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
var path = require('path');
var ngUniversal = require('angular2-universal-preview');
var ngUniversalRender = require('angular2-universal-preview/dist/server/src/render');
var ngCore = require('angular2/core');
var ngRouter = require('angular2/router');
function getExportOrThrow(moduleInstance, moduleFilename, exportName) {
if (!(exportName in moduleInstance)) {
throw new Error('The module "' + moduleFilename + '" has no export named "' + exportName + '"');
}
return moduleInstance[exportName];
}
function findAngularComponent(options) {
var resolvedPath = path.resolve(process.cwd(), options.moduleName);
var loadedModule = require(resolvedPath);
if (options.exportName) {
// If exportName is specified explicitly, use it
return getExportOrThrow(loadedModule, resolvedPath, options.exportName);
} else if (typeof loadedModule === 'function') {
// Otherwise, if the module itself is a function, assume that is the component
return loadedModule;
} else if (typeof loadedModule.default === 'function') {
// Otherwise, if the module has a default export which is a function, assume that is the component
return loadedModule.default;
} else {
// Otherwise, guess the export name by converting tag-name to PascalCase
var tagNameAsPossibleExport = options.tagName.replace(/(-|^)([a-z])/g, function (m1, m2, char) { return char.toUpperCase(); });
return getExportOrThrow(loadedModule, resolvedPath, tagNameAsPossibleExport);
}
}
module.exports = {
renderToString: function(callback, options) {
try {
var component = findAngularComponent(options);
var serverBindings = [
ngRouter.ROUTER_BINDINGS,
ngUniversal.HTTP_PROVIDERS,
ngCore.provide(ngUniversal.BASE_URL, { useValue: options.requestUrl }),
ngCore.provide(ngRouter.APP_BASE_HREF, { useValue: '/' }),
ngUniversal.SERVER_LOCATION_PROVIDERS
];
return ngUniversalRender.renderToString(component, serverBindings).then(
function(successValue) { callback(null, successValue); },
function(errorValue) { callback(errorValue); }
);
} catch (synchronousException) {
callback(synchronousException);
}
}
};