WXL
4 天以前 3bd962a6d7f61239c020e2dbbeb7341e5b842dd1
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
    MIT License http://www.opensource.org/licenses/mit-license.php
    Author Tobias Koppers @sokra
*/
 
"use strict";
 
const RuntimeGlobals = require("../RuntimeGlobals");
const JavascriptModulesPlugin = require("../javascript/JavascriptModulesPlugin");
 
/** @typedef {import("webpack-sources").Source} Source */
/** @typedef {import("../../declarations/WebpackOptions").LibraryOptions} LibraryOptions */
/** @typedef {import("../../declarations/WebpackOptions").LibraryType} LibraryType */
/** @typedef {import("../Chunk")} Chunk */
/** @typedef {import("../ChunkGraph")} ChunkGraph */
/** @typedef {import("../Compilation")} Compilation */
/** @typedef {import("../Compilation").ChunkHashContext} ChunkHashContext */
/** @typedef {import("../Compiler")} Compiler */
/** @typedef {import("../Module")} Module */
/** @typedef {import("../Module").RuntimeRequirements} RuntimeRequirements */
/** @typedef {import("../javascript/JavascriptModulesPlugin").RenderContext} RenderContext */
/** @typedef {import("../javascript/JavascriptModulesPlugin").StartupRenderContext} StartupRenderContext */
/** @typedef {import("../javascript/JavascriptModulesPlugin").ModuleRenderContext} ModuleRenderContext */
/** @typedef {import("../util/Hash")} Hash */
 
const COMMON_LIBRARY_NAME_MESSAGE =
    "Common configuration options that specific library names are 'output.library[.name]', 'entry.xyz.library[.name]', 'ModuleFederationPlugin.name' and 'ModuleFederationPlugin.library[.name]'.";
 
/**
 * Defines the library context type used by this module.
 * @template T
 * @typedef {object} LibraryContext
 * @property {Compilation} compilation
 * @property {ChunkGraph} chunkGraph
 * @property {T} options
 */
 
/**
 * Defines the abstract library plugin options type used by this module.
 * @typedef {object} AbstractLibraryPluginOptions
 * @property {string} pluginName name of the plugin
 * @property {LibraryType} type used library type
 */
 
/**
 * Represents AbstractLibraryPlugin.
 * @template T
 */
class AbstractLibraryPlugin {
    /**
     * Creates an instance of AbstractLibraryPlugin.
     * @param {AbstractLibraryPluginOptions} options options
     */
    constructor({ pluginName, type }) {
        /** @type {AbstractLibraryPluginOptions["pluginName"]} */
        this._pluginName = pluginName;
        /** @type {AbstractLibraryPluginOptions["type"]} */
        this._type = type;
        /** @type {WeakMap<LibraryOptions, T>} */
        this._parseCache = new WeakMap();
    }
 
    /**
     * Applies the plugin by registering its hooks on the compiler.
     * @param {Compiler} compiler the compiler instance
     * @returns {void}
     */
    apply(compiler) {
        const { _pluginName } = this;
        compiler.hooks.thisCompilation.tap(_pluginName, (compilation) => {
            compilation.hooks.finishModules.tap(
                { name: _pluginName, stage: 10 },
                () => {
                    for (const [
                        name,
                        {
                            dependencies: deps,
                            options: { library }
                        }
                    ] of compilation.entries) {
                        const options = this._parseOptionsCached(
                            library !== undefined
                                ? library
                                : compilation.outputOptions.library
                        );
                        if (options !== false) {
                            const dep = deps[deps.length - 1];
                            if (dep) {
                                const module = compilation.moduleGraph.getModule(dep);
                                if (module) {
                                    this.finishEntryModule(module, name, {
                                        options,
                                        compilation,
                                        chunkGraph: compilation.chunkGraph
                                    });
                                }
                            }
                        }
                    }
                }
            );
 
            /**
             * Gets options for chunk.
             * @param {Chunk} chunk chunk
             * @returns {T | false} options for the chunk
             */
            const getOptionsForChunk = (chunk) => {
                if (compilation.chunkGraph.getNumberOfEntryModules(chunk) === 0) {
                    return false;
                }
                const options = chunk.getEntryOptions();
                const library = options && options.library;
                return this._parseOptionsCached(
                    library !== undefined ? library : compilation.outputOptions.library
                );
            };
 
            if (
                this.render !== AbstractLibraryPlugin.prototype.render ||
                this.runtimeRequirements !==
                    AbstractLibraryPlugin.prototype.runtimeRequirements
            ) {
                compilation.hooks.additionalChunkRuntimeRequirements.tap(
                    _pluginName,
                    (chunk, set, { chunkGraph }) => {
                        const options = getOptionsForChunk(chunk);
                        if (options !== false) {
                            this.runtimeRequirements(chunk, set, {
                                options,
                                compilation,
                                chunkGraph
                            });
                        }
                    }
                );
            }
 
            const hooks = JavascriptModulesPlugin.getCompilationHooks(compilation);
 
            if (this.render !== AbstractLibraryPlugin.prototype.render) {
                hooks.render.tap(_pluginName, (source, renderContext) => {
                    const options = getOptionsForChunk(renderContext.chunk);
                    if (options === false) return source;
                    return this.render(source, renderContext, {
                        options,
                        compilation,
                        chunkGraph: compilation.chunkGraph
                    });
                });
            }
 
            if (
                this.embedInRuntimeBailout !==
                AbstractLibraryPlugin.prototype.embedInRuntimeBailout
            ) {
                hooks.embedInRuntimeBailout.tap(
                    _pluginName,
                    (module, renderContext) => {
                        const options = getOptionsForChunk(renderContext.chunk);
                        if (options === false) return;
                        return this.embedInRuntimeBailout(module, renderContext, {
                            options,
                            compilation,
                            chunkGraph: compilation.chunkGraph
                        });
                    }
                );
            }
 
            if (
                this.strictRuntimeBailout !==
                AbstractLibraryPlugin.prototype.strictRuntimeBailout
            ) {
                hooks.strictRuntimeBailout.tap(_pluginName, (renderContext) => {
                    const options = getOptionsForChunk(renderContext.chunk);
                    if (options === false) return;
                    return this.strictRuntimeBailout(renderContext, {
                        options,
                        compilation,
                        chunkGraph: compilation.chunkGraph
                    });
                });
            }
 
            if (
                this.renderModuleContent !==
                AbstractLibraryPlugin.prototype.renderModuleContent
            ) {
                hooks.renderModuleContent.tap(
                    _pluginName,
                    (source, module, renderContext) =>
                        this.renderModuleContent(source, module, renderContext, {
                            compilation,
                            chunkGraph: compilation.chunkGraph
                        })
                );
            }
 
            if (
                this.renderStartup !== AbstractLibraryPlugin.prototype.renderStartup
            ) {
                hooks.renderStartup.tap(
                    _pluginName,
                    (source, module, renderContext) => {
                        const options = getOptionsForChunk(renderContext.chunk);
                        if (options === false) return source;
                        return this.renderStartup(source, module, renderContext, {
                            options,
                            compilation,
                            chunkGraph: compilation.chunkGraph
                        });
                    }
                );
            }
 
            hooks.chunkHash.tap(_pluginName, (chunk, hash, context) => {
                const options = getOptionsForChunk(chunk);
                if (options === false) return;
                this.chunkHash(chunk, hash, context, {
                    options,
                    compilation,
                    chunkGraph: compilation.chunkGraph
                });
            });
        });
    }
 
