/*
Copyright (C) 2017  Cloudbase Solutions SRL
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const splitVendor = require('webpack-blocks-split-vendor')
const happypack = require('webpack-blocks-happypack')
const ProgressPlugin = require('webpack/lib/ProgressPlugin')

const {
  addPlugins, createConfig, entryPoint, env, setOutput,
  sourceMaps, defineConstants, webpack,
} = require('@webpack-blocks/webpack2')

const sourceDir = 'src'
const publicPath = `/${process.env.PUBLIC_PATH || ''}/`.replace('//', '/')
const sourcePath = path.join(process.cwd(), sourceDir)
const outputPath = path.join(process.cwd(), 'dist')
process.noDeprecation = true

const babel = () => () => ({
  module: {
    rules: [
      { test: /\.jsx?$/, exclude: /node_modules/, loader: 'babel-loader' },
    ],
  },
})

const assets = () => () => ({
  module: {
    rules: [
      {
        test: /\.(png|jpe?g|svg|woff2?|ttf|eot)$/,
        loader: 'url-loader',
        options: {
          limit: 8192,
          name: './assets/[hash].[ext]',
        },
      },
    ],
  },
})

const resolveModules = modules => () => ({
  resolve: {
    modules: [].concat(modules, ['node_modules']),
  },
})

const node = () => () => ({
  node: {
    console: true,
    fs: 'empty',
    net: 'empty',
    tls: 'empty',
  },
})

const config = createConfig([
  setOutput({
    filename: '[name].js',
    path: outputPath,
    publicPath,
  }),
  defineConstants({
    'process.env.NODE_ENV': process.env.NODE_ENV,
    'process.env.PUBLIC_PATH': publicPath.replace(/\/$/, ''),
  }),
  () => ({ stats: 'errors-only' }),
  addPlugins([
    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: path.join(process.cwd(), 'public/index.html'),
    }),
    new ProgressPlugin(),
  ]),
  happypack([
    babel(),
  ]),
  assets(),
  resolveModules(sourceDir),
  node(),

  env('development', [
    entryPoint({
      app: [sourcePath, 'webpack-hot-middleware/client?path=/__webpack_hmr&timeout=20000'],
    }),
    sourceMaps(),
    addPlugins([
      new webpack.NamedModulesPlugin(),
      new webpack.HotModuleReplacementPlugin(),
      new webpack.NoEmitOnErrorsPlugin(),
    ]),
  ]),

  env('production', [
    entryPoint({
      app: sourcePath,
    }),
    splitVendor(),
    addPlugins([
      new webpack.optimize.UglifyJsPlugin({ compress: { warnings: false }, mangle: { keep_fnames: true } }),
    ]),
  ]),
])

module.exports = config

@@ -2916,24 +2916,73 @@
 itVendor(),%0A
+    () =%3E (%7B devtool: 'nosources-source-map' %7D),%0A
     addPlugi
@@ -3029,16 +3029,24 @@
 Plugin(%7B
+%0A       
  compres
@@ -3068,16 +3068,24 @@
 false %7D,
+%0A       
  mangle:
@@ -3106,16 +3106,48 @@
 : true %7D
+,%0A        sourceMap: true,%0A     
  %7D),%0A   

Use a javascript source map for production builds