import path from 'path';
import ExtractTextPlugin from 'extract-text-webpack-plugin';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import webpack from 'webpack';
import MinifyPlugin from 'babel-minify-webpack-plugin';

const ENV = process.env.NODE_ENV || 'development';

export default {
  entry: ['babel-polyfill', './src/entry.js'],
  output: {
    path: path.resolve(__dirname, 'static', 'js'),
    filename: 'rookeries.js'
  },

  module: {

    rules: [
      {
        enforce: 'pre',
        test: /\.js$/,
        exclude: [
          /node_modules/,
        ],
        loader: 'eslint-loader',
        options: {
          emitWarning: true
        }
      },
      {
        test: /\.js/,
        include: [
          path.resolve(__dirname, 'src'),
        ],
        exclude: [
          /node_modules/,
        ],

        loader: 'babel-loader'
      },
      {
        test: /\.css$/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: 'css-loader'
        })
      },
      {
        test: /\.html/,
        loader: 'html-loader',
      },

      // From https://github.com/shakacode/font-awesome-loader/blob/master/docs/usage-webpack2.md
      {
        test: /\.woff2?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
        // Limiting the size of the woff fonts breaks font-awesome ONLY for the extract text plugin
        // loader: "url?limit=10000"
        use: 'url-loader'
      },
      {
        test: /\.(ttf|eot|svg)(\?[\s\S]+)?$/,
        use: 'file-loader'
      },
    ]
  },

  devtool: ENV === 'production' ? 'source-map' : 'cheap-module-eval-source-map',

  devServer: {
    contentBase: './',
    historyApiFallback: true,
    host: '0.0.0.0',
    hot: true,
    port: 3000,
    proxy: {
      '/api': 'http://localhost:5000',
      '/auth': 'http://localhost:5000'
    }
  },

  resolve: {
    alias: {
      'react': 'preact-compat',
      'react-dom': 'preact-compat',
    }
  },

  plugins: ([
    new webpack.NoEmitOnErrorsPlugin(),
    new ExtractTextPlugin('rookeries.css'),
    new HtmlWebpackPlugin({
      favicon: './static/images/mr-penguin-amber-favicon.ico',
      title: 'Rookeries - Webpack Dev',
      template: './templates/webpack_base.html',
      xhtml: true
    }),
    new webpack.HotModuleReplacementPlugin(),
    new webpack.DefinePlugin({
      'process.env.BROWSER_ENV': JSON.stringify(true)
    })
  ]).concat(ENV === 'production'
    ? [new MinifyPlugin()]
    : [])
};
