const path = require('path');
const webpack = require('webpack');
const nodeExternals = require('webpack-node-externals');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const { rootDir, env, version, target, staticPath } = require('../helpers/const');

const assetTarget = 'default';

module.exports = {
  context: rootDir,

  name: 'server',
  mode: 'production',
  target: 'node',

  devtool: 'hidden-source-map',

  entry: [path.resolve(rootDir, './src/application/server/renderer.tsx')],

  output: {
    path: path.resolve(rootDir, './build/server'),
    filename: 'renderer.js',
    assetModuleFilename: '[contenthash:8][ext]',
    publicPath: `${staticPath}/${assetTarget}/`,
    library: {
      type: 'commonjs2'
    }
  },

  cache: {
    type: 'filesystem',
    buildDependencies: {
      configs: [__filename, path.resolve(rootDir, '.browserslistrc'), path.resolve(__dirname, '../../babel.config.js')]
    }
  },

  resolve: {
    extensions: ['.tsx', '.ts', '.js'],
    alias: {
      '@root': path.resolve(rootDir, './'),
      '@game': path.resolve(rootDir, './src'),
      '@server': path.resolve(rootDir, '../game-server/src'),
      '@api': path.resolve(rootDir, '../game-api/src'),
      '@map': path.resolve(rootDir, '../map/src'),
      '@core': path.resolve(rootDir, '../core/src'),

      '_service': path.resolve(rootDir, './src'),
      '_build': path.resolve(rootDir, './build'),

      'react': path.resolve(rootDir, './node_modules/react'),
      'react-dom': path.resolve(rootDir, './node_modules/react-dom'),
      '@loadable': path.resolve(rootDir, './node_modules/@loadable'),
      '@babel': path.resolve(rootDir, './node_modules/@babel'),
      'svg-sprite-loader': path.resolve(rootDir, './node_modules/svg-sprite-loader')
    }
  },

  // Игнорируем built-in модули такие как: path, fs, etc.
  // https://github.com/liady/webpack-node-externals
  externalsPresets: {
    node: true
  },

  externals: [nodeExternals()],

  optimization: {
    minimizer: [
      '...',
      new TerserPlugin({
        extractComments: false,
        terserOptions: {
          format: {
            comments: false
          }
        }
      })
    ]
  },

  module: {
    generator: {
      'asset': {
        publicPath: staticPath,
        outputPath: '../client/default'
      },
      'asset/resource': {
        publicPath: staticPath,
        outputPath: '../client/default'
      }
    },

    rules: [
      // JavaScript
      {
        test: /\.m?js$/,
        exclude: /node_modules/,
        type: 'javascript/auto'
      },
      // TypeScript
      {
        test: /\.tsx?$/,
        exclude: /node_modules/,
        use: [
          {
            loader: 'babel-loader',
            options: {
              presets: [
                [
                  '@babel/preset-env',
                  {
                    targets: {
                      node: 'current'
                    }
                  }
                ]
              ]
            }
          }
        ]
      },
      // CSS & Sass
      {
        test: /\.(?:s[ac]|c)ss$/,
        loader: 'null-loader'
      },
      // Images
      {
        test: /\.(?:ico|gif|png|jpg|jpeg|svg)$/i,
        type: 'asset/resource'
      }
    ]
  },

  plugins: [
    new webpack.EnvironmentPlugin({
      NODE_ENV: env,
      STATIC_PATH: staticPath,
      APP_VERSION: version,
      PLATFORM: 'server',
      TARGET: target,
      API_HOST: 'localhost:3001',
      SERVER_MSGPACK: 0,
    }),

    new MiniCssExtractPlugin({
      filename: '[name].[contenthash:8].css',
      chunkFilename: 'chunks/[id].[contenthash:8].css',
      ignoreOrder: true
    }),

    new webpack.optimize.LimitChunkCountPlugin({
      maxChunks: 1
    })
  ]
};
