Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --static flag to build script which allows to generate static html for specific routes #642

Merged
merged 1 commit into from
May 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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