const path = require('path');
const config = require('./config');
const LodashModuleReplacementPlugin = require('lodash-webpack-plugin');
const ESLintPlugin = require('eslint-webpack-plugin');
const MomentLocalesPlugin = require('moment-locales-webpack-plugin');

const jsSource = `./${config.sourcePath}/${config.jsDirectory}`;
const appRoot = path.resolve('./');

const entry = {
  main: [`${jsSource}/main.js`],
  sentry: [`${jsSource}/sentry.js`],
  styleguide: [`${jsSource}/styleguide/styleguide.jsx`],
  survey: [`${jsSource}/surveys/survey.js`],
  survey_admin: [`${jsSource}/surveys/survey-admin.js`],
  survey_results: [`${jsSource}/surveys/survey-results.jsx`],
  campaigns: [`${jsSource}/campaigns.js`],
  charts: [`${jsSource}/charts.js`],
  tinymce: [`${jsSource}/tinymce.js`],
  embed_course_stats: [`${jsSource}/embed_course_stats.js`],
  accordian: [`${jsSource}/accordian.js`]
};

module.exports = (env) => {
  const outputPath = path.resolve(appRoot, `${config.outputPath}/${config.jsDirectory}`);

  let devtool;
  // see https://webpack.js.org/configuration/devtool/ for the detailed descriptions of these
  if (env.production) {
    devtool = 'source-map';
  } else if (env.coverage) {
    devtool = 'cheap-module-source-map';
  } else {
    devtool = 'eval-cheap-source-map';
  }

  if (env.coverage) {
    // In coverage mode, every React component should
    // be bundled within main.js
    entry.main = [`${jsSource}/main-coverage.js`];
  }

  return {
    entry,
    output: {
      path: outputPath,
      filename: env.development ? '[name].js' : '[name].[chunkhash].js',
      publicPath: '/assets/javascripts/',
    },
    module: {
      rules: [
        {
          test: /\.jsx?$/,
          include: path.resolve(__dirname, 'app/assets/javascripts'),
          use: {
            loader: require.resolve('babel-loader'),
            options: {
              cacheDirectory: true,
              cacheCompression: false
            },
          },
        },
      ],
    },
    externals: {
      jquery: 'jQuery',
      'i18n-js': 'I18n'
    },
    plugins: [
      // Creates smaller Lodash builds by replacing feature sets of modules with noop,
      // identity, or simpler alternatives.
      new LodashModuleReplacementPlugin(config.requiredLodashFeatures),
      new MomentLocalesPlugin(),
      new ESLintPlugin({
        extensions: ['js', 'jsx'],
        exclude: ['vendor', 'node_modules'],
        cache: true,
        failOnError: !!env.production,
      }),
    ],
    optimization: {
      splitChunks: {
        cacheGroups: {
          defaultVendors: {
            test: /[\\/]node_modules[\\/]((?!(chart)).*)[\\/]/,
            chunks: chunk => !/tinymce/.test(chunk.name),
            name: 'vendors'
          }
        }
      },
    },
    devtool,
    stats: env.stats ? 'normal' : 'minimal',
  };
};


@@ -2362,33 +2362,53 @@
     
-extensions: %5B'js', '
+files: 'app/assets/javascripts/**/*.%7Bjs,
 jsx
+%7D
 '
-%5D
 ,%0A  
@@ -2417,43 +2417,37 @@
     
-exclude: %5B'vendor', 'node_modules'%5D
+failOnError: !!env.production
 ,%0A  
@@ -2452,21 +2452,23 @@
         
-cache
+threads
 : true,%0A
@@ -2479,38 +2479,55 @@
     
-failOnError: !!env.production,
+lintDirtyModulesOnly: true,%0A        cache: true
 %0A   

enable threads for eslint