const webpack = require('webpack');
const path = require("path");
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;

const prod = (process.env.NODE_ENV === 'production');

/**
 * Generates the bundles. Details of how to use are available in README.md
 */
module.exports = function(stats) {
  const config = {
    entry: {
      'TagsInput': './src/TagsInput.js',
      'index': './example/index.js'
    },
    output: {
      path: path.resolve('build/'),
      filename: '[name].js',
    },
    resolve: {
      mainFields: ['jsnext:main', 'browser', 'main'],
    },
    module: {
      rules: [
        {
          test: /\.js$/,
          exclude: /node_modules/,
          use: [{
            loader: 'babel-loader',
            options: {
              presets: [
                'react', 
                // needed for tree shaking
                // see https://medium.com/modus-create-front-end-development/webpack-2-tree-shaking-configuration-9f1de90f3233
                ["es2015", { "modules": false }]
              ],
              plugins: ['transform-object-rest-spread']
            }
          }]
        }
      ]
    },
    plugins: [
      
      // do not generate artifacts if there are errors
      new webpack.NoEmitOnErrorsPlugin(),

      // save sourcemap next to the files
      new webpack.SourceMapDevToolPlugin({
        filename: '[name].js.map',
        columns: false
      }),

    ]
  }

  if (stats) {
    // show the composition of the bundles
    config.plugins.push(new BundleAnalyzerPlugin());
  }

  return config;

}