pages.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /**
  2. * React Static Boilerplate
  3. * Copyright (c) Konstantin Tarkus | MIT License
  4. */
  5. import File from 'vinyl';
  6. import through from 'through2';
  7. import createTemplate from 'lodash/string/template';
  8. import templateSource from 'raw!../content/index.html';
  9. import { routes, render } from './router.js';
  10. const paths = Object.keys(routes);
  11. const template = createTemplate(templateSource);
  12. export default function() {
  13. let count = 0;
  14. const stream = through.obj((file, encoding, cb) => {
  15. cb(null, new File(file));
  16. });
  17. paths.forEach(async (path) => {
  18. var filename;
  19. if (path === '/') {
  20. filename = './index.html';
  21. } else if (Object.keys(routes).some(x => x.startsWith(path + '/'))) {
  22. filename = '.' + path + '/index.html';
  23. } else {
  24. filename = '.' + path + '.html';
  25. }
  26. const body = await render(path);
  27. const contents = template({title: '', body});
  28. stream.write({
  29. path: filename,
  30. contents: new Buffer(contents)
  31. }, 'utf8', () => {
  32. if (++count === paths.length) {
  33. stream.end();
  34. }
  35. });
  36. });
  37. return stream;
  38. }