const Path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const Webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = (options) => {
  const ExtractSASS = new ExtractTextPlugin(`css/${options.cssFileName}`);

  const webpackConfig = {
    devtool: options.devtool,
    entry: [
      `webpack-dev-server/client?http://localhost:${+ options.port}`,
      'webpack/hot/dev-server',
      Path.join(__dirname, '../src/index'),
    ],
    output: {
      path: Path.join(__dirname, '../../src/main/resources/public'),
      filename: `js/${options.jsFileName}`,
    },
    resolve: {
      extensions: ['', '.js', '.jsx'],
    },
    module: {
      loaders: [{
        test: /.jsx?$/,
        include: Path.join(__dirname, '../src'),
        loader: 'babel',
      }],
    },
    plugins: [
      new Webpack.DefinePlugin({
        'process.env': {
          NODE_ENV: JSON.stringify(options.isProduction ? 'production' : 'development'),
        },
      }),
      new HtmlWebpackPlugin({
        template: Path.join(__dirname, '../src/index.html'),
      }),
    ],
  };

  if (options.isProduction) {
    webpackConfig.entry = [Path.join(__dirname, '../src/index')];

    webpackConfig.plugins.push(
      new Webpack.optimize.OccurenceOrderPlugin(),
      new Webpack.optimize.UglifyJsPlugin({
        compressor: {
          warnings: false,
        },
      }),
      ExtractSASS
    );

    webpackConfig.module.loaders.push({
      test: /\.scss$/,
      loader: ExtractSASS.extract(['css', 'sass']),
    });
  } else {
    webpackConfig.plugins.push(
      new Webpack.HotModuleReplacementPlugin()
    );

    webpackConfig.module.loaders.push({
      test: /\.scss$/,
      loaders: ['style', 'css', 'sass'],
    });

    webpackConfig.devServer = {
      contentBase: Path.join(__dirname, '../'),
      hot: true,
      port: options.port,
      inline: true,
      progress: true,
      historyApiFallback: true,
    };
  }

  return webpackConfig;
};

@@ -290,16 +290,56 @@
 Name%7D%60);
+%0A  const __DEV__ = options.isProduction;
 %0A%0A  cons
@@ -879,16 +879,603 @@
 babel',%0A
+      %7D, %7B%0A        test: /%5C.json$/,%0A        loader: 'json-loader',%0A      %7D,%0A        %7B%0A          test: /%5C.txt$/,%0A          loader: 'raw-loader',%0A        %7D,%0A        %7B%0A          test: /%5C.(png%7Cjpg%7Cjpeg%7Cgif%7Csvg%7Cwoff%7Cwoff2)$/,%0A          loader: 'url-loader',%0A          query: %7B%0A            name: __DEV__ ? '%5Bpath%5D%5Bname%5D.%5Bext%5D?%5Bhash%5D' : '%5Bhash%5D.%5Bext%5D',%0A            limit: 10000,%0A          %7D,%0A        %7D,%0A        %7B%0A          test: /%5C.(eot%7Cttf%7Cwav%7Cmp3)$/,%0A          loader: 'file-loader',%0A          query: %7B%0A            name: __DEV__ ? '%5Bpath%5D%5Bname%5D.%5Bext%5D?%5Bhash%5D' : '%5Bhash%5D.%5Bext%5D',%0A          %7D,%0A  
       %7D%5D

add image loader for webpack config