const { resolve } = require('path')

const NodePackage = require('./package.json')
const webpack = require('webpack')
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
const WebpackCleanupPlugin = require('webpack-cleanup-plugin')

const module_resolve = require('resolve')

const outputPath = resolve('./dist-client')
const publicPath = '/dist-client'

try {
  let path = module_resolve.sync('@hatiolab/things-shell', {
    basedir: process.cwd()
  })
  var thingsShellModulePath = resolve(path, '../..')
  var externModulesPath = resolve(path, '../../../..')
} catch (e) {
  console.log('@hatiolab/things-shell module not found.')
  var thingsShellModulePath = resolve(__dirname)
  var externModulesPath = resolve(__dirname, 'node_modules')
}

console.log('ThingsShell Module Path', thingsShellModulePath)
console.log('Extern Module Path', externModulesPath)

module.exports = {
  mode: 'production',
  entry: {
    bundle: [resolve(thingsShellModulePath, './client/index.js')],
    'scene-components': [
      resolve(thingsShellModulePath, './client/scene-components.js')
    ]
  },
  output: {
    publicPath,
    path: outputPath,
    filename: '[name].js'
  },
  resolve: {
    modules: [externModulesPath]
  },
  resolveLoader: {
    modules: [externModulesPath, resolve(thingsShellModulePath, 'web_loaders')]
  },
  optimization: {
    splitChunks: {
      cacheGroups: {
        commons: {
          test: /[\\/]node_modules[\\/]/,
          name: 'vendors',
          chunks: 'all'
        }
      }
    },
    minimize: true
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        include: [thingsShellModulePath, externModulesPath],
        use: {
          loader: 'babel-loader',
          options: {
            presets: [
              [
                '@babel/env',
                {
                  targets: {
                    browsers: ['last 2 Chrome versions', 'Safari 10']
                  },
                  debug: true
                }
              ]
            ],
            plugins: [
              [
                '@babel/plugin-proposal-decorators',
                {
                  decoratorsBeforeExport: false
                }
              ],
              ['@babel/plugin-proposal-class-properties'],
              ['@babel/plugin-syntax-dynamic-import'],
              [
                '@babel/plugin-proposal-object-rest-spread',
                {
                  useBuiltIns: true
                }
              ]
            ]
          }
        }
      },
      {
        test: /\.template$/,
        use: ['text-loader']
      },
      {
        test: /\.html$/,
        use: [
          {
            loader: 'babel-loader'
          }
        ]
      },
      {
        test: /\.css$/,
        use: ['text-loader']
      },
      {
        test: /\.postcss$/,
        use: ['text-loader', 'postcss-loader']
      },
      {
        test: /\.(gif|jpe?g|png)$/,
        loader: 'url-loader?limit=25000',
        query: {
          limit: 10000,
          name: '[path][name].[hash:8].[ext]'
        }
      },
      {
        test: /\.(obj|mtl|tga|3ds|max|dae)$/,
        use: [
          {
            loader: 'file-loader',
            options: {}
          }
        ]
      },
      {
        test: /things-scene-components.import$/,
        use: [
          {
            loader: 'babel-loader'
          },
          {
            loader: 'things-scene-webpack-loader',
            options: {
              module_path: externModulesPath
            }
          }
        ]
      },
      {
        test: /things-scene-components-with-tools.import$/,
        use: [
          {
            loader: 'babel-loader'
          },
          {
            loader: 'things-scene-config-webpack-loader',
            options: {
              module_path: externModulesPath
            }
          }
        ]
      }
    ]
  },
  plugins: [
    new webpack.DefinePlugin({
      'process.env': {
        'NODE-ENV': JSON.stringify('production'),
        'APP-VERSION': JSON.stringify(NodePackage.version)
      }
    }),
    new WebpackCleanupPlugin(),
    new UglifyJsPlugin({
      test: /\-min\.js$/
    })
  ],
  devtool: 'cheap-module-source-map'
}
