import path from 'path';

import webpack from 'webpack';

import Logger from './Logger';

function generateBaseConfig({ entry, type, tmpdir }) {
  const outFile = `happo-bundle-${type}-${Buffer.from(process.cwd()).toString('base64')}.js`;
  const babelLoader = require.resolve('babel-loader');
  const baseConfig = {
    entry,
    output: {
      filename: outFile,
      path: tmpdir,
    },
    resolve: {
      extensions: ['*', '.js', '.jsx', '.json'],
    },
    module: {
      rules: [
        {
          test: /\.js$/,
          exclude: /node_modules/,
          use: {
            loader: babelLoader,
          },
        },
      ],
    },
    plugins: [],
  };
  if (type === 'react') {
    const babelPresetReact = require.resolve('babel-preset-react');

    const [babelRule] = baseConfig.module.rules;
    babelRule.test = /\.jsx?$/;
    babelRule.use.options = { presets: [babelPresetReact] };
  }
  return baseConfig;
}

export default function createWebpackBundle(
  entry,
  { type, customizeWebpackConfig, plugins, tmpdir },
  { onBuildReady },
) {
  let config = generateBaseConfig({ entry, type, tmpdir });
  plugins.forEach((plugin) => {
    if (typeof plugin.customizeWebpackConfig === 'function') {
      config = plugin.customizeWebpackConfig(config);
    }
  });
  config = customizeWebpackConfig(config);
  const compiler = webpack(config);
  const bundleFilePath = path.join(config.output.path, config.output.filename);

  if (onBuildReady) {
    // We're in watch/dev mode
    let hash;
    compiler.watch({}, (err, stats) => {
      if (err) {
        new Logger().error(err);
      } else if (stats.compilation.errors && stats.compilation.errors.length) {
        stats.compilation.errors.forEach(e => new Logger().error(e));
      } else if (hash !== stats.hash) {
        hash = stats.hash;
        onBuildReady(bundleFilePath);
      }
    });
    return;
  }

  // We're not in watch/dev mode
  return new Promise((resolve, reject) => {
    compiler.run((err, stats) => {
      if (err) {
        reject(err);
        return;
      }
      if (stats.compilation.errors && stats.compilation.errors.length) {
        reject(stats.compilation.errors[0]);
        return;
      }
      resolve(bundleFilePath);
    });
  });
}

@@ -699,21 +699,204 @@
 ) %7B%0A    
-const
+let babelPresetReact;%0A    try %7B%0A      // try with the babel 7 package%0A      babelPresetReact = require.resolve('@babel/preset-react');%0A    %7D catch (e) %7B%0A      // fall back to regular%0A     
  babelPr
@@ -945,16 +945,22 @@
 react');
+%0A    %7D
 %0A%0A    co

Use babel 7 compatible react preset if it exists