|
52 | 52 | /***/ function(module, exports, __webpack_require__) {
|
53 | 53 |
|
54 | 54 | "use strict";
|
55 |
| - // Pass through the invocation to the 'aspnet-prerendering' package, verifying that it can be loaded |
56 |
| - function renderToString(callback) { |
57 |
| - var aspNetPrerendering; |
| 55 | + var path = __webpack_require__(2); |
| 56 | + // Separate declaration and export just to add type checking on function signature |
| 57 | + exports.renderToString = renderToStringImpl; |
| 58 | + // This function is invoked by .NET code (via NodeServices). Its job is to hand off execution to the application's |
| 59 | + // prerendering boot function. It can operate in two modes: |
| 60 | + // [1] Legacy mode |
| 61 | + // This is for backward compatibility with projects created with templates older than the generator version 0.6.0. |
| 62 | + // In this mode, we don't really do anything here - we just load the 'aspnet-prerendering' NPM module (which must |
| 63 | + // exist in node_modules, and must be v1.x (not v2+)), and pass through all the parameters to it. Code in |
| 64 | + // 'aspnet-prerendering' v1.x will locate the boot function and invoke it. |
| 65 | + // The drawback to this mode is that, for it to work, you have to deploy node_modules to production. |
| 66 | + // [2] Current mode |
| 67 | + // This is for projects created with the Yeoman generator 0.6.0+ (or projects manually updated). In this mode, |
| 68 | + // we don't invoke 'require' at runtime at all. All our dependencies are bundled into the NuGet package, so you |
| 69 | + // don't have to deploy node_modules to production. |
| 70 | + // To determine whether we're in mode [1] or [2], the code locates your prerendering boot function, and checks whether |
| 71 | + // a certain flag is attached to the function instance. |
| 72 | + function renderToStringImpl(callback, applicationBasePath, bootModule, absoluteRequestUrl, requestPathAndQuery, customDataParameter, overrideTimeoutMilliseconds) { |
58 | 73 | try {
|
59 |
| - aspNetPrerendering = __webpack_require__(2); |
| 74 | + var renderToStringFunc = findRenderToStringFunc(applicationBasePath, bootModule); |
| 75 | + var isNotLegacyMode = renderToStringFunc && renderToStringFunc['isServerRenderer']; |
| 76 | + if (isNotLegacyMode) { |
| 77 | + // Current (non-legacy) mode - we invoke the exported function directly (instead of going through aspnet-prerendering) |
| 78 | + // It's type-safe to just apply the incoming args to this function, because we already type-checked that it's a RenderToStringFunc, |
| 79 | + // just like renderToStringImpl itself is. |
| 80 | + renderToStringFunc.apply(null, arguments); |
| 81 | + } |
| 82 | + else { |
| 83 | + // Legacy mode - just hand off execution to 'aspnet-prerendering' v1.x, which must exist in node_modules at runtime |
| 84 | + renderToStringFunc = __webpack_require__(3).renderToString; |
| 85 | + if (renderToStringFunc) { |
| 86 | + renderToStringFunc(callback, applicationBasePath, bootModule, absoluteRequestUrl, requestPathAndQuery, customDataParameter, overrideTimeoutMilliseconds); |
| 87 | + } |
| 88 | + else { |
| 89 | + callback('If you use aspnet-prerendering >= 2.0.0, you must update your server-side boot module to call createServerRenderer. ' |
| 90 | + + 'Either update your boot module code, or revert to aspnet-prerendering version 1.x'); |
| 91 | + } |
| 92 | + } |
60 | 93 | }
|
61 | 94 | catch (ex) {
|
62 |
| - // Developers sometimes have trouble with badly-configured Node installations, where it's unable |
63 |
| - // to find node_modules. Or they accidentally fail to deploy node_modules, or even to run 'npm install'. |
64 |
| - // Make sure such errors are reported back to the .NET part of the app. |
65 |
| - callback('Prerendering failed because of an error while loading \'aspnet-prerendering\'. Error was: ' |
| 95 | + // Make sure loading errors are reported back to the .NET part of the app |
| 96 | + callback('Prerendering failed because of error: ' |
66 | 97 | + ex.stack
|
67 | 98 | + '\nCurrent directory is: '
|
68 | 99 | + process.cwd());
|
69 |
| - return; |
70 | 100 | }
|
71 |
| - return aspNetPrerendering.renderToString.apply(this, arguments); |
72 | 101 | }
|
73 |
| - exports.renderToString = renderToString; |
| 102 | + ; |
| 103 | + function findBootModule(applicationBasePath, bootModule) { |
| 104 | + var bootModuleNameFullPath = path.resolve(applicationBasePath, bootModule.moduleName); |
| 105 | + if (bootModule.webpackConfig) { |
| 106 | + // If you're using asp-prerender-webpack-config, you're definitely in legacy mode |
| 107 | + return null; |
| 108 | + } |
| 109 | + else { |
| 110 | + return require(bootModuleNameFullPath); |
| 111 | + } |
| 112 | + } |
| 113 | + function findRenderToStringFunc(applicationBasePath, bootModule) { |
| 114 | + // First try to load the module |
| 115 | + var foundBootModule = findBootModule(applicationBasePath, bootModule); |
| 116 | + if (foundBootModule === null) { |
| 117 | + return null; // Must be legacy mode |
| 118 | + } |
| 119 | + // Now try to pick out the function they want us to invoke |
| 120 | + var renderToStringFunc; |
| 121 | + if (bootModule.exportName) { |
| 122 | + // Explicitly-named export |
| 123 | + renderToStringFunc = foundBootModule[bootModule.exportName]; |
| 124 | + } |
| 125 | + else if (typeof foundBootModule !== 'function') { |
| 126 | + // TypeScript-style default export |
| 127 | + renderToStringFunc = foundBootModule.default; |
| 128 | + } |
| 129 | + else { |
| 130 | + // Native default export |
| 131 | + renderToStringFunc = foundBootModule; |
| 132 | + } |
| 133 | + // Validate the result |
| 134 | + if (typeof renderToStringFunc !== 'function') { |
| 135 | + if (bootModule.exportName) { |
| 136 | + throw new Error("The module at " + bootModule.moduleName + " has no function export named " + bootModule.exportName + "."); |
| 137 | + } |
| 138 | + else { |
| 139 | + throw new Error("The module at " + bootModule.moduleName + " does not export a default function, and you have not specified which export to invoke."); |
| 140 | + } |
| 141 | + } |
| 142 | + return renderToStringFunc; |
| 143 | + } |
74 | 144 |
|
75 | 145 |
|
76 | 146 | /***/ },
|
77 | 147 | /* 2 */
|
| 148 | +/***/ function(module, exports) { |
| 149 | + |
| 150 | + module.exports = require("path"); |
| 151 | + |
| 152 | +/***/ }, |
| 153 | +/* 3 */ |
78 | 154 | /***/ function(module, exports) {
|
79 | 155 |
|
80 | 156 | module.exports = require("aspnet-prerendering");
|
|
0 commit comments