"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = require("path");
const webpack_plugin_1 = __importDefault(require("@loadable/webpack-plugin"));
const pnp_webpack_plugin_1 = __importDefault(require("pnp-webpack-plugin"));
const terser_webpack_plugin_1 = __importDefault(require("terser-webpack-plugin"));
const webpack_blocks_1 = require("webpack-blocks");
const micro_core_1 = require("@vtex/micro-core");
const aliases_1 = require("../aliases");
const cacheGroups_1 = require("./modules/cacheGroups");
const webnew_1 = require("./webnew");
const webold_1 = require("./webold");
const entriesFromPages = async (project) => {
    const files = await project.root.getFiles('pages');
    return files.reduce((acc, path) => {
        if (path.endsWith('.tsx')) {
            const name = path_1.basename(path, '.tsx');
            acc[name] = path.replace(project.rootPath, '.');
        }
        return acc;
    }, {});
};
class Bundle extends micro_core_1.BundlePlugin {
    constructor() {
        super(...arguments);
        this.getWebpackConfig = async (config, target) => {
            const entrypoints = await entriesFromPages(this.project);
            const block = [
                webpack_blocks_1.entryPoint(entrypoints),
                webpack_blocks_1.addPlugins([
                    new webpack_plugin_1.default({
                        outputAsset: false,
                        writeToDisk: false,
                    }),
                ]),
                webpack_blocks_1.resolve({
                    alias: aliases_1.aliases.reduce((acc, packageName) => {
                        acc[packageName] = require.resolve(packageName);
                        return acc;
                    }, {}),
                    plugins: [pnp_webpack_plugin_1.default],
                }),
                cacheGroups_1.cacheGroup(micro_core_1.pagesRuntimeName, /\/react\/|\/react-dom\/|\/@loadable\//),
                cacheGroups_1.cacheGroup(micro_core_1.pagesFrameworkName, /\/micro-react\/components\//),
                webpack_blocks_1.optimization({
                    splitChunks: {
                        maxInitialRequests: 30,
                        maxAsyncRequests: 10,
                    },
                }),
                webpack_blocks_1.customConfig({
                    name: target,
                    target: 'web',
                    bail: true,
                    node: false,
                    profile: true,
                    resolveLoader: {
                        plugins: [pnp_webpack_plugin_1.default.moduleLoader(module)],
                    },
                }),
                webpack_blocks_1.env('production', [
                    webpack_blocks_1.optimization({
                        noEmitOnErrors: true,
                        moduleIds: 'size',
                        chunkIds: 'total-size',
                        nodeEnv: 'production',
                        removeAvailableModules: true,
                        removeEmptyChunks: true,
                        mergeDuplicateChunks: true,
                        flagIncludedChunks: true,
                        providedExports: true,
                        usedExports: true,
                        concatenateModules: true,
                        sideEffects: true,
                        portableRecords: false,
                        minimizer: [
                            new terser_webpack_plugin_1.default({
                                extractComments: true,
                            }),
                        ],
                    }),
                    webpack_blocks_1.performance({
                        hints: 'warning',
                    }),
                ]),
            ];
            return webpack_blocks_1.group([
                config,
                ...block,
                webpack_blocks_1.match(['*.tsx', '*.ts'], [target === 'webnew' ? webnew_1.webnewBabel : webold_1.weboldBabel]),
            ]);
        };
    }
}
exports.default = Bundle;
