1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const { RawSource } = require("webpack-sources");
|
9 | const Module = require("../Module");
|
10 | const RuntimeGlobals = require("../RuntimeGlobals");
|
11 | const Template = require("../Template");
|
12 | const makeSerializable = require("../util/makeSerializable");
|
13 | const FallbackItemDependency = require("./FallbackItemDependency");
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | const TYPES = new Set(["javascript"]);
|
31 | const RUNTIME_REQUIREMENTS = new Set([RuntimeGlobals.module]);
|
32 |
|
33 | class FallbackModule extends Module {
|
34 | |
35 |
|
36 |
|
37 | constructor(requests) {
|
38 | super("fallback-module");
|
39 | this.requests = requests;
|
40 | this._identifier = `fallback ${this.requests.join(" ")}`;
|
41 | }
|
42 |
|
43 | |
44 |
|
45 |
|
46 | identifier() {
|
47 | return this._identifier;
|
48 | }
|
49 |
|
50 | |
51 |
|
52 |
|
53 |
|
54 | readableIdentifier(requestShortener) {
|
55 | return this._identifier;
|
56 | }
|
57 |
|
58 | |
59 |
|
60 |
|
61 |
|
62 | libIdent(options) {
|
63 | return `${this.layer ? `(${this.layer})/` : ""}webpack/container/fallback/${
|
64 | this.requests[0]
|
65 | }/and ${this.requests.length - 1} more`;
|
66 | }
|
67 |
|
68 | |
69 |
|
70 |
|
71 |
|
72 |
|
73 | chunkCondition(chunk, { chunkGraph }) {
|
74 | return chunkGraph.getNumberOfEntryModules(chunk) > 0;
|
75 | }
|
76 |
|
77 | |
78 |
|
79 |
|
80 |
|
81 |
|
82 | needBuild(context, callback) {
|
83 | callback(null, !this.buildInfo);
|
84 | }
|
85 |
|
86 | |
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | build(options, compilation, resolver, fs, callback) {
|
95 | this.buildMeta = {};
|
96 | this.buildInfo = {
|
97 | strict: true
|
98 | };
|
99 |
|
100 | this.clearDependenciesAndBlocks();
|
101 | for (const request of this.requests)
|
102 | this.addDependency(new FallbackItemDependency(request));
|
103 |
|
104 | callback();
|
105 | }
|
106 |
|
107 | |
108 |
|
109 |
|
110 |
|
111 | size(type) {
|
112 | return this.requests.length * 5 + 42;
|
113 | }
|
114 |
|
115 | |
116 |
|
117 |
|
118 | getSourceTypes() {
|
119 | return TYPES;
|
120 | }
|
121 |
|
122 | |
123 |
|
124 |
|
125 |
|
126 | codeGeneration({ runtimeTemplate, moduleGraph, chunkGraph }) {
|
127 | const ids = this.dependencies.map(dep =>
|
128 | chunkGraph.getModuleId(moduleGraph.getModule(dep))
|
129 | );
|
130 | const code = Template.asString([
|
131 | `var ids = ${JSON.stringify(ids)};`,
|
132 | "var error, result, i = 0;",
|
133 | `var loop = ${runtimeTemplate.basicFunction("next", [
|
134 | "while(i < ids.length) {",
|
135 | Template.indent([
|
136 | "try { next = __webpack_require__(ids[i++]); } catch(e) { return handleError(e); }",
|
137 | "if(next) return next.then ? next.then(handleResult, handleError) : handleResult(next);"
|
138 | ]),
|
139 | "}",
|
140 | "if(error) throw error;"
|
141 | ])}`,
|
142 | `var handleResult = ${runtimeTemplate.basicFunction("result", [
|
143 | "if(result) return result;",
|
144 | "return loop();"
|
145 | ])};`,
|
146 | `var handleError = ${runtimeTemplate.basicFunction("e", [
|
147 | "error = e;",
|
148 | "return loop();"
|
149 | ])};`,
|
150 | "module.exports = loop();"
|
151 | ]);
|
152 | const sources = new Map();
|
153 | sources.set("javascript", new RawSource(code));
|
154 | return { sources, runtimeRequirements: RUNTIME_REQUIREMENTS };
|
155 | }
|
156 |
|
157 | serialize(context) {
|
158 | const { write } = context;
|
159 | write(this.requests);
|
160 | super.serialize(context);
|
161 | }
|
162 |
|
163 | static deserialize(context) {
|
164 | const { read } = context;
|
165 | const obj = new FallbackModule(read());
|
166 | obj.deserialize(context);
|
167 | return obj;
|
168 | }
|
169 | }
|
170 |
|
171 | makeSerializable(FallbackModule, "webpack/lib/container/FallbackModule");
|
172 |
|
173 | module.exports = FallbackModule;
|