|
| 1 | +var createMemoryHistory = require('history/lib/createMemoryHistory'); |
| 2 | +var url = require('url'); |
| 3 | +var babelCore = require('babel-core'); |
| 4 | +var babelConfig = { |
| 5 | + presets: ["es2015", "react"] |
| 6 | +}; |
| 7 | + |
| 8 | +var origJsLoader = require.extensions['.js']; |
| 9 | +require.extensions['.js'] = loadViaBabel; |
| 10 | +require.extensions['.jsx'] = loadViaBabel; |
| 11 | + |
| 12 | +function loadViaBabel(module, filename) { |
| 13 | + // Assume that all the app's own code is ES2015+ (optionally with JSX), but that none of the node_modules are. |
| 14 | + // The distinction is important because ES2015+ forces strict mode, and it may break ES3/5 if you try to run it in strict |
| 15 | + // mode when the developer didn't expect that (e.g., current versions of underscore.js can't be loaded in strict mode). |
| 16 | + var useBabel = filename.indexOf('node_modules') < 0; |
| 17 | + if (useBabel) { |
| 18 | + var transformedFile = babelCore.transformFileSync(filename, babelConfig); |
| 19 | + return module._compile(transformedFile.code, filename); |
| 20 | + } else { |
| 21 | + return origJsLoader.apply(this, arguments); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +var domainTasks = require('./domain-tasks.js'); |
| 26 | +var bootServer = require('./boot-server.jsx').default; |
| 27 | + |
| 28 | +function render(requestUrl, callback) { |
| 29 | + var store; |
| 30 | + var params = { |
| 31 | + location: url.parse(requestUrl), |
| 32 | + history: createMemoryHistory(requestUrl), |
| 33 | + state: undefined |
| 34 | + }; |
| 35 | + |
| 36 | + // Open a new domain that can track all the async tasks commenced during first render |
| 37 | + domainTasks.run(function() { |
| 38 | + // Since route matching is asynchronous, add the rendering itself to the list of tasks we're awaiting |
| 39 | + domainTasks.addTask(new Promise(function (resolve, reject) { |
| 40 | + // Now actually perform the first render that will match a route and commence associated tasks |
| 41 | + bootServer(params, function(error, result) { |
| 42 | + if (error) { |
| 43 | + reject(error); |
| 44 | + } else { |
| 45 | + // The initial 'loading' state HTML is irrelevant - we only want to capture the state |
| 46 | + // so we can use it to perform a real render once all data is loaded |
| 47 | + store = result.store; |
| 48 | + resolve(); |
| 49 | + } |
| 50 | + }); |
| 51 | + })); |
| 52 | + }).then(function() { |
| 53 | + // By now, all the data should be loaded, so we can render for real based on the state now |
| 54 | + params.state = store.getState(); |
| 55 | + bootServer(params, function(error, result) { |
| 56 | + if (error) { |
| 57 | + callback(error, null); |
| 58 | + } else { |
| 59 | + var html = result.html + `<script>window.__INITIAL_STATE = ${ JSON.stringify(store.getState()) }</script>`; |
| 60 | + callback(null, html) |
| 61 | + } |
| 62 | + }); |
| 63 | + }).catch(function(error) { |
| 64 | + callback(error, null); |
| 65 | + }); |
| 66 | +} |
| 67 | + |
| 68 | +render('/', (err, html) => { |
| 69 | + if (err) { |
| 70 | + throw err; |
| 71 | + } |
| 72 | + |
| 73 | + console.log(html); |
| 74 | +}); |
0 commit comments