    /**
     * Parse options cached.
     * @param {LibraryOptions=} library normalized library option
     * @returns {T | false} preprocess as needed by overriding
     */
    _parseOptionsCached(library) {
        if (!library) return false;
        if (library.type !== this._type) return false;
        const cacheEntry = this._parseCache.get(library);
        if (cacheEntry !== undefined) return cacheEntry;
        const result = this.parseOptions(library);
        this._parseCache.set(library, result);
        return result;
    }
 
    /* istanbul ignore next */
    /**
     * Returns preprocess as needed by overriding.
     * @abstract
     * @param {LibraryOptions} library normalized library option
     * @returns {T} preprocess as needed by overriding
     */
    parseOptions(library) {
        const AbstractMethodError = require("../AbstractMethodError");
 
        throw new AbstractMethodError();
    }
 
    /**
     * Finish entry module.
     * @param {Module} module the exporting entry module
     * @param {string} entryName the name of the entrypoint
     * @param {LibraryContext<T>} libraryContext context
     * @returns {void}
     */
    finishEntryModule(module, entryName, libraryContext) {}
 
    /**
     * Embed in runtime bailout.
     * @param {Module} module the exporting entry module
     * @param {RenderContext} renderContext render context
     * @param {LibraryContext<T>} libraryContext context
     * @returns {string | undefined} bailout reason
     */
    embedInRuntimeBailout(module, renderContext, libraryContext) {
        return undefined;
    }
 
    /**
     * Strict runtime bailout.
     * @param {RenderContext} renderContext render context
     * @param {LibraryContext<T>} libraryContext context
     * @returns {string | undefined} bailout reason
     */
    strictRuntimeBailout(renderContext, libraryContext) {
        return undefined;
    }
 
    /**
     * Processes the provided chunk.
     * @param {Chunk} chunk the chunk
     * @param {RuntimeRequirements} set runtime requirements
     * @param {LibraryContext<T>} libraryContext context
     * @returns {void}
     */
    runtimeRequirements(chunk, set, libraryContext) {
        if (this.render !== AbstractLibraryPlugin.prototype.render) {
            set.add(RuntimeGlobals.returnExportsFromRuntime);
        }
    }
 
    /**
     * Returns source with library export.
     * @param {Source} source source
     * @param {RenderContext} renderContext render context
     * @param {LibraryContext<T>} libraryContext context
     * @returns {Source} source with library export
     */
    render(source, renderContext, libraryContext) {
        return source;
    }
 
    /**
     * Renders source with library export.
     * @param {Source} source source
     * @param {Module} module module
     * @param {StartupRenderContext} renderContext render context
     * @param {LibraryContext<T>} libraryContext context
     * @returns {Source} source with library export
     */
    renderStartup(source, module, renderContext, libraryContext) {
        return source;
    }
 
    /**
     * Renders module content.
     * @param {Source} source source
     * @param {Module} module module
     * @param {ModuleRenderContext} renderContext render context
     * @param {Omit<LibraryContext<T>, "options">} libraryContext context
     * @returns {Source} source with library export
     */
    renderModuleContent(source, module, renderContext, libraryContext) {
        return source;
    }
 
    /**
     * Processes the provided chunk.
     * @param {Chunk} chunk the chunk
     * @param {Hash} hash hash
     * @param {ChunkHashContext} chunkHashContext chunk hash context
     * @param {LibraryContext<T>} libraryContext context
     * @returns {void}
     */
    chunkHash(chunk, hash, chunkHashContext, libraryContext) {
        const options = this._parseOptionsCached(
            libraryContext.compilation.outputOptions.library
        );
        hash.update(this._pluginName);
        hash.update(JSON.stringify(options));
    }
}
 
AbstractLibraryPlugin.COMMON_LIBRARY_NAME_MESSAGE = COMMON_LIBRARY_NAME_MESSAGE;
 
module.exports = AbstractLibraryPlugin;