text: var webpack = require('webpack'),
    path = require('path'),
    fileSystem = require('fs'),
    env = require('./utils/env'),
    { CleanWebpackPlugin } = require('clean-webpack-plugin'),
    CopyWebpackPlugin = require('copy-webpack-plugin'),
    HtmlWebpackPlugin = require('html-webpack-plugin'),
    WriteFilePlugin = require('write-file-webpack-plugin');
  
  // load the secrets
  var alias = {
    'react-dom': '@hot-loader/react-dom',
  };
  
  var secretsPath = path.join(__dirname, 'secrets.' + env.NODE_ENV + '.js');
  
  var fileExtensions = [
    'jpg',
    'jpeg',
    'png',
    'gif',
    'eot',
    'otf',
    'svg',
    'ttf',
    'woff',
    'woff2',
  ];
  
  if (fileSystem.existsSync(secretsPath)) {
    alias['secrets'] = secretsPath;
  }
  
  var options = {
    mode: process.env.NODE_ENV || 'development',
    entry: {
      options: path.join(__dirname, 'src', 'pages', 'Options', 'index.jsx'),
      sidebar: path.join(__dirname, 'src', 'pages', 'Sidebar', 'index.jsx'),
      background: path.join(__dirname, 'src', 'pages', 'Background', 'index.js'),
      contentScript: path.join(__dirname, 'src', 'pages', 'Content', 'index.jsx'),
    },
    chromeExtensionBoilerplate: {
      notHotReload: ['contentScript'],
    },
    output: {
      path: path.join(__dirname, 'build'),
      filename: '[name].bundle.js',
    },
    module: {
      rules: [
        {
          test: /\.css$/,
          loader: 'style-loader!css-loader',
          exclude: /node_modules/,
        },
        {
          test: new RegExp('.(' + fileExtensions.join('|') + ')$'),
          loader: 'file-loader?name=[name].[ext]',
          exclude: /node_modules/,
        },
        {
          test: /\.html$/,
          loader: 'html-loader',
          exclude: /node_modules/,
        },
        {
          test: /\.(js|jsx)$/,
          loader: 'babel-loader',
          exclude: /node_modules/,
        },
      ],
    },
    resolve: {
      alias: alias,
      extensions: fileExtensions
        .map((extension) => '.' + extension)
        .concat(['.jsx', '.js', '.css']),
    },
    plugins: [
      new webpack.ProgressPlugin(),
      // clean the build folder
      new CleanWebpackPlugin({
        verbose: true,
        // cleanStaleWebpackAssets: false,
      }),
      // expose and write the allowed env vars on the compiled bundle
      new webpack.EnvironmentPlugin(['NODE_ENV']),
      new CopyWebpackPlugin(
        [
          {
            from: 'src/manifest.json',
            to: path.join(__dirname, 'build'),
            force: true,
            transform: function(content, path) {
              // generates the manifest file using the package.json informations
              return Buffer.from(
                JSON.stringify({
                  description: process.env.npm_package_description,
                  version: process.env.npm_package_version,
                  ...JSON.parse(content.toString()),
                })
              );
            },
          },
        ],
        {
          logLevel: 'info',
          copyUnmodified: true,
        }
      ),
      new CopyWebpackPlugin(
        [
          {
            from: 'src/pages/Content/content.styles.css',
            to: path.join(__dirname, 'build'),
            force: true,
          },
        ],
        {
          logLevel: 'info',
          copyUnmodified: true,
        }
      ),
      new HtmlWebpackPlugin({
        template: path.join(__dirname, 'src', 'pages', 'Options', 'index.html'),
        filename: 'options.html',
        chunks: ['options'],
      }),
      new HtmlWebpackPlugin({
        template: path.join(__dirname, 'src', 'pages', 'Sidebar', 'index.html'),
        filename: 'sidebar.html',
        chunks: ['sidebar'],
      }),
      new HtmlWebpackPlugin({
        template: path.join(
          __dirname,
          'src',
          'pages',
          'Background',
          'index.html'
        ),
        filename: 'background.html',
        chunks: ['background'],
      }),
      new WriteFilePlugin(),
    ],
  };
  
  if (env.NODE_ENV === 'development') {
    options.devtool = 'cheap-module-eval-source-map';
  }
  
  module.exports = options;
  