source: frontend/node_modules/webpack/lib/container/ContainerReferencePlugin.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: 4.6 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra and Zackary Jackson @ScriptedAlchemy
4*/
5
6"use strict";
7
8const ExternalModule = require("../ExternalModule");
9const ExternalsPlugin = require("../ExternalsPlugin");
10const RuntimeGlobals = require("../RuntimeGlobals");
11const FallbackDependency = require("./FallbackDependency");
12const FallbackItemDependency = require("./FallbackItemDependency");
13const FallbackModuleFactory = require("./FallbackModuleFactory");
14const RemoteModule = require("./RemoteModule");
15const RemoteRuntimeModule = require("./RemoteRuntimeModule");
16const RemoteToExternalDependency = require("./RemoteToExternalDependency");
17const { parseOptions } = require("./options");
18
19/** @typedef {import("../../declarations/plugins/container/ContainerReferencePlugin").ContainerReferencePluginOptions} ContainerReferencePluginOptions */
20/** @typedef {import("../Compiler")} Compiler */
21
22const slashCode = "/".charCodeAt(0);
23const PLUGIN_NAME = "ContainerReferencePlugin";
24
25class ContainerReferencePlugin {
26 /**
27 * Creates an instance of ContainerReferencePlugin.
28 * @param {ContainerReferencePluginOptions} options options
29 */
30 constructor(options) {
31 /** @typedef {ContainerReferencePluginOptions} */
32 this.options = options;
33 }
34
35 /**
36 * Applies the plugin by registering its hooks on the compiler.
37 * @param {Compiler} compiler the compiler instance
38 * @returns {void}
39 */
40 apply(compiler) {
41 compiler.hooks.validate.tap(PLUGIN_NAME, () => {
42 compiler.validate(
43 () =>
44 require("../../schemas/plugins/container/ContainerReferencePlugin.json"),
45 this.options,
46 {
47 name: "Container Reference Plugin",
48 baseDataPath: "options"
49 },
50 (options) =>
51 require("../../schemas/plugins/container/ContainerReferencePlugin.check")(
52 options
53 )
54 );
55 });
56
57 const { remoteType } = this.options;
58 const remotes = parseOptions(
59 this.options.remotes,
60 (item) => ({
61 external: Array.isArray(item) ? item : [item],
62 shareScope: this.options.shareScope || "default"
63 }),
64 (item) => ({
65 external: Array.isArray(item.external)
66 ? item.external
67 : [item.external],
68 shareScope: item.shareScope || this.options.shareScope || "default"
69 })
70 );
71
72 /** @type {Record<string, string>} */
73 const remoteExternals = {};
74 for (const [key, config] of remotes) {
75 let i = 0;
76 for (const external of config.external) {
77 if (external.startsWith("internal ")) continue;
78 remoteExternals[
79 `webpack/container/reference/${key}${i ? `/fallback-${i}` : ""}`
80 ] = external;
81 i++;
82 }
83 }
84
85 new ExternalsPlugin(remoteType, remoteExternals).apply(compiler);
86
87 compiler.hooks.compilation.tap(
88 PLUGIN_NAME,
89 (compilation, { normalModuleFactory }) => {
90 compilation.dependencyFactories.set(
91 RemoteToExternalDependency,
92 normalModuleFactory
93 );
94
95 compilation.dependencyFactories.set(
96 FallbackItemDependency,
97 normalModuleFactory
98 );
99
100 compilation.dependencyFactories.set(
101 FallbackDependency,
102 new FallbackModuleFactory()
103 );
104
105 normalModuleFactory.hooks.factorize.tap(PLUGIN_NAME, (data) => {
106 if (!data.request.includes("!")) {
107 for (const [key, config] of remotes) {
108 if (
109 data.request.startsWith(`${key}`) &&
110 (data.request.length === key.length ||
111 data.request.charCodeAt(key.length) === slashCode)
112 ) {
113 return new RemoteModule(
114 data.request,
115 config.external.map((external, i) =>
116 external.startsWith("internal ")
117 ? external.slice(9)
118 : `webpack/container/reference/${key}${
119 i ? `/fallback-${i}` : ""
120 }`
121 ),
122 `.${data.request.slice(key.length)}`,
123 config.shareScope
124 );
125 }
126 }
127 }
128 });
129
130 compilation.hooks.runtimeRequirementInTree
131 .for(RuntimeGlobals.ensureChunkHandlers)
132 .tap(PLUGIN_NAME, (chunk, set) => {
133 set.add(RuntimeGlobals.module);
134 set.add(RuntimeGlobals.moduleFactoriesAddOnly);
135 set.add(RuntimeGlobals.hasOwnProperty);
136 set.add(RuntimeGlobals.initializeSharing);
137 set.add(RuntimeGlobals.shareScopeMap);
138 compilation.addRuntimeModule(chunk, new RemoteRuntimeModule());
139 });
140
141 const { chunkCondition } =
142 ExternalModule.getCompilationHooks(compilation);
143
144 // External modules issued by remote modules should be placed in entry chunks
145 // to ensure they are loaded and initialize first
146 chunkCondition.tap(
147 PLUGIN_NAME,
148 (chunk, compilation) =>
149 compilation.chunkGraph.getNumberOfEntryModules(chunk) > 0
150 );
151 }
152 );
153 }
154}
155
156module.exports = ContainerReferencePlugin;
Note: See TracBrowser for help on using the repository browser.