const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); const MiniCssExtractPlugin = require('mini-css-extract-plugin'); const CopyWebpackPlugin = require('copy-webpack-plugin'); const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin'); const FaviconsWebpackPlugin = require('favicons-webpack-plugin'); module.exports = (env, argv) => { const isProduction = argv.mode === 'production'; return { entry: './src/index.tsx', output: { path: path.resolve(__dirname, 'dist'), filename: 'bundle.js', clean: true, // Dev needs '/' for HMR WebSocket; prod uses '' for relative paths through proxies publicPath: isProduction ? '' : '/', }, resolve: { extensions: ['.ts', '.tsx', '.js'], }, module: { rules: [ { test: /\.tsx?$/, exclude: /node_modules/, use: { loader: 'babel-loader', options: { presets: [ '@babel/preset-env', ['@babel/preset-react', { runtime: 'automatic' }], '@babel/preset-typescript', ], plugins: isProduction ? [] : ['react-refresh/babel'], }, }, }, { test: /\.css$/, use: [ isProduction ? MiniCssExtractPlugin.loader : 'style-loader', 'css-loader', 'postcss-loader', ], }, ], }, plugins: [ new HtmlWebpackPlugin({ template: './src/index.html', }), new CopyWebpackPlugin({ patterns: [{ from: 'public', noErrorOnMissing: true }], }), new FaviconsWebpackPlugin({ logo: './public/favicon.svg', favicons: { background: '#0c0a09', icons: { favicons: true, android: false, appleIcon: false, appleStartup: false, yandex: false, windows: false, }, }, }), ...(isProduction ? [new MiniCssExtractPlugin()] : [new ReactRefreshWebpackPlugin()]), ], devServer: { host: '0.0.0.0', port: 3001, allowedHosts: 'all', client: { webSocketURL: 'auto://0.0.0.0:0/ws', }, historyApiFallback: { index: '/index.html', }, hot: true, liveReload: true, static: { directory: path.resolve(__dirname, 'public'), watch: { ignored: ['**/assets/**'], }, }, proxy: [ { context: ['/api', '/s'], target: process.env.API_PROXY_TARGET || 'http://localhost:8001', }, { context: ['/pb'], target: process.env.PB_PROXY_TARGET || 'http://localhost:8090', pathRewrite: { '^/pb': '' }, }, ], }, }; };