WXL
3 天以前 9bce51f651aad297ef9eb6df832bfdaf1de05d84
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
/*
    MIT License http://www.opensource.org/licenses/mit-license.php
    Author Tobias Koppers @sokra, Zackary Jackson @ScriptedAlchemy, Marais Rossouw @maraisr
*/
 
"use strict";
 
const memoize = require("../util/memoize");
const ContainerEntryDependency = require("./ContainerEntryDependency");
const ContainerEntryModuleFactory = require("./ContainerEntryModuleFactory");
const ContainerExposedDependency = require("./ContainerExposedDependency");
const { parseOptions } = require("./options");
 
/** @typedef {import("../../declarations/plugins/container/ContainerPlugin").ContainerPluginOptions} ContainerPluginOptions */
/** @typedef {import("../Compiler")} Compiler */
/** @typedef {import("./ContainerEntryModule").ExposesList} ExposesList */
 
const getModuleFederationPlugin = memoize(() =>
    require("./ModuleFederationPlugin")
);
 
const PLUGIN_NAME = "ContainerPlugin";
 
class ContainerPlugin {
    /**
     * Creates an instance of ContainerPlugin.
     * @param {ContainerPluginOptions} options options
     */
    constructor(options) {
        /** @type {ContainerPluginOptions} */
        this.options = options;
    }
 
    /**
     * Applies the plugin by registering its hooks on the compiler.
     * @param {Compiler} compiler the compiler instance
     * @returns {void}
     */
    apply(compiler) {
        compiler.hooks.validate.tap(PLUGIN_NAME, () => {
            compiler.validate(
                () => require("../../schemas/plugins/container/ContainerPlugin.json"),
                this.options,
                {
                    name: "Container Plugin",
                    baseDataPath: "options"
                },
                (options) =>
                    require("../../schemas/plugins/container/ContainerPlugin.check")(
                        options
                    )
            );
        });
 
        const library = this.options.library || {
            type: "var",
            name: this.options.name
        };
 
        if (!compiler.options.output.enabledLibraryTypes.includes(library.type)) {
            compiler.options.output.enabledLibraryTypes.push(library.type);
        }
 
        const exposes = /** @type {ExposesList} */ (
            parseOptions(
                this.options.exposes,
                (item) => ({
                    import: Array.isArray(item) ? item : [item],
                    name: undefined
                }),
                (item) => ({
                    import: Array.isArray(item.import) ? item.import : [item.import],
                    name: item.name || undefined
                })
            )
        );
 
        const shareScope = this.options.shareScope || "default";
 
        compiler.hooks.make.tapAsync(PLUGIN_NAME, (compilation, callback) => {
            const hooks =
                getModuleFederationPlugin().getCompilationHooks(compilation);
            const dep = new ContainerEntryDependency(
                this.options.name,
                exposes,
                shareScope
            );
            dep.loc = { name: this.options.name };
            compilation.addEntry(
                compilation.options.context,
                dep,
                {
                    name: this.options.name,
                    filename: this.options.filename,
                    runtime: this.options.runtime,
                    library
                },
                (error) => {
                    if (error) return callback(error);
                    hooks.addContainerEntryDependency.call(dep);
                    callback();
                }
            );
        });
 
        compiler.hooks.thisCompilation.tap(
            PLUGIN_NAME,
            (compilation, { normalModuleFactory }) => {
                compilation.dependencyFactories.set(
                    ContainerEntryDependency,
                    new ContainerEntryModuleFactory()
                );
 
                compilation.dependencyFactories.set(
                    ContainerExposedDependency,
                    normalModuleFactory
                );
            }
        );
    }
}
 
module.exports = ContainerPlugin;