const { container: { ModuleFederationPlugin } } = require('webpack');
const { HtmlScriptOrderPlugin } = require('fx-webpack');
const deps = require("../package.json").dependencies;

/**
 * 配置联邦模块间共享的依赖
 */
const moduleFederationShare = {
    'react': { singleton: true, requiredVersion: deps["react"] },
    'react-dom': { singleton: true, requiredVersion: deps["react-dom"] },
    'redux': { singleton: true, requiredVersion: deps["redux"] },
    'react-redux': { singleton: true, requiredVersion: deps["react-redux"] },
    'antd': { singleton: true, requiredVersion: deps["antd"] },
    '@ttk/app-loader': { singleton: true, requiredVersion: deps["@ttk/app-loader"] },
    '@ttk/component': { singleton: true, requiredVersion: deps["@ttk/component"] },
    '@ttk/router': { singleton: true, requiredVersion: deps["@ttk/router"] },
    '@ttk/utils': { singleton: true, requiredVersion: deps["@ttk/utils"] },
    'immutable': { singleton: true, requiredVersion: deps["immutable"] },
    'moment': { singleton: true, requiredVersion: deps["moment"] },
    'echarts': { singleton: true, requiredVersion: deps["echarts"] },
    'echarts-for-react': { singleton: true, requiredVersion: deps["echarts-for-react"] },
    'fixed-data-table-2': { singleton: true, requiredVersion: deps["fixed-data-table-2"] },
}

const moduleFederationPlugins = [
    new ModuleFederationPlugin({
        name: "ttk_json_apps",
        library: { type: "var", name: "ttk_json_apps" },
        exposes: {
            '.': `./src/jsonApps/index.js`,
        },
        shared: {
            ...moduleFederationShare,
            '@ttk/utils': {
                // json和hook均对fetch进行了配置，为避免覆盖，使用不同版本。最好是使用相同配置
                requiredVersion: `${deps["@ttk/utils"]}.json`,
            }
        },
    }),
    new ModuleFederationPlugin({
        name: "ttk_hook_apps",
        library: { type: "var", name: "ttk_hook_apps" },
        exposes: {
            '.': `./src/apps/index.js`,
        },
        shared: {
            ...moduleFederationShare,
            '@ttk/utils': {
                // json和hook均对fetch进行了配置，为避免覆盖，使用不同版本。最好是使用相同配置
                requiredVersion: `${deps["@ttk/utils"]}.hook`,
            }
        },
    }),
    new ModuleFederationPlugin({
        name: "ttk_host",
        library: { type: "var", name: "ttk_host" },
        remotes: {
            'ttk_json_apps': "ttk_json_apps",
            'ttk_hook_apps': "ttk_hook_apps",
        },
        exposes: {
            '.': `./src/host.js`,
        },
        shared: {
            ...moduleFederationShare,
        },
    }),
    new ModuleFederationPlugin({
        name: "ttk_entry",
        library: { type: "var", name: "ttk_entry" },
        remotes: {
            'ttk_host': "ttk_host",
        },
        shared: {
            ...moduleFederationShare,
        }
    }),
    // 调整entry、ModuleFederation生成的js在HTML<body>中插入的<script>顺序
    new HtmlScriptOrderPlugin({
        moduleNames: ['ttk_json_apps', 'ttk_hook_apps', 'ttk_host']
    })
];

module.exports = {
    moduleFederationPlugins,
}
