"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.runWebpackDevServer = exports.getWebpackCompiler = void 0;
const path_1 = __importDefault(require("path"));
const webpack_1 = __importDefault(require("webpack"));
const webpack_dev_server_1 = __importDefault(require("webpack-dev-server"));
const fork_ts_checker_webpack_plugin_1 = __importDefault(require("fork-ts-checker-webpack-plugin"));
const logger_1 = require("./logger");
const { ModuleFederationPlugin } = webpack_1.default.container;
const DEBUG_MODE = !!process.env.SALES_APP_DEBUG;
const getWebpackCompiler = (projectDir, mode = 'development') => {
    const config = {
        mode,
        bail: mode === 'production',
        entry: path_1.default.resolve(projectDir, './src/index.ts'),
        module: {
            rules: [
                {
                    test: /\.(js|jsx)$/,
                    exclude: /node_modules/,
                    loader: require.resolve('babel-loader'),
                    options: {
                        targets: 'defaults',
                        presets: ['@babel/preset-env', '@babel/preset-react'],
                    },
                },
                {
                    test: /\.(ts|tsx)$/,
                    exclude: /node_modules/,
                    loader: require.resolve('ts-loader'),
                    options: {
                        transpileOnly: true,
                        configFile: path_1.default.join(projectDir, 'tsconfig.json'),
                    },
                },
                {
                    test: /\.css$/i,
                    use: [
                        { loader: require.resolve('style-loader') },
                        { loader: require.resolve('css-loader') },
                    ],
                },
            ],
        },
        resolve: {
            extensions: ['.js', '.jsx', '.ts', '.tsx'],
        },
        output: {
            clean: true,
            path: path_1.default.resolve(projectDir, './dist'),
            filename: '[name]-[contenthash].js',
            chunkFilename: '[name]-[contenthash].js',
        },
        plugins: [
            new fork_ts_checker_webpack_plugin_1.default({
                async: false, // This will block the build on TypeScript errors
                typescript: {
                    configFile: path_1.default.join(projectDir, 'tsconfig.json'),
                },
            }),
            new ModuleFederationPlugin({
                name: 'SalesAppExtensions',
                filename: 'sales-app-extensions.js',
                exposes: {
                    './components': path_1.default.resolve(projectDir, './src/index.ts'),
                },
                shared: {
                    react: {
                        import: 'react',
                        shareKey: 'react',
                        shareScope: 'default',
                        singleton: true,
                        requiredVersion: '^18',
                    },
                    'react/jsx-runtime': {
                        singleton: true,
                        requiredVersion: '^18',
                    },
                    'react-dom': {
                        singleton: true,
                        requiredVersion: '^18',
                    },
                },
            }),
        ],
        infrastructureLogging: {
            level: DEBUG_MODE ? 'log' : 'error',
        },
        stats: (DEBUG_MODE ? 'normal' : 'errors-only'),
    };
    const compiler = (0, webpack_1.default)(config);
    return {
        compiler,
        config,
    };
};
exports.getWebpackCompiler = getWebpackCompiler;
const runWebpackDevServer = async (projectDir, port) => {
    const { compiler } = (0, exports.getWebpackCompiler)(projectDir);
    const server = new webpack_dev_server_1.default({
        port,
        hot: false,
        liveReload: true,
        client: {
            overlay: DEBUG_MODE,
        },
    }, compiler);
    logger_1.logger.log('Starting webpack dev server...');
    await server.start();
};
exports.runWebpackDevServer = runWebpackDevServer;
//# sourceMappingURL=webpack.js.map