text: const webpack = require('webpack');
  const path = require('path');
  const ExtractTextPlugin = require('extract-text-webpack-plugin');
  
  const sourcePath = path.join(__dirname, './client');
  const staticsPath = path.join(__dirname, './static');
  
  module.exports = function (env) {
    const nodeEnv = env && env.prod ? 'production' : 'development';
    const isProd = nodeEnv === 'production';
  
    const extractGlobalCss = new ExtractTextPlugin({
      filename: '[name].[chunkhash].global.css',
      allChunks: true,
      disable: !isProd,
    });
  
    const extractFontAwesome = new ExtractTextPlugin({
      filename: 'font-awesome.[chunkhash].css',
      allChunks: true,
      disable: false,
    });
  
    const plugins = [
      // new webpack.optimize.CommonsChunkPlugin({
      //   name: 'vendor',
      //   minChunks: Infinity,
      //   filename: 'vendor.bundle.js'
      // }),
      new webpack.DefinePlugin({
        'process.env': {NODE_ENV: JSON.stringify(nodeEnv)}
      }),
      new webpack.NamedModulesPlugin(),
      new webpack.HotModuleReplacementPlugin(),
      extractGlobalCss,
      extractFontAwesome,
    ];
  
    if (isProd) {
      plugins.push(
        new webpack.LoaderOptionsPlugin({
          minimize: true,
          debug: false
        }),
        new webpack.optimize.UglifyJsPlugin({
          compress: {
            warnings: false,
            screw_ie8: true,
            conditionals: true,
            unused: true,
            comparisons: true,
            sequences: true,
            dead_code: true,
            evaluate: true,
            if_return: true,
            join_vars: true,
          },
          output: {
            comments: false,
          },
        })
      );
    } else {
      plugins.push(
        new webpack.HotModuleReplacementPlugin()
      );
    }
  
    return {
      devtool: isProd ? 'source-map' : 'eval',
      context: sourcePath,
      entry: {
        js: './index.js',
        vendor: ['react']
      },
      output: {
        path: staticsPath,
        filename: '[name].bundle.js',
      },
      module: {
        rules: [
          {
            test: /\.html$/,
            exclude: /node_modules/,
            use: {
              loader: 'file-loader',
              query: {
                name: '[name].[ext]'
              },
            },
          },
          {
            test: /\.css$/,
            exclude: /node_modules/,
            use: [
              'style-loader',
              'css-loader'
            ]
          },
          {
            test: /\.(js|jsx)$/,
            exclude: /node_modules/,
            use: [
              'babel-loader'
            ]
          },
          {   // Treat font-awesome apart as the font are not loading well when imported with style-loader.
            test: /.scss$/,
            include: /font-awesome/,
            use: extractFontAwesome.extract({
              fallback: 'style-loader',
              use: [
                {
                  loader: 'css-loader',
                  options: {
                    sourceMap: !isProd,
                  },
                }, {
                  loader: 'sass-loader',
                  options: {
                    sourceMap: !isProd,
                  },
                },
              ],
            }),
          },
          {
            // Global SASS files
            test: /^((?!local).)*\.scss/,
            exclude: /font-awesome/,
            use: extractGlobalCss.extract({
              fallback: 'style-loader',
              use: [
                {
                  loader: 'css-loader',
                  options: {
                    sourceMap: !isProd,
                  },
                }, {
                  loader: 'sass-loader',
                  options: {
                    sourceMap: !isProd,
                  },
                },
              ],
            }),
          },
          {
            test: /\.(ttf|eot|svg|woff|woff2)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
            use: {
              loader: 'file-loader',
              options: {
                name: '[name].[ext]'
              },
            },
          },
        ],
      },
      resolve: {
        extensions: ['.webpack-loader.js', '.web-loader.js', '.loader.js', '.js', '.jsx'],
        alias: {
          'ui-css': path.join(__dirname, './node_modules/ui-css/lib'),
          'react-ui-components': path.join(__dirname, './node_modules/react-ui-components/lib/components'),
        },
        modules: [
          path.resolve(__dirname, 'node_modules'),
          sourcePath
        ]
      },
  
      plugins,
  
      performance: isProd && {
        maxAssetSize: 100,
        maxEntrypointSize: 300,
        hints: 'warning',
      },
      externals: {
        'react/addons': 'react',
        'react/lib/ExecutionEnvironment': 'react',
        'react/lib/ReactContext': 'react',
      },
      stats: {
        colors: {
          green: '\u001b[32m',
        }
      },
  
      devServer: {
        contentBase: './client',
        historyApiFallback: true,
        port: 3000,
        compress: isProd,
        inline: !isProd,
        hot: !isProd,
        stats: {
          assets: true,
          children: false,
          chunks: false,
          hash: false,
          modules: false,
          publicPath: false,
          timings: true,
          version: false,
          warnings: true,
          colors: {
            green: '\u001b[32m',
          }
        },
      }
    };
  };
  