source: frontend/node_modules/webpack/lib/ExternalsPlugin.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 2.6 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const { ModuleExternalInitFragment } = require("./ExternalModule");
9const ExternalModuleFactoryPlugin = require("./ExternalModuleFactoryPlugin");
10const ConcatenatedModule = require("./optimize/ConcatenatedModule");
11
12/** @typedef {import("../declarations/WebpackOptions").ExternalsType} ExternalsType */
13/** @typedef {import("../declarations/WebpackOptions").Externals} Externals */
14/** @typedef {import("./Compiler")} Compiler */
15/** @typedef {import("./ExternalModule").Imported} Imported */
16/** @typedef {import("./Dependency")} Dependency */
17
18const PLUGIN_NAME = "ExternalsPlugin";
19
20class ExternalsPlugin {
21 /**
22 * Creates an instance of ExternalsPlugin.
23 * @param {ExternalsType | ((dependency: Dependency) => ExternalsType)} type default external type
24 * @param {Externals} externals externals config
25 */
26 constructor(type, externals) {
27 this.type = type;
28 this.externals = externals;
29 }
30
31 /**
32 * Applies the plugin by registering its hooks on the compiler.
33 * @param {Compiler} compiler the compiler instance
34 * @returns {void}
35 */
36 apply(compiler) {
37 compiler.hooks.compile.tap(PLUGIN_NAME, ({ normalModuleFactory }) => {
38 new ExternalModuleFactoryPlugin(this.type, this.externals).apply(
39 normalModuleFactory
40 );
41 });
42
43 compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
44 const { concatenatedModuleInfo } =
45 ConcatenatedModule.getCompilationHooks(compilation);
46 concatenatedModuleInfo.tap(PLUGIN_NAME, (updatedInfo, moduleInfo) => {
47 const rawExportMap = updatedInfo.rawExportMap;
48
49 if (!rawExportMap) {
50 return;
51 }
52
53 const chunkInitFragments = moduleInfo.chunkInitFragments;
54 const moduleExternalInitFragments =
55 /** @type {ModuleExternalInitFragment[]} */
56 (
57 chunkInitFragments
58 ? /** @type {unknown[]} */
59 (chunkInitFragments).filter(
60 (fragment) => fragment instanceof ModuleExternalInitFragment
61 )
62 : []
63 );
64
65 let initFragmentChanged = false;
66
67 for (const fragment of moduleExternalInitFragments) {
68 const imported = fragment.getImported();
69
70 if (Array.isArray(imported)) {
71 const newImported =
72 /** @type {Imported} */
73 (
74 imported.map(([specifier, finalName]) => [
75 specifier,
76 rawExportMap.has(specifier)
77 ? rawExportMap.get(specifier)
78 : finalName
79 ])
80 );
81 fragment.setImported(newImported);
82 initFragmentChanged = true;
83 }
84 }
85
86 if (initFragmentChanged) {
87 return true;
88 }
89 });
90 });
91 }
92}
93
94module.exports = ExternalsPlugin;
Note: See TracBrowser for help on using the repository browser.