WXL
4 天以前 2cc85c64f1c64a2dbaeae276a3e2ca8420de76b7
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*
    MIT License http://www.opensource.org/licenses/mit-license.php
    Author Tobias Koppers @sokra
*/
 
"use strict";
 
const { ConcatSource } = require("webpack-sources");
const { UsageState } = require("../ExportsInfo");
const ExternalModule = require("../ExternalModule");
const RuntimeGlobals = require("../RuntimeGlobals");
const Template = require("../Template");
const HarmonyExportImportedSpecifierDependency = require("../dependencies/HarmonyExportImportedSpecifierDependency");
const ConcatenatedModule = require("../optimize/ConcatenatedModule");
const { propertyAccess } = require("../util/property");
const { getEntryRuntime, getRuntimeKey } = require("../util/runtime");
const AbstractLibraryPlugin = require("./AbstractLibraryPlugin");
 
/** @typedef {import("webpack-sources").Source} Source */
/** @typedef {import("../../declarations/WebpackOptions").LibraryOptions} LibraryOptions */
/** @typedef {import("../../declarations/WebpackOptions").LibraryType} LibraryType */
/** @typedef {import("../../declarations/WebpackOptions").LibraryExport} LibraryExport */
/** @typedef {import("../Chunk")} Chunk */
/** @typedef {import("../Compiler")} Compiler */
/** @typedef {import("../ModuleGraph")} ModuleGraph */
/** @typedef {import("../Module")} Module */
/** @typedef {import("../Module").BuildMeta} BuildMeta */
/** @typedef {import("../Module").RuntimeRequirements} RuntimeRequirements */
/** @typedef {import("../javascript/JavascriptModulesPlugin").StartupRenderContext} StartupRenderContext */
/** @typedef {import("../javascript/JavascriptModulesPlugin").ModuleRenderContext} ModuleRenderContext */
/** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
/** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
 
/**
 * Defines the shared type used by this module.
 * @template T
 * @typedef {import("./AbstractLibraryPlugin").LibraryContext<T>} LibraryContext<T>
 */
 
/**
 * Defines the module library plugin options type used by this module.
 * @typedef {object} ModuleLibraryPluginOptions
 * @property {LibraryType} type
 */
 
/**
 * Defines the module library plugin parsed type used by this module.
 * @typedef {object} ModuleLibraryPluginParsed
 * @property {string} name
 * @property {LibraryExport=} export
 */
 
const PLUGIN_NAME = "ModuleLibraryPlugin";
 
/**
 * Represents the module library plugin runtime component.
 * @typedef {ModuleLibraryPluginParsed} T
 * @extends {AbstractLibraryPlugin<ModuleLibraryPluginParsed>}
 */
class ModuleLibraryPlugin extends AbstractLibraryPlugin {
    /**
     * Creates an instance of ModuleLibraryPlugin.
     * @param {ModuleLibraryPluginOptions} options the plugin options
     */
    constructor(options) {
        super({
            pluginName: "ModuleLibraryPlugin",
            type: options.type
        });
    }
 
