source: frontend/node_modules/webpack/lib/container/FallbackModule.js

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

Fix frontend appearance

  • Property mode set to 100644
File size: 6.9 KB
RevLine 
[9af201e]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 { RawSource } = require("webpack-sources");
9const Module = require("../Module");
10const {
11 JAVASCRIPT_TYPE,
12 JAVASCRIPT_TYPES
13} = require("../ModuleSourceTypeConstants");
14const { WEBPACK_MODULE_TYPE_FALLBACK } = require("../ModuleTypeConstants");
15const RuntimeGlobals = require("../RuntimeGlobals");
16const Template = require("../Template");
17const makeSerializable = require("../util/makeSerializable");
18const FallbackItemDependency = require("./FallbackItemDependency");
19
20/** @typedef {import("../config/defaults").WebpackOptionsNormalizedWithDefaults} WebpackOptions */
21/** @typedef {import("../Chunk")} Chunk */
22/** @typedef {import("../Compilation")} Compilation */
23/** @typedef {import("../Module").BuildCallback} BuildCallback */
24/** @typedef {import("../Module").CodeGenerationContext} CodeGenerationContext */
25/** @typedef {import("../Module").CodeGenerationResult} CodeGenerationResult */
26/** @typedef {import("../Module").LibIdentOptions} LibIdentOptions */
27/** @typedef {import("../Module").LibIdent} LibIdent */
28/** @typedef {import("../Module").NameForCondition} NameForCondition */
29/** @typedef {import("../Module").NeedBuildCallback} NeedBuildCallback */
30/** @typedef {import("../Module").NeedBuildContext} NeedBuildContext */
31/** @typedef {import("../Module").Sources} Sources */
32/** @typedef {import("../Module").SourceTypes} SourceTypes */
33/** @typedef {import("../RequestShortener")} RequestShortener */
34/** @typedef {import("../ResolverFactory").ResolverWithOptions} ResolverWithOptions */
35/** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
36/** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
37/** @typedef {import("../util/fs").InputFileSystem} InputFileSystem */
38/** @typedef {import("./RemoteModule").ExternalRequests} ExternalRequests */
39
40const RUNTIME_REQUIREMENTS = new Set([RuntimeGlobals.module]);
41
42class FallbackModule extends Module {
43 /**
44 * Creates an instance of FallbackModule.
45 * @param {ExternalRequests} requests list of requests to choose one
46 */
47 constructor(requests) {
48 super(WEBPACK_MODULE_TYPE_FALLBACK);
49 /** @type {ExternalRequests} */
50 this.requests = requests;
51 /** @type {string} */
52 this._identifier = `fallback ${this.requests.join(" ")}`;
53 }
54
55 /**
56 * Returns the unique identifier used to reference this module.
57 * @returns {string} a unique identifier of the module
58 */
59 identifier() {
60 return this._identifier;
61 }
62
63 /**
64 * Returns a human-readable identifier for this module.
65 * @param {RequestShortener} requestShortener the request shortener
66 * @returns {string} a user readable identifier of the module
67 */
68 readableIdentifier(requestShortener) {
69 return this._identifier;
70 }
71
72 /**
73 * Gets the library identifier.
74 * @param {LibIdentOptions} options options
75 * @returns {LibIdent | null} an identifier for library inclusion
76 */
77 libIdent(options) {
78 return `${this.layer ? `(${this.layer})/` : ""}webpack/container/fallback/${
79 this.requests[0]
80 }/and ${this.requests.length - 1} more`;
81 }
82
83 /**
84 * Returns true if the module can be placed in the chunk.
85 * @param {Chunk} chunk the chunk which condition should be checked
86 * @param {Compilation} compilation the compilation
87 * @returns {boolean} true if the module can be placed in the chunk
88 */
89 chunkCondition(chunk, { chunkGraph }) {
90 return chunkGraph.getNumberOfEntryModules(chunk) > 0;
91 }
92
93 /**
94 * Checks whether the module needs to be rebuilt for the current build state.
95 * @param {NeedBuildContext} context context info
96 * @param {NeedBuildCallback} callback callback function, returns true, if the module needs a rebuild
97 * @returns {void}
98 */
99 needBuild(context, callback) {
100 callback(null, !this.buildInfo);
101 }
102
103 /**
104 * Builds the module using the provided compilation context.
105 * @param {WebpackOptions} options webpack options
106 * @param {Compilation} compilation the compilation
107 * @param {ResolverWithOptions} resolver the resolver
108 * @param {InputFileSystem} fs the file system
109 * @param {BuildCallback} callback callback function
110 * @returns {void}
111 */
112 build(options, compilation, resolver, fs, callback) {
113 this.buildMeta = {};
114 this.buildInfo = {
115 strict: true
116 };
117
118 this.clearDependenciesAndBlocks();
119 for (const request of this.requests) {
120 this.addDependency(new FallbackItemDependency(request));
121 }
122
123 callback();
124 }
125
126 /**
127 * Returns the estimated size for the requested source type.
128 * @param {string=} type the source type for which the size should be estimated
129 * @returns {number} the estimated size of the module (must be non-zero)
130 */
131 size(type) {
132 return this.requests.length * 5 + 42;
133 }
134
135 /**
136 * Returns the source types this module can generate.
137 * @returns {SourceTypes} types available (do not mutate)
138 */
139 getSourceTypes() {
140 return JAVASCRIPT_TYPES;
141 }
142
143 /**
144 * Generates code and runtime requirements for this module.
145 * @param {CodeGenerationContext} context context for code generation
146 * @returns {CodeGenerationResult} result
147 */
148 codeGeneration({ runtimeTemplate, moduleGraph, chunkGraph }) {
149 const ids = this.dependencies.map((dep) =>
150 chunkGraph.getModuleId(/** @type {Module} */ (moduleGraph.getModule(dep)))
151 );
152 const code = Template.asString([
153 `var ids = ${JSON.stringify(ids)};`,
154 "var error, result, i = 0;",
155 `var loop = ${runtimeTemplate.basicFunction("next", [
156 "while(i < ids.length) {",
157 Template.indent([
158 `try { next = ${RuntimeGlobals.require}(ids[i++]); } catch(e) { return handleError(e); }`,
159 "if(next) return next.then ? next.then(handleResult, handleError) : handleResult(next);"
160 ]),
161 "}",
162 "if(error) throw error;"
163 ])}`,
164 `var handleResult = ${runtimeTemplate.basicFunction("result", [
165 "if(result) return result;",
166 "return loop();"
167 ])};`,
168 `var handleError = ${runtimeTemplate.basicFunction("e", [
169 "error = e;",
170 "return loop();"
171 ])};`,
172 "module.exports = loop();"
173 ]);
174 /** @type {Sources} */
175 const sources = new Map();
176 sources.set(JAVASCRIPT_TYPE, new RawSource(code));
177 return { sources, runtimeRequirements: RUNTIME_REQUIREMENTS };
178 }
179
180 /**
181 * Serializes this instance into the provided serializer context.
182 * @param {ObjectSerializerContext} context context
183 */
184 serialize(context) {
185 const { write } = context;
186 write(this.requests);
187 super.serialize(context);
188 }
189
190 /**
191 * Restores this instance from the provided deserializer context.
192 * @param {ObjectDeserializerContext} context context
193 * @returns {FallbackModule} deserialized fallback module
194 */
195 static deserialize(context) {
196 const { read } = context;
197 const obj = new FallbackModule(read());
198 obj.deserialize(context);
199 return obj;
200 }
201}
202
203makeSerializable(FallbackModule, "webpack/lib/container/FallbackModule");
204
205module.exports = FallbackModule;
Note: See TracBrowser for help on using the repository browser.