|
1 | | -'use strict' |
2 | | - |
3 | | -const path = require('path') |
4 | | -const invariant = require('invariant') |
5 | | -const EventStream = require('event-stream') |
6 | | -const JSONStream = require('JSONStream') |
7 | | -const ReactDOMServer = require('react-dom/server') |
8 | | -const React = require('react') |
9 | | - |
10 | | -function getDefaultExports(moduleID) { |
| 1 | +const path = require("path"); |
| 2 | +const invariant = require("invariant"); |
| 3 | +const EventStream = require("event-stream"); |
| 4 | +const JSONStream = require("JSONStream"); |
| 5 | +const ReactDOMServer = require("react-dom/server"); |
| 6 | +const React = require("react"); |
| 7 | + |
| 8 | +function loadModule(moduleId) { |
11 | 9 | // Clear the require cache, in case the file was |
12 | 10 | // changed since the server was started. |
13 | | - const cacheKey = require.resolve(moduleID) |
14 | | - delete require.cache[cacheKey] |
| 11 | + const cacheKey = require.resolve(moduleId); |
| 12 | + delete require.cache[cacheKey]; |
15 | 13 |
|
16 | | - const moduleExports = require(moduleID) |
| 14 | + const moduleExports = require(moduleId); |
17 | 15 |
|
18 | | - // Return exports.default if using ES2015 modules. |
19 | | - if (moduleExports && moduleExports.default) |
20 | | - return moduleExports.default |
| 16 | + // Return exports.default if using ES modules. |
| 17 | + if (moduleExports && moduleExports.default) { |
| 18 | + return moduleExports.default; |
| 19 | + } |
21 | 20 |
|
22 | | - return moduleExports |
| 21 | + return moduleExports; |
23 | 22 | } |
24 | 23 |
|
25 | 24 | function renderToStaticMarkup(element, callback) { |
26 | | - callback(null, ReactDOMServer.renderToStaticMarkup(element)) |
| 25 | + callback(null, ReactDOMServer.renderToStaticMarkup(element)); |
27 | 26 | } |
28 | 27 |
|
29 | 28 | function renderToString(element, callback) { |
30 | | - callback(null, ReactDOMServer.renderToString(element)) |
| 29 | + callback(null, ReactDOMServer.renderToString(element)); |
31 | 30 | } |
32 | 31 |
|
33 | 32 | function handleRequest(workingDir, request, callback) { |
34 | | - const componentPath = request.component |
35 | | - const renderMethod = request.render |
36 | | - const props = request.props |
| 33 | + const componentPath = request.component; |
| 34 | + const renderMethod = request.render; |
| 35 | + const props = request.props; |
37 | 36 |
|
38 | | - invariant( |
39 | | - componentPath != null, |
40 | | - 'Missing { component } in request' |
41 | | - ) |
42 | | - |
43 | | - let render |
44 | | - if (renderMethod == null || renderMethod === 'renderToString') { |
45 | | - render = renderToString |
46 | | - } else if (renderMethod === 'renderToStaticMarkup') { |
47 | | - render = renderToStaticMarkup |
| 37 | + invariant(componentPath != null, "Missing { component } in request"); |
| 38 | + |
| 39 | + let render; |
| 40 | + if (renderMethod == null || renderMethod === "renderToString") { |
| 41 | + render = renderToString; |
| 42 | + } else if (renderMethod === "renderToStaticMarkup") { |
| 43 | + render = renderToStaticMarkup; |
48 | 44 | } else { |
49 | | - const methodFile = path.resolve(workingDir, renderMethod) |
| 45 | + const methodFile = path.resolve(workingDir, renderMethod); |
50 | 46 |
|
51 | 47 | try { |
52 | | - render = getDefaultExports(methodFile) |
| 48 | + render = loadModule(methodFile); |
53 | 49 | } catch (error) { |
54 | | - if (error.code !== 'MODULE_NOT_FOUND') |
55 | | - process.stderr.write(error.stack + '\n') |
| 50 | + if (error.code !== "MODULE_NOT_FOUND") { |
| 51 | + process.stderr.write(error.stack + "\n"); |
| 52 | + } |
56 | 53 | } |
57 | 54 | } |
58 | 55 |
|
59 | 56 | invariant( |
60 | | - typeof render === 'function', |
61 | | - 'Cannot load render method: %s', |
| 57 | + typeof render === "function", |
| 58 | + "Cannot load render method: %s", |
62 | 59 | renderMethod |
63 | | - ) |
| 60 | + ); |
64 | 61 |
|
65 | | - const componentFile = path.resolve(workingDir, componentPath) |
| 62 | + const componentFile = path.resolve(workingDir, componentPath); |
66 | 63 |
|
67 | | - let component |
| 64 | + let component; |
68 | 65 | try { |
69 | | - component = getDefaultExports(componentFile) |
| 66 | + component = loadModule(componentFile); |
70 | 67 | } catch (error) { |
71 | | - if (error.code !== 'MODULE_NOT_FOUND') |
72 | | - process.stderr.write(error.stack + '\n') |
| 68 | + if (error.code !== "MODULE_NOT_FOUND") { |
| 69 | + process.stderr.write(error.stack + "\n"); |
| 70 | + } |
73 | 71 | } |
74 | 72 |
|
75 | | - invariant( |
76 | | - component != null, |
77 | | - 'Cannot load component: %s', |
78 | | - componentPath |
79 | | - ) |
80 | | - |
81 | | - render( |
82 | | - React.createElement(component, props), |
83 | | - callback |
84 | | - ) |
| 73 | + invariant(component != null, "Cannot load component: %s", componentPath); |
| 74 | + |
| 75 | + render(React.createElement(component, props), callback); |
85 | 76 | } |
86 | 77 |
|
87 | 78 | function createRequestHandler(workingDir) { |
88 | | - return function (request, callback) { |
| 79 | + return function(request, callback) { |
89 | 80 | try { |
90 | | - handleRequest(workingDir, request, function (error, html) { |
| 81 | + handleRequest(workingDir, request, function(error, html) { |
91 | 82 | if (error) { |
92 | | - callback(error) |
93 | | - } else if (typeof html !== 'string') { |
| 83 | + callback(error); |
| 84 | + } else if (typeof html !== "string") { |
94 | 85 | // Crash the server process. |
95 | | - callback(new Error('Render method must return a string')) |
| 86 | + callback(new Error("Render method must return a string")); |
96 | 87 | } else { |
97 | | - callback(null, JSON.stringify({ html: html })) |
| 88 | + callback(null, JSON.stringify({ html: html })); |
98 | 89 | } |
99 | | - }) |
| 90 | + }); |
100 | 91 | } catch (error) { |
101 | | - callback(null, JSON.stringify({ error: error.message })) |
| 92 | + callback(null, JSON.stringify({ error: error.message })); |
102 | 93 | } |
103 | | - } |
| 94 | + }; |
104 | 95 | } |
105 | 96 |
|
106 | 97 | // Redirect stdout to stderr, but save a reference so we can |
107 | 98 | // still write to stdout. |
108 | | -const stdout = process.stdout |
109 | | -Object.defineProperty(process, 'stdout', { |
| 99 | +const stdout = process.stdout; |
| 100 | +Object.defineProperty(process, "stdout", { |
110 | 101 | configurable: true, |
111 | 102 | enumerable: true, |
112 | 103 | value: process.stderr |
113 | | -}) |
| 104 | +}); |
114 | 105 |
|
115 | 106 | // Ensure console.log knows about the new stdout. |
116 | | -const Console = require('console').Console |
117 | | -Object.defineProperty(global, 'console', { |
| 107 | +const Console = require("console").Console; |
| 108 | +Object.defineProperty(global, "console", { |
118 | 109 | configurable: true, |
119 | 110 | enumerable: true, |
120 | 111 | value: new Console(process.stdout, process.stderr) |
121 | | -}) |
| 112 | +}); |
122 | 113 |
|
123 | 114 | // Read JSON blobs from stdin, pipe output to stdout. |
124 | 115 | process.stdin |
125 | 116 | .pipe(JSONStream.parse()) |
126 | 117 | .pipe(EventStream.map(createRequestHandler(process.cwd()))) |
127 | | - .pipe(stdout) |
| 118 | + .pipe(stdout); |
0 commit comments