啊鑫
7 天以前 fca192d3c38c5dcfbb6ace8bc71d6078f6a079b2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
    MIT License http://www.opensource.org/licenses/mit-license.php
    Author Tobias Koppers @sokra and Zackary Jackson @ScriptedAlchemy
*/
 
"use strict";
 
const { SyncHook } = require("tapable");
const isValidExternalsType = require("../../schemas/plugins/container/ExternalsType.check.js");
const Compilation = require("../Compilation");
const SharePlugin = require("../sharing/SharePlugin");
const createSchemaValidation = require("../util/create-schema-validation");
const ContainerPlugin = require("./ContainerPlugin");
const ContainerReferencePlugin = require("./ContainerReferencePlugin");
const HoistContainerReferences = require("./HoistContainerReferencesPlugin");
 
/** @typedef {import("../../declarations/plugins/container/ModuleFederationPlugin").ExternalsType} ExternalsType */
/** @typedef {import("../../declarations/plugins/container/ModuleFederationPlugin").ModuleFederationPluginOptions} ModuleFederationPluginOptions */
/** @typedef {import("../../declarations/plugins/container/ModuleFederationPlugin").Shared} Shared */
/** @typedef {import("../Compiler")} Compiler */
/** @typedef {import("../Dependency")} Dependency */
 
/**
 * @typedef {object} CompilationHooks
 * @property {SyncHook<Dependency>} addContainerEntryDependency
 * @property {SyncHook<Dependency>} addFederationRuntimeDependency
 */
 
const validate = createSchemaValidation(
    require("../../schemas/plugins/container/ModuleFederationPlugin.check.js"),
    () => require("../../schemas/plugins/container/ModuleFederationPlugin.json"),
    {
        name: "Module Federation Plugin",
        baseDataPath: "options"
    }
);
 
/** @type {WeakMap<Compilation, CompilationHooks>} */
const compilationHooksMap = new WeakMap();
const PLUGIN_NAME = "ModuleFederationPlugin";
 
class ModuleFederationPlugin {
    /**
     * @param {ModuleFederationPluginOptions} options options
     */
    constructor(options) {
        validate(options);
 
        this._options = options;
    }
 
    /**
     * Get the compilation hooks associated with this plugin.
     * @param {Compilation} compilation The compilation instance.
     * @returns {CompilationHooks} The hooks for the compilation.
     */
    static getCompilationHooks(compilation) {
        if (!(compilation instanceof Compilation)) {
            throw new TypeError(
                "The 'compilation' argument must be an instance of Compilation"
            );
        }
        let hooks = compilationHooksMap.get(compilation);
        if (!hooks) {
            hooks = {
                addContainerEntryDependency: new SyncHook(["dependency"]),
                addFederationRuntimeDependency: new SyncHook(["dependency"])
            };
            compilationHooksMap.set(compilation, hooks);
        }
        return hooks;
    }
 
    /**
     * Apply the plugin
     * @param {Compiler} compiler the compiler instance
     * @returns {void}
     */
    apply(compiler) {
        const { _options: options } = this;
        const library = options.library || { type: "var", name: options.name };
        const remoteType =
            options.remoteType ||
            (options.library && isValidExternalsType(options.library.type)
                ? /** @type {ExternalsType} */ (options.library.type)
                : "script");
        if (
            library &&
            !compiler.options.output.enabledLibraryTypes.includes(library.type)
        ) {
            compiler.options.output.enabledLibraryTypes.push(library.type);
        }
        compiler.hooks.afterPlugins.tap(PLUGIN_NAME, () => {
            if (
                options.exposes &&
                (Array.isArray(options.exposes)
                    ? options.exposes.length > 0
                    : Object.keys(options.exposes).length > 0)
            ) {
                new ContainerPlugin({
                    name: /** @type {string} */ (options.name),
                    library,
                    filename: options.filename,
                    runtime: options.runtime,
                    shareScope: options.shareScope,
                    exposes: options.exposes
                }).apply(compiler);
            }
            if (
                options.remotes &&
                (Array.isArray(options.remotes)
                    ? options.remotes.length > 0
                    : Object.keys(options.remotes).length > 0)
            ) {
                new ContainerReferencePlugin({
                    remoteType,
                    shareScope: options.shareScope,
                    remotes: options.remotes
                }).apply(compiler);
            }
            if (options.shared) {
                new SharePlugin({
                    shared: options.shared,
                    shareScope: options.shareScope
                }).apply(compiler);
            }
            new HoistContainerReferences().apply(compiler);
        });
    }
}
 
module.exports = ModuleFederationPlugin;