From 3fdc74bdf28ffb47d4fe6db06f06f6d32a61768c Mon Sep 17 00:00:00 2001 From: Jeff Avallone Date: Fri, 16 Feb 2018 17:16:23 -0500 Subject: [PATCH] Adding color output to prerender script and relocating to script --- .gitignore | 2 +- package.json | 2 +- {src => script}/prerender.js | 12 ++++++++---- webpack.prod.js | 4 ++-- 4 files changed, 12 insertions(+), 8 deletions(-) rename {src => script}/prerender.js (61%) diff --git a/.gitignore b/.gitignore index a076b55..e6df21a 100644 --- a/.gitignore +++ b/.gitignore @@ -20,7 +20,7 @@ node_modules/ # Build output build/ -prerender/ +script/__build__/ # Coverage reports coverage/ diff --git a/package.json b/package.json index a7490c0..0ddd68e 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "start:http-server": "http-server ./build", "build": "cross-env NODE_ENV=production run-s build:webpack build:prerender", "build:webpack": "webpack --config webpack.prod.js", - "build:prerender": "node ./prerender/prerender.js", + "build:prerender": "node ./script/__build__/prerender.js", "test": "run-s test:lint 'test:unit --coverage'", "test:unit": "jest", "test:lint": "eslint --ignore-path .gitignore .", diff --git a/src/prerender.js b/script/prerender.js similarity index 61% rename from src/prerender.js rename to script/prerender.js index fad2912..988e73c 100644 --- a/src/prerender.js +++ b/script/prerender.js @@ -1,10 +1,14 @@ +// NOTE: This script *MUST* be built with webpack since it requires React +// components. The script is built and run as part of `yarn build` + import React from 'react'; import { renderToString } from 'react-dom/server'; import fs from 'fs'; import util from 'util'; import cheerio from 'cheerio'; +import colors from 'colors/safe'; -import './i18n'; +import 'i18n'; const readdir = util.promisify(fs.readdir); const readFile = util.promisify(fs.readFile); @@ -13,7 +17,7 @@ const writeFile = util.promisify(fs.writeFile); readdir('./src/pages') .then(pages => ( Promise.all(pages.map(async page => { - const Component = (await import(`./pages/${ page }/Component`)).default; + const Component = (await import(`pages/${ page }/Component`)).default; const pagePath = `./build/${ page }.html`; const markup = cheerio.load(await readFile(pagePath)); @@ -21,11 +25,11 @@ readdir('./src/pages') markup('#root').html(renderToString()); await writeFile(pagePath, markup.html()); - console.log(`${ page } prerendered`); // eslint-disable-line no-console + console.log(colors.green.bold('PRERENDERED:'), `${ page }.html`); // eslint-disable-line no-console })) )) .then(() => console.log('Done prerendering')) // eslint-disable-line no-console .catch(e => { - console.log(e.toString()); // eslint-disable-line no-console + console.error(colors.red.bold('FAIL:'), e); // eslint-disable-line no-console process.exit(1); }); diff --git a/webpack.prod.js b/webpack.prod.js index d0e4fe2..59185b0 100644 --- a/webpack.prod.js +++ b/webpack.prod.js @@ -27,12 +27,12 @@ module.exports = [ whitelist: [ /\.svg$/ ] })], entry: { - prerender: './src/prerender.js' + prerender: './script/prerender.js' }, output: { filename: '[name].js', chunkFilename: '[name].chunk.js', - path: path.resolve(__dirname, 'prerender') + path: path.resolve(__dirname, 'script/__build__') }, resolve: { modules: ['src', 'node_modules']