|
| 1 | +/** |
| 2 | + * NOTE: This file must be run with babel-node as Node is not yet compatible |
| 3 | + * with all of ES6 and we also use JSX. |
| 4 | + */ |
| 5 | +import url from 'url'; |
| 6 | +import React from 'react'; |
| 7 | +import { renderToStaticMarkup } from 'react-dom/server'; |
| 8 | +import express from 'express'; |
| 9 | +import webpack from 'webpack'; |
| 10 | + |
| 11 | +import config from './webpack.config.dev.js'; |
| 12 | + |
| 13 | +const Html = ({ |
| 14 | + title = 'Rainbow Unicorns', |
| 15 | + bundle = '/app.js', |
| 16 | + body = '', |
| 17 | + stylesheet = '', |
| 18 | +}) => ( |
| 19 | + <html lang="en"> |
| 20 | + <head> |
| 21 | + <meta charSet="utf-8" /> |
| 22 | + <meta httpEquiv="X-UA-Compatible" content="IE=edge" /> |
| 23 | + <meta name="viewport" content="width=device-width, initial-scale=1" /> |
| 24 | + <title>{title}</title> |
| 25 | + {stylesheet && <link rel="stylesheet" href={stylesheet} />} |
| 26 | + </head> |
| 27 | + <body> |
| 28 | + <div id="root" dangerouslySetInnerHTML={{ __html: body }} /> |
| 29 | + <script src={bundle} /> |
| 30 | + </body> |
| 31 | + </html> |
| 32 | +); |
| 33 | + |
| 34 | +/** |
| 35 | + * Render the entire web page to a string. We use render to static markup here |
| 36 | + * to avoid react hooking on to the document HTML that will not be managed by |
| 37 | + * React. The body prop is a string that contains the actual document body, |
| 38 | + * which react will hook on to. |
| 39 | + * |
| 40 | + * We also take this opportunity to prepend the doctype string onto the |
| 41 | + * document. |
| 42 | + * |
| 43 | + * @param {object} props |
| 44 | + * @return {string} |
| 45 | + */ |
| 46 | +const renderDocumentToString = props => { |
| 47 | + const markup = renderToStaticMarkup(<Html {...props} />); |
| 48 | + return `<!doctype html>${markup}`; |
| 49 | +}; |
| 50 | + |
| 51 | +const app = express(); |
| 52 | +const compiler = webpack(config); |
| 53 | + |
| 54 | +app.use(require('webpack-dev-middleware')(compiler, { |
| 55 | + noInfo: true, |
| 56 | + publicPath: config.output.publicPath, |
| 57 | +})); |
| 58 | + |
| 59 | +app.use(require('webpack-hot-middleware')(compiler)); |
| 60 | + |
| 61 | +// Send the boilerplate HTML payload down for all get requests. Routing will be |
| 62 | +// handled entirely client side and we don't make an effort to pre-render pages |
| 63 | +// before they are served when in dev mode. |
| 64 | +app.get('*', (req, res) => { |
| 65 | + const html = renderDocumentToString({ |
| 66 | + bundle: `${config.output.publicPath}app.js`, |
| 67 | + }); |
| 68 | + res.send(html); |
| 69 | +}); |
| 70 | + |
| 71 | +// NOTE: url.parse can't handle URLs without a protocol explicitly defined. So |
| 72 | +// if we parse '//localhost:8888' it doesn't work. We manually add a protocol even |
| 73 | +// though we are only interested in the port. |
| 74 | +const { port } = url.parse(`http:${config.output.publicPath}`); |
| 75 | + |
| 76 | +app.listen(port, 'localhost', err => { |
| 77 | + if (err) return console.error(err); // eslint-disable-line no-console |
| 78 | + console.log(`Dev server listening at http://localhost:${port}`); // eslint-disable-line no-console |
| 79 | + |
| 80 | + return undefined; |
| 81 | +}); |
0 commit comments