2018-02-16 22:16:23 +00:00
|
|
|
// NOTE: This script *MUST* be built with webpack since it requires React
|
|
|
|
// components. The script is built and run as part of `yarn build`
|
|
|
|
|
2018-02-13 22:10:32 +00:00
|
|
|
import React from 'react';
|
|
|
|
import { renderToString } from 'react-dom/server';
|
|
|
|
import fs from 'fs';
|
2018-02-16 01:26:03 +00:00
|
|
|
import util from 'util';
|
2018-02-13 22:10:32 +00:00
|
|
|
import cheerio from 'cheerio';
|
2018-02-16 22:16:23 +00:00
|
|
|
import colors from 'colors/safe';
|
2018-02-13 22:10:32 +00:00
|
|
|
|
2018-02-16 22:16:23 +00:00
|
|
|
import 'i18n';
|
2018-02-13 22:10:32 +00:00
|
|
|
|
2018-02-16 01:26:03 +00:00
|
|
|
const readdir = util.promisify(fs.readdir);
|
|
|
|
const readFile = util.promisify(fs.readFile);
|
|
|
|
const writeFile = util.promisify(fs.writeFile);
|
2018-02-13 22:10:32 +00:00
|
|
|
|
2018-02-16 01:26:03 +00:00
|
|
|
readdir('./src/pages')
|
|
|
|
.then(pages => (
|
|
|
|
Promise.all(pages.map(async page => {
|
2018-02-16 22:16:23 +00:00
|
|
|
const Component = (await import(`pages/${ page }/Component`)).default;
|
2018-02-16 01:26:03 +00:00
|
|
|
const pagePath = `./build/${ page }.html`;
|
2018-02-13 22:10:32 +00:00
|
|
|
|
2018-02-16 01:26:03 +00:00
|
|
|
const markup = cheerio.load(await readFile(pagePath));
|
2018-02-13 22:10:32 +00:00
|
|
|
|
2018-02-16 01:26:03 +00:00
|
|
|
markup('#root').html(renderToString(<Component/>));
|
|
|
|
await writeFile(pagePath, markup.html());
|
|
|
|
|
2018-02-16 22:16:23 +00:00
|
|
|
console.log(colors.green.bold('PRERENDERED:'), `${ page }.html`); // eslint-disable-line no-console
|
2018-02-16 01:26:03 +00:00
|
|
|
}))
|
|
|
|
))
|
|
|
|
.then(() => console.log('Done prerendering')) // eslint-disable-line no-console
|
|
|
|
.catch(e => {
|
2018-02-16 22:16:23 +00:00
|
|
|
console.error(colors.red.bold('FAIL:'), e); // eslint-disable-line no-console
|
2018-02-16 01:26:03 +00:00
|
|
|
process.exit(1);
|
2018-02-13 22:10:32 +00:00
|
|
|
});
|