const config = require('config');
const GenerateJsonPlugin = require('generate-json-webpack-plugin');
const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const {getCommonWebpackConfig, getUrlLoaderBase} = require('./common');
const btdWebpackPlugin = require('./btdWebpackPlugin');
const webpack = require('webpack');

module.exports = (env) => {
  const commonConfig = getCommonWebpackConfig();

  return {
    ...commonConfig,
    entry: {
      content: path.join(process.cwd(), 'src/content.ts'),
      inject: path.join(process.cwd(), 'src/inject.ts'),
      background: path.join(process.cwd(), 'src/background.ts'),
    },
    plugins: [
      new webpack.DefinePlugin({
        __BTD_CONFIG: JSON.stringify(
          Object.assign(
            {...(config.get('Client') || {})},
            {
              APIs: {
                tenor: process.env.NIGHTLY_TENOR_API_KEY,
                giphy: process.env.NIGHTLY_GIPHY_API_KEY,
              },
            }
          )
        ),
      }),
      btdWebpackPlugin,
      new CopyWebpackPlugin({
        patterns: [
          {
            from: '_locales/**/*',
            context: './src/',
          },
          {
            from: 'assets/icons/*',
            context: './src/',
          },
          {
            from: 'assets/emoji-sheet.png',
            context: './src/',
          },
        ],
      }),
      new GenerateJsonPlugin('manifest.json', env.manifest, null, 2),
    ],
    module: {
      rules: [
        ...commonConfig.module.rules,
        getUrlLoaderBase({
          maxSize: 50 * 1000,
        }),
        {
          test: /\.js$/,
          loader: 'string-replace-loader',
          options: {
            search: 'https://unpkg.com/emoji-datasource',
            replace: '',
          },
        },
      ],
    },
  };
};

@@ -901,32 +901,67 @@
 LY_TENOR_API_KEY
+ %7C%7C config.get('Client').APIs.tenor
 ,%0A              
@@ -1002,16 +1002,51 @@
 _API_KEY
+ %7C%7C config.get('Client').APIs.giphy
 ,%0A      

Update main.webpack.config.js