@@ -3,20 +3,39 @@ var ngUniversal = require('angular2-universal-patched');
33var ng = require ( 'angular2/angular2' ) ;
44var ngRouter = require ( 'angular2/router' ) ;
55
6- module . exports = {
7- renderComponent : function ( callback , options ) {
8- // Find the component class. Use options.componentExport if specified, otherwise convert tag-name to PascalCase.
9- var loadedModule = require ( path . resolve ( process . cwd ( ) , options . componentModule ) ) ;
10- var componentExport = options . componentExport || options . tagName . replace ( / ( - | ^ ) ( [ a - z ] ) / g, function ( m1 , m2 , char ) { return char . toUpperCase ( ) ; } ) ;
11- var component = loadedModule [ componentExport ] ;
12- if ( ! component ) {
13- throw new Error ( 'The module "' + options . componentModule + '" has no export named "' + componentExport + '"' ) ;
14- }
6+ function getExportOrThrow ( moduleInstance , moduleFilename , exportName ) {
7+ if ( ! ( exportName in moduleInstance ) ) {
8+ throw new Error ( 'The module "' + moduleFilename + '" has no export named "' + exportName + '"' ) ;
9+ }
10+ return moduleInstance [ exportName ] ;
11+ }
1512
13+ function findAngularComponent ( options ) {
14+ var resolvedPath = path . resolve ( process . cwd ( ) , options . moduleName ) ;
15+ var loadedModule = require ( resolvedPath ) ;
16+ if ( options . exportName ) {
17+ // If exportName is specified explicitly, use it
18+ return getExportOrThrow ( loadedModule , resolvedPath , options . exportName ) ;
19+ } else if ( typeof loadedModule === 'function' ) {
20+ // Otherwise, if the module itself is a function, assume that is the component
21+ return loadedModule ;
22+ } else if ( typeof loadedModule . default === 'function' ) {
23+ // Otherwise, if the module has a default export which is a function, assume that is the component
24+ return loadedModule . default ;
25+ } else {
26+ // Otherwise, guess the export name by converting tag-name to PascalCase
27+ var tagNameAsPossibleExport = options . tagName . replace ( / ( - | ^ ) ( [ a - z ] ) / g, function ( m1 , m2 , char ) { return char . toUpperCase ( ) ; } ) ;
28+ return getExportOrThrow ( loadedModule , resolvedPath , tagNameAsPossibleExport ) ;
29+ }
30+ }
31+
32+ module . exports = {
33+ renderToString : function ( callback , options ) {
34+ var component = findAngularComponent ( options ) ;
1635 var serverBindings = [
1736 ngRouter . ROUTER_BINDINGS ,
1837 ngUniversal . HTTP_PROVIDERS ,
19- ng . provide ( ngUniversal . BASE_URL , { useValue : options . baseUrl } ) ,
38+ ng . provide ( ngUniversal . BASE_URL , { useValue : options . requestUrl } ) ,
2039 ngUniversal . SERVER_LOCATION_PROVIDERS
2140 ] ;
2241
0 commit comments