    /**
     * Applies the plugin by registering its hooks on the compiler.
     * @param {Compiler} compiler the compiler instance
     * @returns {void}
     */
    apply(compiler) {
        super.apply(compiler);
 
        compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
            const { onDemandExportsGeneration } =
                ConcatenatedModule.getCompilationHooks(compilation);
            onDemandExportsGeneration.tap(
                PLUGIN_NAME,
                (module, runtimes, source, finalName) => {
                    /** @type {BuildMeta} */
                    const buildMeta = module.buildMeta || (module.buildMeta = {});
 
                    /** @type {BuildMeta["exportsSourceByRuntime"]} */
                    const exportsSourceByRuntime =
                        buildMeta.exportsSourceByRuntime ||
                        (buildMeta.exportsSourceByRuntime = new Map());
 
                    /** @type {BuildMeta["exportsFinalNameByRuntime"]} */
                    const exportsFinalNameByRuntime =
                        buildMeta.exportsFinalNameByRuntime ||
                        (buildMeta.exportsFinalNameByRuntime = new Map());
 
                    for (const runtime of runtimes) {
                        const key = getRuntimeKey(runtime);
                        exportsSourceByRuntime.set(key, source);
                        exportsFinalNameByRuntime.set(key, finalName);
                    }
 
                    return true;
                }
            );
        });
    }
 
    /**
     * 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,
        { options, compilation, compilation: { moduleGraph } }
    ) {
        const runtime = getEntryRuntime(compilation, entryName);
        if (options.export) {
            const exportsInfo = moduleGraph.getExportInfo(
                module,
                Array.isArray(options.export) ? options.export[0] : options.export
            );
            exportsInfo.setUsed(UsageState.Used, runtime);
            exportsInfo.canMangleUse = false;
        } else {
            const exportsInfo = moduleGraph.getExportsInfo(module);
 
            if (
                // If the entry module is commonjs, its exports cannot be mangled
                (module.buildMeta && module.buildMeta.treatAsCommonJs) ||
                // The entry module provides unknown exports
                exportsInfo._otherExportsInfo.provided === null
            ) {
                exportsInfo.setUsedInUnknownWay(runtime);
            } else {
                exportsInfo.setAllKnownExportsUsed(runtime);
            }
        }
        moduleGraph.addExtraReason(module, "used as library export");
    }
 
    /**
     * Returns preprocess as needed by overriding.
     * @param {LibraryOptions} library normalized library option
     * @returns {T} preprocess as needed by overriding
     */
    parseOptions(library) {
        const { name } = library;
        if (name) {
            throw new Error(
                `Library name must be unset. ${AbstractLibraryPlugin.COMMON_LIBRARY_NAME_MESSAGE}`
            );
        }
        const _name = /** @type {string} */ (name);
        return {
            name: _name,
            export: library.export
        };
    }
 
    /**
     * Analyze unknown provided exports.
     * @param {Source} source source
     * @param {Module} module module
     * @param {ModuleGraph} moduleGraph moduleGraph
     * @param {RuntimeSpec} runtime chunk runtime
     * @param {[string, string][]} exports exports
     * @param {Set<string>} alreadyRenderedExports already rendered exports
     * @returns {ConcatSource} source with null provided exports
     */
    _analyzeUnknownProvidedExports(
        source,
        module,
        moduleGraph,
        runtime,
        exports,
        alreadyRenderedExports
    ) {
        const result = new ConcatSource(source);
        /** @type {Set<string>} */
        const moduleRequests = new Set();
        /** @type {Map<string, string>} */
        const unknownProvidedExports = new Map();
 
        /**
         * Resolves dynamic star reexport.
         * @param {Module} module the module
         * @param {boolean} isDynamicReexport if module is dynamic reexported
         */
        const resolveDynamicStarReexport = (module, isDynamicReexport) => {
            for (const connection of moduleGraph.getOutgoingConnections(module)) {
                const dep = connection.dependency;
 
                // Only handle star-reexport statement
                if (
                    dep instanceof HarmonyExportImportedSpecifierDependency &&
                    dep.name === null
                ) {
                    const importedModule = connection.resolvedModule;
                    const importedModuleExportsInfo =
                        moduleGraph.getExportsInfo(importedModule);
 
                    // The imported module provides unknown exports
                    // So keep the reexports rendered in the bundle
                    if (
                        dep.getMode(moduleGraph, runtime).type === "dynamic-reexport" &&
                        importedModuleExportsInfo._otherExportsInfo.provided === null
                    ) {
                        // Handle export * from 'external'
                        if (importedModule instanceof ExternalModule) {
                            moduleRequests.add(importedModule.userRequest);
                        } else {
                            resolveDynamicStarReexport(importedModule, true);
                        }
                    }
                    // If importer modules existing `dynamic-reexport` dependency
                    // We should keep export statement rendered in the bundle
                    else if (isDynamicReexport) {
                        for (const exportInfo of importedModuleExportsInfo.orderedExports) {
                            if (!exportInfo.provided || exportInfo.name === "default") {
                                continue;
                            }
                            const originalName = exportInfo.name;
                            const usedName = exportInfo.getUsedName(originalName, runtime);
 
                            if (!alreadyRenderedExports.has(originalName) && usedName) {
                                unknownProvidedExports.set(originalName, usedName);
                            }
                        }
                    }
                }
            }
        };
 
        resolveDynamicStarReexport(module, false);
 
        for (const request of moduleRequests) {
            result.add(`export * from "${request}";\n`);
        }
 
        for (const [origin, used] of unknownProvidedExports) {
            exports.push([
                origin,
                `${RuntimeGlobals.exports}${propertyAccess([used])}`
            ]);
        }
 
        return result;
    }
 
    /**
     * 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, { options, compilation }) {
        const {
            moduleGraph,
            chunk,
            codeGenerationResults,
            inlined,
            inlinedInIIFE,
            runtimeTemplate
        } = renderContext;
        let result = new ConcatSource(source);
        const exportInfos = options.export
            ? [
                    moduleGraph.getExportInfo(
                        module,
                        Array.isArray(options.export) ? options.export[0] : options.export
                    )
                ]
            : moduleGraph.getExportsInfo(module).orderedExports;
 
        const exportsFinalNameByRuntime =
            (module.buildMeta &&
                module.buildMeta.exportsFinalNameByRuntime &&
                module.buildMeta.exportsFinalNameByRuntime.get(
                    getRuntimeKey(chunk.runtime)
                )) ||
            {};
 
        const isInlinedEntryWithoutIIFE = inlined && !inlinedInIIFE;
        // Direct export bindings from on-demand concatenation
        const definitions = isInlinedEntryWithoutIIFE
            ? exportsFinalNameByRuntime
            : {};
 
        /** @type {string[]} */
        const shortHandedExports = [];
        /** @type {[string, string][]} */
        const exports = [];
        /** @type {Set<string>} */
        const alreadyRenderedExports = new Set();
 
        const isAsync = moduleGraph.isAsync(module);
 
        const treatAsCommonJs =
            module.buildMeta && module.buildMeta.treatAsCommonJs;
        const skipRenderDefaultExport = Boolean(treatAsCommonJs);
 
        const moduleExportsInfo = moduleGraph.getExportsInfo(module);
 
        // Define ESM compatibility flag will rely on `__webpack_exports__`
        const needHarmonyCompatibilityFlag =
            moduleExportsInfo.otherExportsInfo.getUsed(chunk.runtime) !==
                UsageState.Unused ||
            moduleExportsInfo
                .getReadOnlyExportInfo("__esModule")
                .getUsed(chunk.runtime) !== UsageState.Unused;
 
        let needExportsDeclaration =
            !isInlinedEntryWithoutIIFE || isAsync || needHarmonyCompatibilityFlag;
 
        if (isAsync) {
            result.add(
                `${RuntimeGlobals.exports} = await ${RuntimeGlobals.exports};\n`
            );
        }
 
        // Try to find all known exports of the entry module
        outer: for (const exportInfo of exportInfos) {
            if (!exportInfo.provided) continue;
 
            const originalName = exportInfo.name;
            // Skip rendering the default export in some cases
            if (skipRenderDefaultExport && originalName === "default") continue;
 
            // Try to find all exports from the reexported modules
            const target = exportInfo.findTarget(moduleGraph, (_m) => true);
            if (target) {
                const reexportsInfo = moduleGraph.getExportsInfo(target.module);
                for (const reexportInfo of reexportsInfo.orderedExports) {
                    if (
                        reexportInfo.provided === false &&
                        reexportInfo.name !== "default" &&
                        reexportInfo.name === /** @type {string[]} */ (target.export)[0]
                    ) {
                        continue outer;
                    }
                }
            }
 
            const usedName =
                /** @type {string} */
                (exportInfo.getUsedName(originalName, chunk.runtime));
            /** @type {string | undefined} */
            const definition = definitions[usedName];
            /** @type {string | undefined} */
            let finalName;
 
            if (definition) {
                finalName = definition;
            } else {
                // Fallback to `__webpack_exports__` property access
                // when no direct export binding was found
                finalName = `${RuntimeGlobals.exports}${Template.toIdentifier(originalName)}`;
                needExportsDeclaration = true;
                result.add(
                    `${runtimeTemplate.renderConst()} ${finalName} = ${RuntimeGlobals.exports}${propertyAccess(
                        [usedName]
                    )};\n`
                );
            }
 
            if (
                // If the name includes `property access` and `call expressions`
                finalName &&
                (finalName.includes(".") ||
                    finalName.includes("[") ||
                    finalName.includes("("))
            ) {
                if (exportInfo.isReexport()) {
                    const { data } = codeGenerationResults.get(module, chunk.runtime);
                    const topLevelDeclarations =
                        (data && data.get("topLevelDeclarations")) ||
                        (module.buildInfo && module.buildInfo.topLevelDeclarations);
 
                    if (topLevelDeclarations && topLevelDeclarations.has(originalName)) {
                        const name = `${RuntimeGlobals.exports}${Template.toIdentifier(originalName)}`;
                        result.add(
                            `${runtimeTemplate.renderConst()} ${name} = ${finalName};\n`
                        );
                        shortHandedExports.push(`${name} as ${originalName}`);
                    } else {
                        exports.push([originalName, finalName]);
                    }
                } else {
                    exports.push([originalName, finalName]);
                }
            } else {
                shortHandedExports.push(
                    definition && finalName === originalName
                        ? finalName
                        : `${finalName} as ${originalName}`
                );
            }
 
            alreadyRenderedExports.add(originalName);
        }
 
        // Add default export `__webpack_exports__` statement to keep better compatibility
        if (treatAsCommonJs) {
            needExportsDeclaration = true;
            shortHandedExports.push(`${RuntimeGlobals.exports} as default`);
        }
 
        if (shortHandedExports.length > 0) {
            result.add(`export { ${shortHandedExports.join(", ")} };\n`);
        }
 
        result = this._analyzeUnknownProvidedExports(
            result,
            module,
            moduleGraph,
            chunk.runtime,
            exports,
            alreadyRenderedExports
        );
 
        for (const [exportName, final] of exports) {
            result.add(
                `export ${runtimeTemplate.renderConst()} ${exportName} = ${final};\n`
            );
        }
 
        if (!needExportsDeclaration) {
            renderContext.needExportsDeclaration = false;
        }
 
        return result;
    }
 
    /**
     * 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,
        { factory, inlinedInIIFE, chunk },
        libraryContext
    ) {
        const exportsSource =
            module.buildMeta &&
            module.buildMeta.exportsSourceByRuntime &&
            module.buildMeta.exportsSourceByRuntime.get(getRuntimeKey(chunk.runtime));
 
        // Re-add the module's exports source when rendered in factory
        // or as an inlined startup module wrapped in an IIFE
        if ((inlinedInIIFE || factory) && exportsSource) {
            return new ConcatSource(exportsSource, source);
        }
        return source;
    }
}
 
module.exports = ModuleLibraryPlugin;