const path = require('path');

const {CleanWebpackPlugin} = require('clean-webpack-plugin');
const webpack = require('webpack');

const CONTEXT = path.resolve(__dirname);
const SCSS_DIR = path.resolve(CONTEXT, 'scss');
const PROJECT_DIR = path.resolve(CONTEXT, 'src');
const OUTPUT_DIR = path.resolve(CONTEXT, 'lib');

const IS_PRODUCTION = (process.env['NODE_ENV'] === 'production');

const config = {
  mode: 'development',
  devtool: 'source-map',
  context: CONTEXT,
  entry: {
    index: './src/index.js',
  },
  output: {
    path: OUTPUT_DIR,
    filename: '[name].js',
    library: 'homehub_tradfri',
    libraryTarget: 'umd',
    umdNamedDefine: true,
  },
  module: {
    rules: [
      {
        test: /\.js?$/,
        include: PROJECT_DIR,
        use: [
          'babel-loader',
        ],
      },
      {
        test: /\.scss?$/,
        include: PROJECT_DIR,
        use: [
          'style-loader',
          'css-loader',
          'sass-loader',
        ],
      },
      {
        test: /\.css?$/,
        use: [
          'style-loader',
          'css-loader',
        ],
      },
      {
        test: /\.(svg)$/,
        include: PROJECT_DIR,
        use: [
          {
            loader: 'babel-loader',
          },
          {
            loader: 'react-svg-loader',
            options: {
              jsx: true,
            },
          },
        ],
      },
      {
        test: /\.(png)$/,
        include: PROJECT_DIR,
        use: [
          {
            loader: 'file-loader',
            options: {
              name: '[name].[ext]',
            },
          },
        ],
      },
      {
        test: /\.scss$/,
        include: SCSS_DIR,
        use: [
          'null-loader',
        ],
      },
    ],
  },
  resolve: {
    alias: {
      'scss': SCSS_DIR,
      'src': PROJECT_DIR,
    },
  },
  externals: {
    '@bthlabs/homehub-components': '@bthlabs/homehub-components',
    '@bthlabs/homehub-core': '@bthlabs/homehub-core',
    '@bthlabs/homehub-icons': '@bthlabs/homehub-icons',
    react: 'react',
    'react-bootstrap': 'react-bootstrap',
    'react-dom': 'react-dom',
  },
  plugins: [
    new CleanWebpackPlugin(),
    new webpack.DefinePlugin({
      PRODUCTION: IS_PRODUCTION,
    }),
  ],
};

module.exports = config;
