render.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /**
  2. * React Static Boilerplate
  3. * https://github.com/koistya/react-static-boilerplate
  4. * Copyright (c) Konstantin Tarkus (@koistya) | MIT license
  5. */
  6. import { join, dirname } from 'path';
  7. import glob from 'glob';
  8. import React from 'react';
  9. import createTemplate from 'lodash/string/template';
  10. import fs from './lib/fs';
  11. const template = createTemplate(`<!doctype html>
  12. <html class="no-js" lang="">
  13. <head>
  14. <meta charset="utf-8">
  15. <meta http-equiv="x-ua-compatible" content="ie=edge">
  16. <title><%- title %></title>
  17. <meta name="description" content="">
  18. <meta name="viewport" content="width=device-width, initial-scale=1">
  19. <link rel="apple-touch-icon" href="apple-touch-icon.png">
  20. </head>
  21. <body>
  22. <div id="app"><%= body %></div>
  23. <script src="/app.js"></script>
  24. <script>
  25. (function(b,o,i,l,e,r){b.GoogleAnalyticsObject=l;b[l]||(b[l]=
  26. function(){(b[l].q=b[l].q||[]).push(arguments)});b[l].l=+new Date;
  27. e=o.createElement(i);r=o.getElementsByTagName(i)[0];
  28. e.src='https://www.google-analytics.com/analytics.js';
  29. r.parentNode.insertBefore(e,r)}(window,document,'script','ga'));
  30. ga('create','UA-XXXXX-X','auto');ga('send','pageview');
  31. </script>
  32. </body>
  33. </html>`);
  34. export default async ({ pages }) => {
  35. console.log('render');
  36. const { route } = require('../build/app.node');
  37. for (const page of pages) {
  38. await route(page.path, async (component) => {
  39. const data = {
  40. title: '',
  41. body: React.renderToString(component)
  42. };
  43. const file = join(__dirname, '../build', page.file.substr(0, page.file.lastIndexOf('.')) + '.html');
  44. const html = template(data);
  45. await fs.mkdir(dirname(file));
  46. await fs.writeFile(file, html);
  47. })
  48. }
  49. };