Skip to content

Commit

Permalink
Add --static flag to build script which allows to generate static htm…
Browse files Browse the repository at this point in the history
…l for specific routes (#642)
  • Loading branch information
frenzzy authored and koistya committed May 13, 2016
1 parent 629bea0 commit 200c229
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
1 change: 1 addition & 0 deletions tools/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
5 changes: 5 additions & 0 deletions tools/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
45 changes: 45 additions & 0 deletions tools/render.js
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions tools/runServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 200c229

Please sign in to comment.