{"size": 2955, "ext": "js", "max_stars_repo_path": "src/webpack/static-render-pages.js", "max_stars_repo_name": "mapbox/batfish", "max_stars_repo_stars_event_min_datetime": "2017-06-06T16:43:08.000Z", "max_stars_repo_stars_event_max_datetime": "2022-01-21T02:04:08.000Z", "max_issues_repo_path": "src/webpack/static-render-pages.js", "max_issues_repo_name": "mapbox/batfish", "max_issues_repo_issues_event_min_datetime": "2017-06-06T16:44:39.000Z", "max_issues_repo_issues_event_max_datetime": "2021-06-15T15:20:48.000Z", "max_forks_repo_path": "src/webpack/static-render-pages.js", "max_forks_repo_name": "mapbox/batfish", "max_forks_repo_forks_event_min_datetime": "2017-10-10T01:37:12.000Z", "max_forks_repo_forks_event_max_datetime": "2021-04-04T20:26:56.000Z"}
{"max_stars_count": 125, "max_issues_count": 231, "max_forks_count": 17, "avg_line_length": 33.5795454545, "max_line_length": 227, "alphanum_fraction": 0.6582064298}
// @flow

// **WARNING**
// This file gets compiled by Webpack before it is executed.
// So when it is executed, the __dirname will be {outputDirectory}/assets/.
// For any other fs activity, this file location must be taken into acconut.

import 'source-map-support/register';
import fs from 'fs';
import pify from 'pify';
import mkdirp from 'mkdirp';
import path from 'path';
import { batfishContext } from 'batfish-internal/context';
import renderInlineJsScripts from '../node/render-inline-js-scripts';
import { renderHtmlPage } from './render-html-page';

// Statically render pages as HTML.
//
// Returned Promise resolves when all HTML pages have been rendered and written.
function staticRenderPages(
  batfishConfig: BatfishConfiguration,
  assets: {
    vendor: { js: string },
    app: { js: string }
  },
  manifestJs: string,
  cssUrl?: string
): Promise<Array<void>> {
  const inlineJsScripts = renderInlineJsScripts(batfishConfig.inlineJs);

  // Load the full stylesheet lazily, after DOMContentLoaded. The page will
  // still render quickly because it will have its own CSS injected inline.
  let css = '';
  if (cssUrl) {
    if (batfishConfig.staticHtmlInlineDeferCss) {
      const loadCssJs = `document.addEventListener('DOMContentLoaded',function(){var s=document.createElement('link');s.rel='stylesheet';s.href='${cssUrl}';document.head.insertBefore(s, document.getElementById('loadCss'));});`;
      css = `<script id="loadCss">${loadCssJs}</script>`;
    } else {
      css = `<link rel="stylesheet" href="${cssUrl}" />`;
    }
  }

  const appendToBody = [
    // The Webpack manifest is inlined because it is very small.
    `<script>${manifestJs.replace(/\/\/#.*?map$/, '')}</script>`,
    `<script src="${assets.vendor.js}"></script>`,
    `<script src="${assets.app.js}"></script>`
  ];

  const writePage = (route: BatfishRouteData): Promise<void> => {
    return renderHtmlPage({
      route,
      inlineJsScripts,
      css,
      appendToBody,
      spa: batfishConfig.spa
    }).then((html) => {
      // Write every page as an index.html file in the directory corresponding
      // to its route's path. Except the 404 page.
      if (route.is404) {
        return mkdirp(batfishConfig.outputDirectory).then(() => {
          return pify(fs.writeFile)(
            path.join(batfishConfig.outputDirectory, '404.html'),
            html
          );
        });
      }

      const baseRelativePath =
        route.path === batfishConfig.siteBasePath
          ? '/'
          : route.path.slice(batfishConfig.siteBasePath.length);
      const directory = path.join(
        batfishConfig.outputDirectory,
        baseRelativePath
      );
      const indexFile = path.join(directory, 'index.html');
      return mkdirp(directory).then(() => {
        return pify(fs.writeFile)(indexFile, html);
      });
    });
  };

  return Promise.all(batfishContext.routes.map(writePage));
}

export default staticRenderPages;
