const { merge } = require('webpack-merge')
const common = require('./webpack.common.js')
const path = require('path')

// Check if HTTPS mode is enabled via environment variable
const useHttps = process.env.HTTPS === 'true'

module.exports = merge(common, {
    mode: 'development',
    devtool: 'eval-source-map',
    devServer: {
        allowedHosts: 'all',
        hot: true,
        open: false,
        port: 8080,
        // Enable HTTPS with self-signed certificate when HTTPS=true
        ...(useHttps && { server: 'https' }),
        static: {
            directory: path.join(__dirname, './build'),
            watch: true,
        },
        watchFiles: {
            paths: ['src/**/*', '../../build/**/*'],
            options: {
                usePolling: false,
                ignored: /node_modules/,
            },
        },
        client: {
            progress: true,
            overlay: {
                errors: true,
                warnings: false,
            },
        },
        devMiddleware: {
            writeToDisk: true,
        },
    },
})

console.log(module.exports);