const path = require('path');
const getRepositoryName = require('git-repo-name');
const webpack = require('webpack');
const autoprefixer = require('autoprefixer');

const HtmlWebpackPlugin = require('html-webpack-plugin');
const FaviconsWebpackPlugin = require('favicons-webpack-plugin');
const LodashModuleReplacementPlugin = require('lodash-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const OpenBrowserPlugin = require('open-browser-webpack-plugin');

const production = process.env.NODE_ENV === 'production';
const pagesBuild = process.env.BUILD === 'pages';
const repositoryName = pagesBuild && getRepositoryName.sync();

/*********************************** Loaders ***********************************/
const loaders = [
  { // react-hot is implemented as babel plugin now
    test: /\.(js|jsx)$/,
    loader: 'babel',
    include: path.join(__dirname, 'src'),
    exclude: /node_modules/,
  },

  { // used for all project files and some dependencies
    test: /\.scss$/,
    loader: production
      ? ExtractTextPlugin.extract(['css', 'postcss', 'sass', 'sass-resources'])
      : ['style', 'css?sourceMap', 'postcss', 'sass?sourceMap', 'sass-resources'].join('!'),
    include: path.join(__dirname, 'src'),
  },

  { // used for dependencies that don't support sass
    test: /\.css$/,
    loaders: ['style', 'css', 'postcss'],
  },

  { // svg sprites generated only for icons
    test: /\.svg$/,
    loader: `svg-sprite?${JSON.stringify({ name: '[hash]', prefixize: true })}`,
    include: path.join(__dirname, 'src/ui/Icon'),
  },

  { // other svg images will processed as normal
    test: /\.svg$/,
    loader: 'file',
    include: path.join(__dirname, 'src'),
    exclude: path.join(__dirname, 'src/ui/Icon'),
  },
];

// Plugins used in all builds
const pluginsBase = [
  new HtmlWebpackPlugin({
    title: 'React Github Pages Boilerplate',
    template: 'template.ejs',
  }),

  new FaviconsWebpackPlugin({
    logo: './favicon.png',
    background: '#ffeeee',
    icons: {
      android: false,
      appleIcon: false,
      appleStartup: false,
      coast: false,
      favicons: true,
      firefox: false,
      opengraph: false,
      twitter: false,
      yandex: false,
      windows: false,
    },
  }),

  new webpack.DefinePlugin({
    'process.env': { // build is used for gh-pages
      NODE_ENV: JSON.stringify(process.env.NODE_ENV || ''),
    },
  }),
];

const developmentPlugins = [
  ...pluginsBase,
  new webpack.HotModuleReplacementPlugin(),
  new OpenBrowserPlugin({ url: 'http://localhost:3002' }),
];

const productionPlugins = [
  ...pluginsBase,
  new ExtractTextPlugin('style.css'),
  new LodashModuleReplacementPlugin(),
  new webpack.optimize.OccurrenceOrderPlugin(),
  new webpack.optimize.DedupePlugin(),
  new webpack.optimize.UglifyJsPlugin({
    beautify: false,
    comments: false,
    compress: {
      sequences: true,
      booleans: true,
      loops: true,
      unused: false,
      warnings: false,
      drop_console: true,
      unsafe: true,
    },
  }),
];

module.exports.loaders = loaders;
module.exports.plugins = {
  base: pluginsBase,
  development: developmentPlugins,
  production: productionPlugins,
};

module.exports = {
  devtool: production ? 'cheap-module-source-map' : 'eval',

  entry: production
    ? ['babel-polyfill', './src/index']
    : [
      'react-hot-loader/patch',
      'webpack-dev-server/client?http://localhost:3002',
      'webpack/hot/only-dev-server',
      'babel-polyfill',
      './src/index',
    ],

  output: {
    path: path.join(__dirname, 'public'),
    filename: 'bundle.js',
    publicPath: pagesBuild ? `/${repositoryName}/` : '/',
  },

  resolve: {
    root: [
      path.resolve(__dirname, 'src'),
      path.resolve(__dirname, 'node_modules'),
    ],
    extensions: ['', '.js', '.jsx'],
  },

  module: { loaders },
  plugins: production ? productionPlugins : developmentPlugins,

  sassResources: ['./src/styles/variables.scss', './src/styles/mixins.scss'],
  postcss: [autoprefixer({ browsers: ['last 4 versions'] })],
};

@@ -3058,162 +3058,8 @@
 %5D;%0A%0A
-module.exports.loaders = loaders;%0Amodule.exports.plugins = %7B%0A  base: pluginsBase,%0A  development: developmentPlugins,%0A  production: productionPlugins,%0A%7D;%0A%0A
 modu

remove needless loaders and plugins exports from webpack config