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