<commit_before>import path from 'path';
import webpack from 'webpack';
import webpackDevMiddleware from 'webpack-dev-middleware';
import webpackHotMiddleware from 'webpack-hot-middleware';
import webpackConfig from '../../../../shared/webpack/webpack.client';
import logger from '../../../../shared/logger';

export default function addDevMiddlewares(app, config) {

  logger.info('Building your client-side app, this might take a minute.');

  const compiler = webpack(webpackConfig);
  const middleware = webpackDevMiddleware(compiler, {
    noInfo: true,
    publicPath: config.publicPath,
    silent: true,
    stats: 'errors-only',
  });

  app.use(middleware);
  app.use(webpackHotMiddleware(compiler, {
    log: logger.info.bind(logger),
  }));

  // Since webpackDevMiddleware uses memory-fs internally to store build
  // artifacts, we use it instead
  const fs = middleware.fileSystem;

  // TODO dllPlugin
  // if (dllPlugin) {
  //   app.get(/\.dll\.js$/, (req, res) => {
  //     const filename = req.path.replace(/^\//, '');
  //     res.sendFile(path.join(process.cwd(), dllPlugin.path, filename));
  //   });
  // }

  // TODO mechanism for 404, 500, ... routes.
  return function serveRoute(req, res, html) {
    fs.readFile(path.join(compiler.outputPath, 'index.html'), (err, file) => {
      if (err) {
        return res.sendStatus(404);
      }

      if (typeof html !== 'string') {
        html = '';
      }

      const pageHtml = file.toString();
      const appHtml = pageHtml.replace('%COMPONENT%', `<div>${html}</div>`);

      res.send(appHtml);
    });
  };
}<commit_msg>Use cheerio rather than regex to inject app body<commit_after>import path from 'path';
import webpack from 'webpack';
import webpackDevMiddleware from 'webpack-dev-middleware';
import webpackHotMiddleware from 'webpack-hot-middleware';
import cheerio from 'cheerio';
// import Helmet from 'react-helmet';
import webpackConfig from '../../../../shared/webpack/webpack.client';
import logger from '../../../../shared/logger';

export default function addDevMiddlewares(app, config) {

  logger.info('Building your client-side app, this might take a minute.');

  const compiler = webpack(webpackConfig);
  const middleware = webpackDevMiddleware(compiler, {
    noInfo: true,
    publicPath: config.publicPath,
    silent: true,
    stats: 'errors-only',
  });

  app.use(middleware);
  app.use(webpackHotMiddleware(compiler, {
    log: logger.info.bind(logger),
  }));

  // Since webpackDevMiddleware uses memory-fs internally to store build
  // artifacts, we use it instead
  const fs = middleware.fileSystem;

  // TODO dllPlugin
  // if (dllPlugin) {
  //   app.get(/\.dll\.js$/, (req, res) => {
  //     const filename = req.path.replace(/^\//, '');
  //     res.sendFile(path.join(process.cwd(), dllPlugin.path, filename));
  //   });
  // }

  // TODO mechanism for 404, 500, ... routes.
  return function serveRoute(req, res, html) {
    fs.readFile(path.join(compiler.outputPath, 'index.html'), (err, file) => {
      if (err) {
        return res.sendStatus(404);
      }

      if (typeof html !== 'string') {
        html = '';
      }

      // TODO inject helmet
      const doc = cheerio(file.toString());
      doc.find('#app').append(`<div>${html}</div>`);

      res.send(doc.toString());
    });
  };
}
{'fraction_non_alphanumeric': 0.1655130905808005, 'fraction_numerical': 0.005416792055371652, 'mean_word_length': 3.019347037484885, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '14032', 'n_tokens_mistral': 1051, 'n_tokens_neox': 1021, 'n_words': 250}