text: 'use strict'
  
  process.env.BABEL_ENV = 'web'
  
  const devMode = process.env.NODE_ENV !== 'production'
  const path = require('path')
  const { dependencies } = require('../package.json')
  const webpack = require('webpack')
  const TerserPlugin = require('terser-webpack-plugin');
  const CopyWebpackPlugin = require('copy-webpack-plugin')
  const MiniCssExtractPlugin = require('mini-css-extract-plugin')
  const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')
  const HtmlWebpackPlugin = require('html-webpack-plugin')
  const { VueLoaderPlugin } = require('vue-loader')
  
  /**
   * List of node_modules to include in webpack bundle
   *
   * Required for specific packages like Vue UI libraries
   * that provide pure *.vue files that need compiling
   * https://simulatedgreg.gitbooks.io/electron-vue/content/en/webpack-configurations.html#white-listing-externals
   */
  let whiteListedModules = ['vue']
  
  let webConfig = {
    devtool: '#cheap-module-eval-source-map',
    entry: {
      index: path.join(__dirname, '../src/renderer/pages/index/main.js')
    },
    externals: [
      ...Object.keys(dependencies || {}).filter(d => !whiteListedModules.includes(d))
    ],
    module: {
      rules: [
        {
          test: /\.(js|vue)$/,
          enforce: 'pre',
          exclude: /node_modules/,
          use: {
            loader: 'eslint-loader',
            options: {
              formatter: require('eslint-friendly-formatter')
            }
          }
        },
        {
          test: /\.scss$/,
          use: [
            devMode ? 'vue-style-loader' : MiniCssExtractPlugin.loader,
            'css-loader',
            {
              loader: 'sass-loader',
              options: {
                prependData: '@import "@/components/Theme/Variables.scss";',
                sassOptions: {
                  includePaths:[__dirname, 'src']
                }
              },
            }
          ]
        },
        {
          test: /\.sass$/,
          use: [
            devMode ? 'vue-style-loader' : MiniCssExtractPlugin.loader,
            'css-loader',
            {
              loader: 'sass-loader',
              options: {
                indentedSyntax: true,
                prependData: '@import "@/components/Theme/Variables.scss";',
                sassOptions: {
                  includePaths:[__dirname, 'src']
                }
              },
            }
          ]
        },
        {
          test: /\.less$/,
          use: [
            devMode ? 'vue-style-loader' : MiniCssExtractPlugin.loader,
            'css-loader',
            'less-loader'
          ]
        },
        {
          test: /\.css$/,
          use: [
            devMode ? 'vue-style-loader' : MiniCssExtractPlugin.loader,
            'css-loader'
          ]
        },
        {
          test: /\.js$/,
          use: 'babel-loader',
          include: [ path.resolve(__dirname, '../src/renderer') ],
          exclude: /node_modules/
        },
        {
          test: /\.vue$/,
          use: {
            loader: 'vue-loader',
            options: {
              extractCSS: true,
              loaders: {
                sass: 'vue-style-loader!css-loader!sass-loader?indentedSyntax=1',
                scss: 'vue-style-loader!css-loader!sass-loader',
                less: 'vue-style-loader!css-loader!less-loader'
              }
            }
          }
        },
        {
          test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
          use: {
            loader: 'url-loader',
            query: {
              limit: 10000,
              name: 'imgs/[name].[ext]'
            }
          }
        },
        {
          test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
          use: {
            loader: 'url-loader',
            query: {
              limit: 10000,
              name: 'fonts/[name].[ext]'
            }
          }
        }
      ]
    },
    plugins: [
      new VueLoaderPlugin(),
      new MiniCssExtractPlugin({
        filename: '[name].css',
        chunkFilename: '[id].css'
      }),
      new OptimizeCSSPlugin({
        cssProcessorOptions: {
          safe: true,
          discardComments: { removeAll: true }
        }
      }),
      new HtmlWebpackPlugin({
        title: 'Motrix',
        filename: 'index.html',
        chunks: ['index'],
        template: path.resolve(__dirname, '../src/index.ejs'),
        templateParameters(compilation, assets, options) {
          return {
            compilation: compilation,
            webpack: compilation.getStats().toJson(),
            webpackConfig: compilation.options,
            htmlWebpackPlugin: {
              files: assets,
              options: options
            },
            process
          }
        },
        // minify: {
        //   collapseWhitespace: true,
        //   removeAttributeQuotes: true,
        //   removeComments: true
        // },
        nodeModules: devMode
          ? path.resolve(__dirname, '../node_modules')
          : false
      }),
      new webpack.DefinePlugin({
        'process.env.IS_WEB': 'true'
      }),
      new webpack.HotModuleReplacementPlugin(),
      new webpack.NoEmitOnErrorsPlugin()
    ],
    output: {
      filename: '[name].js',
      path: path.join(__dirname, '../dist/web')
    },
    resolve: {
      alias: {
        '@': path.join(__dirname, '../src/renderer'),
        '@shared': path.join(__dirname, '../src/shared'),
        'vue$': 'vue/dist/vue.esm.js'
      },
      extensions: ['.js', '.vue', '.json', '.css']
    },
    target: 'web',
    optimization: {
      minimize: !devMode,
      minimizer: [
        new TerserPlugin({
          extractComments: false,
        })
      ],
    },
  }
  
  /**
   * Adjust webConfig for production settings
   */
  if (!devMode) {
    webConfig.devtool = ''
  
    webConfig.plugins.push(
      new CopyWebpackPlugin([
        {
          from: path.join(__dirname, '../static'),
          to: path.join(__dirname, '../dist/web/static'),
          ignore: ['.*']
        }
      ]),
      new webpack.DefinePlugin({
        'process.env.NODE_ENV': '"production"'
      }),
      new webpack.LoaderOptionsPlugin({
        minimize: true
      })
    )
  }
  
  module.exports = webConfig
  