11/// <reference path="../npm/aspnet-prerendering/src/PrerenderingInterfaces.d.ts" />
22import * as url from 'url' ;
33import * as path from 'path' ;
4+ import * as fs from 'fs' ;
45declare var __non_webpack_require__ ;
56
67// Separate declaration and export just to add type checking on function signature
@@ -22,7 +23,8 @@ export const renderToString: RenderToStringFunc = renderToStringImpl;
2223// a certain flag is attached to the function instance.
2324function renderToStringImpl ( callback : RenderToStringCallback , applicationBasePath : string , bootModule : BootModuleInfo , absoluteRequestUrl : string , requestPathAndQuery : string , customDataParameter : any , overrideTimeoutMilliseconds : number ) {
2425 try {
25- let renderToStringFunc = findRenderToStringFunc ( applicationBasePath , bootModule ) ;
26+ const forceLegacy = isLegacyAspNetPrerendering ( ) ;
27+ const renderToStringFunc = ! forceLegacy && findRenderToStringFunc ( applicationBasePath , bootModule ) ;
2628 const isNotLegacyMode = renderToStringFunc && renderToStringFunc [ 'isServerRenderer' ] ;
2729
2830 if ( isNotLegacyMode ) {
@@ -32,9 +34,9 @@ function renderToStringImpl(callback: RenderToStringCallback, applicationBasePat
3234 renderToStringFunc . apply ( null , arguments ) ;
3335 } else {
3436 // Legacy mode - just hand off execution to 'aspnet-prerendering' v1.x, which must exist in node_modules at runtime
35- renderToStringFunc = require ( 'aspnet-prerendering' ) . renderToString ;
36- if ( renderToStringFunc ) {
37- renderToStringFunc ( callback , applicationBasePath , bootModule , absoluteRequestUrl , requestPathAndQuery , customDataParameter , overrideTimeoutMilliseconds ) ;
37+ const aspNetPrerenderingV1RenderToString = require ( 'aspnet-prerendering' ) . renderToString ;
38+ if ( aspNetPrerenderingV1RenderToString ) {
39+ aspNetPrerenderingV1RenderToString ( callback , applicationBasePath , bootModule , absoluteRequestUrl , requestPathAndQuery , customDataParameter , overrideTimeoutMilliseconds ) ;
3840 } else {
3941 callback ( 'If you use aspnet-prerendering >= 2.0.0, you must update your server-side boot module to call createServerRenderer. '
4042 + 'Either update your boot module code, or revert to aspnet-prerendering version 1.x' ) ;
@@ -92,3 +94,22 @@ function findRenderToStringFunc(applicationBasePath: string, bootModule: BootMod
9294
9395 return renderToStringFunc ;
9496}
97+
98+ function isLegacyAspNetPrerendering ( ) {
99+ const version = getAspNetPrerenderingPackageVersion ( ) ;
100+ return version && / ^ 1 \. / . test ( version ) ;
101+ }
102+
103+ function getAspNetPrerenderingPackageVersion ( ) {
104+ try {
105+ const packageEntryPoint = __non_webpack_require__ . resolve ( 'aspnet-prerendering' ) ;
106+ const packageDir = path . dirname ( packageEntryPoint ) ;
107+ const packageJsonPath = path . join ( packageDir , 'package.json' ) ;
108+ const packageJson = __non_webpack_require__ ( packageJsonPath ) ;
109+ return packageJson . version . toString ( ) ;
110+ } catch ( ex ) {
111+ // Implies aspnet-prerendering isn't in node_modules at all (or node_modules itself doesn't exist,
112+ // which will be the case in production based on latest templates).
113+ return null ;
114+ }
115+ }
0 commit comments