text: 'use strict'
  
  const autoprefixer = require('autoprefixer')
  const webpack = require('webpack')
  const HtmlWebpackPlugin = require('html-webpack-plugin')
  const ExtractTextPlugin = require('extract-text-webpack-plugin')
  const InterpolateHtmlPlugin = require('react-dev-utils-academy/InterpolateHtmlPlugin')
  const CompressionPlugin = require('compression-webpack-plugin')
  const paths = require('./paths')
  
  const publicPath = paths.servedPath
  const shouldUseRelativeAssetPaths = publicPath === './'
  
  if (process.env.NODE_ENV !== 'production') {
    throw new Error('Production builds must have NODE_ENV=production.')
  }
  
  // Note: defined here because it will be used more than once.
  const cssFilename = 'static/css/[name].[contenthash:8].css'
  
  // ExtractTextPlugin expects the build output to be flat.
  const extractTextPluginOptions = shouldUseRelativeAssetPaths
    ? { publicPath: Array(cssFilename.split('/').length).join('../') }
    : {}
  
  module.exports = {
    bail: true,
    devtool: 'source-map',
    entry: {
      vendor: [paths.appVendorJs]
    },
    output: {
      path: paths.appBuildVendor,
      filename: '[name].js',
      chunkFilename: '[name].js',
      library: '[name]',
      publicPath
    },
    resolve: {
      modules: [paths.appVendorJs, 'node_modules', paths.appNodeModules],
      extensions: ['.js']
    },
    resolveLoader: {
      modules: [paths.appNodeModules]
    },
    module: {
      rules: [
        // Disable require.ensure as it's not a standard language feature.
        { parser: { requireEnsure: false } },
        // First, run the linter.
        {
          test: /\.(js|jsx)$/,
          enforce: 'pre',
          use: [
            {
              options: {
                useEslintrc: true
              },
              loader: 'eslint-loader'
            }
          ],
          include: paths.appVendorJs
        },
        {
          exclude: [
            /\.html$/,
            /\.(js|jsx)$/,
            /\.css$/,
            /\.json$/,
            /\.bmp$/,
            /\.gif$/,
            /\.jpe?g$/,
            /\.png$/
          ],
          loader: 'file-loader',
          options: {
            name: 'static/[name].[hash:8].[ext]'
          }
        },
        {
          test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
          loader: 'url-loader',
          options: {
            limit: 10000,
            name: 'static/img/[name].[hash:8].[ext]'
          }
        },
        {
          test: /\.(js|jsx)$/,
          include: paths.appVendorJs,
          loader: 'babel-loader',
          exclude: [/\.test\.jsx?$/, /\.story\.(js|jsx)$/],
          options: {
            babelrc: true
          }
        },
        {
          test: /\.css$/,
          loader: ExtractTextPlugin.extract(
            Object.assign(
              {
                fallback: 'style-loader',
                use: [
                  {
                    loader: 'css-loader',
                    options: {
                      importLoaders: 1
                    }
                  },
                  {
                    loader: 'postcss-loader',
                    options: {
                      ident: 'postcss', // https://webpack.js.org/guides/migrating/#complex-options
                      plugins: () => [
                        autoprefixer({
                          browsers: [
                            '>1%',
                            'last 4 versions',
                            'Firefox ESR',
                            'not ie < 9' // React doesn't support IE8 anyway
                          ]
                        })
                      ]
                    }
                  }
                ]
              },
              extractTextPluginOptions
            )
          )
        }
        // ** STOP ** Are you adding a new loader?
        // Remember to add the new extension(s) to the "url" loader exclusion list.
      ]
    },
    plugins: [
      new InterpolateHtmlPlugin(process.env.NODE_ENV),
  
      new HtmlWebpackPlugin({
        inject: true,
        template: paths.appHtml,
        minify: {
          removeComments: true,
          collapseWhitespace: true,
          removeRedundantAttributes: true,
          useShortDoctype: true,
          removeEmptyAttributes: true,
          removeStyleLinkTypeAttributes: true,
          keepClosingSlash: true,
          minifyJS: true,
          minifyCSS: true,
          minifyURLs: true
        }
      }),
  
      new webpack.DefinePlugin({
        'process.env': {
          NODE_ENV: JSON.stringify('production')
        }
      }),
  
      new webpack.optimize.UglifyJsPlugin({
        compress: {
          screw_ie8: true, // React doesn't support IE8
          warnings: false,
          conditionals: true,
          unused: true,
          comparisons: true,
          sequences: true,
          dead_code: true,
          evaluate: true,
          if_return: true,
          join_vars: true
        },
        mangle: {
          screw_ie8: true
        },
        output: {
          comments: false,
          screw_ie8: true
        },
        sourceMap: true
      }),
  
      new webpack.optimize.AggressiveMergingPlugin({
        minSizeReduce: 1.5,
        moveToParents: true
      }),
  
      new webpack.DllPlugin({
        path: paths.appManifestVendor,
        name: '[name]',
        context: paths.appVendor
      }),
  
      new CompressionPlugin({
        asset: '[path].gz[query]',
        algorithm: 'gzip',
        test: /\.(js|html|css)$/,
        threshold: 10240,
        minRatio: 0.8
      })
    ],
    node: {
      fs: 'empty',
      net: 'empty',
      tls: 'empty'
    },
    profile: true
  }
  