const webpack = require('webpack');
const UglifyJsPlugin = require('webpack/lib/optimize/UglifyJsPlugin');
const path = require('path');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;

const minify = process.env.MINIFY ? '.min': '';
const plugins = minify ? [new UglifyJsPlugin({ minimize: true })] : [];
const WebpackShellPlugin = require('webpack-shell-plugin');

// plugins.push(new BundleAnalyzerPlugin())
plugins.push(new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/));
if (process.env.NODE_ENV !== 'production') {
  plugins.push(new WebpackShellPlugin({
    onBuildExit: ['echo "Pushing yalc"','yalc push'],
  }));
}

const config = {
  entry: {
    [`index${minify}.js`]: [`${__dirname}/app/index.js`]
  },
  devtool: 'source-map',
  output: {
    path: `${__dirname}/lib`,
    filename: '[name]',
    library: 'index',
    libraryTarget: 'umd',
    umdNamedDefine: true
  },
  externals: [
    'react',
    'react-dom'
  ],
  module: {
    loaders: [
      {
        test: /(\.jsx|\.js)$/,
        loader: 'babel-loader',
        exclude: /(node_modules|bower_components)/
      },
      {
        test: /\.scss$/,
        loader: 'style!css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!postcss!sass'
      },
      {
        test: /\.(png|gif)(\?.*)?$/,
        loader: 'url-loader?mimetype=image/png'
      },
    ]
  },
  resolve: {
    root: path.resolve('./app'),
    extensions: ['', '.js']
  },
  plugins: plugins,
  postcss: {}
};

module.exports = config;
