{"size": 1474, "ext": "js", "max_stars_repo_path": "tests/server.js", "max_stars_repo_name": "Tibblist/cache-fetch", "max_stars_repo_stars_event_min_datetime": "2020-05-12T03:47:59.000Z", "max_stars_repo_stars_event_max_datetime": "2020-05-12T03:47:59.000Z", "max_issues_repo_path": "tests/server.js", "max_issues_repo_name": "Tibblist/cache-fetch", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "tests/server.js", "max_forks_repo_name": "Tibblist/cache-fetch", "max_forks_repo_forks_event_min_datetime": "2019-07-10T12:35:13.000Z", "max_forks_repo_forks_event_max_datetime": "2019-07-10T12:35:13.000Z"}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 26.8, "max_line_length": 74, "alphanum_fraction": 0.6071913161}
const express = require("express");
const webpack = require("webpack");
const middleware = require("webpack-dev-middleware");

const compiler = webpack(require("../webpack.config.js"));

// Turns input into an array if not one already
function normalizeArray(arr) {
  return Array.isArray(arr) ? arr : [arr];
}

// Gets all the Javascript paths that Webpack has compiled, across chunks
function getAllJsPaths(webpackJson) {
  const { assetsByChunkName } = webpackJson;
  return Object.values(assetsByChunkName).reduce((paths, assets) => {
    for (let asset of normalizeArray(assets)) {
      if (asset != null && asset.endsWith(".js")) {
        paths.push(asset);
      }
    }
    return paths;
  }, []);
}

let port = 4444;
const index = Math.max(
  process.argv.indexOf("--port"),
  process.argv.indexOf("-p")
);
if (index !== -1) {
  port = +process.argv[index + 1] || port;
}

const app = express()
  .use(middleware(compiler, { serverSideRender: true }))
  .use((req, res) => {
    const webpackJson = res.locals.webpackStats.toJson();
    const paths = getAllJsPaths(webpackJson);
    res.send(
      `<!DOCTYPE html>
      <html>
        <head>
          <title>Test</title>
        </head>
        <body>
          <div id="root">Running Tests!</div>
          ${paths.map(path => `<script src="${path}"></script>`).join("")}
        </body>
      </html>`
    );
  })
  .listen(port, () => {
    console.log(`Server started at http://localhost:${port}/`);
  });
