diff --git a/package.json b/package.json index b18438df8..219df4d16 100644 --- a/package.json +++ b/package.json @@ -156,6 +156,7 @@ "bundle": "babel-node tools/run bundle", "build": "babel-node tools/run build", "deploy": "babel-node tools/run deploy", + "render": "babel-node tools/run render", "start": "babel-node tools/run start" } } diff --git a/tools/README.md b/tools/README.md index 85881717d..6b4fefa42 100644 --- a/tools/README.md +++ b/tools/README.md @@ -27,6 +27,7 @@ Flag | Description ----------- | -------------------------------------------------- `--release` | Minimizes and optimizes the compiled output `--verbose` | Prints detailed information to the console +`--static` | Renders [specified routes](./render.js#L15) as static html files For example: diff --git a/tools/build.js b/tools/build.js index 482de1bda..9ce2632a6 100644 --- a/tools/build.js +++ b/tools/build.js @@ -11,6 +11,7 @@ import run from './run'; import clean from './clean'; import copy from './copy'; import bundle from './bundle'; +import render from './render'; /** * Compiles the project from source files into a distributable @@ -20,6 +21,10 @@ async function build() { await run(clean); await run(copy); await run(bundle); + + if (process.argv.includes('--static')) { + await run(render); + } } export default build; diff --git a/tools/render.js b/tools/render.js new file mode 100644 index 000000000..5512f8e17 --- /dev/null +++ b/tools/render.js @@ -0,0 +1,45 @@ +/** + * React Starter Kit (https://www.reactstarterkit.com/) + * + * Copyright © 2014-2016 Kriasoft, LLC. All rights reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE.txt file in the root directory of this source tree. + */ + +import runServer from './runServer'; +import fs from './lib/fs'; +import fetch from 'node-fetch'; +import { host } from '../src/config'; + +// Enter your paths here which you want to render as static +const routes = [ + '/', + '/contact', + '/login', + '/register', + '/about', + '/privacy', + '/404', // https://help.github.com/articles/creating-a-custom-404-page-for-your-github-pages-site/ +]; + +async function render() { + let server; + await new Promise(resolve => (server = runServer(resolve))); + + await routes.reduce((promise, route) => promise.then(async () => { + const url = `http://${host}${route}`; + const dir = `build/public${route.replace(/[^\/]*$/, '')}`; + const name = route.endsWith('/') ? 'index.html' : `${route.match(/[^/]+$/)[0]}.html`; + const dist = `${dir}${name}`; + const res = await fetch(url); + const text = await res.text(); + await fs.makeDir(dir); + await fs.writeFile(dist, text); + console.log(`${dist} => ${res.status} ${res.statusText}`); + }), Promise.resolve()); + + server.kill('SIGTERM'); +} + +export default render; diff --git a/tools/runServer.js b/tools/runServer.js index 4846be6fc..f2bb51316 100644 --- a/tools/runServer.js +++ b/tools/runServer.js @@ -47,6 +47,7 @@ function runServer(cb) { server.stdout.on('data', onStdOut); server.stderr.on('data', x => process.stderr.write(x)); + return server; } process.on('exit', () => {