1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const { OriginalSource, RawSource } = require("webpack-sources");
|
9 | const Module = require("./Module");
|
10 | const RuntimeGlobals = require("./RuntimeGlobals");
|
11 | const DelegatedSourceDependency = require("./dependencies/DelegatedSourceDependency");
|
12 | const StaticExportsDependency = require("./dependencies/StaticExportsDependency");
|
13 | const makeSerializable = require("./util/makeSerializable");
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | const TYPES = new Set(["javascript"]);
|
36 | const RUNTIME_REQUIREMENTS = new Set([
|
37 | RuntimeGlobals.module,
|
38 | RuntimeGlobals.require
|
39 | ]);
|
40 |
|
41 | class DelegatedModule extends Module {
|
42 | constructor(sourceRequest, data, type, userRequest, originalRequest) {
|
43 | super("javascript/dynamic", null);
|
44 |
|
45 |
|
46 | this.sourceRequest = sourceRequest;
|
47 | this.request = data.id;
|
48 | this.delegationType = type;
|
49 | this.userRequest = userRequest;
|
50 | this.originalRequest = originalRequest;
|
51 |
|
52 | this.delegateData = data;
|
53 |
|
54 |
|
55 | this.delegatedSourceDependency = undefined;
|
56 | }
|
57 |
|
58 | |
59 |
|
60 |
|
61 | getSourceTypes() {
|
62 | return TYPES;
|
63 | }
|
64 |
|
65 | |
66 |
|
67 |
|
68 |
|
69 | libIdent(options) {
|
70 | return typeof this.originalRequest === "string"
|
71 | ? this.originalRequest
|
72 | : this.originalRequest.libIdent(options);
|
73 | }
|
74 |
|
75 | |
76 |
|
77 |
|
78 | identifier() {
|
79 | return `delegated ${JSON.stringify(this.request)} from ${
|
80 | this.sourceRequest
|
81 | }`;
|
82 | }
|
83 |
|
84 | |
85 |
|
86 |
|
87 |
|
88 | readableIdentifier(requestShortener) {
|
89 | return `delegated ${this.userRequest} from ${this.sourceRequest}`;
|
90 | }
|
91 |
|
92 | |
93 |
|
94 |
|
95 |
|
96 |
|
97 | needBuild(context, callback) {
|
98 | return callback(null, !this.buildMeta);
|
99 | }
|
100 |
|
101 | |
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | build(options, compilation, resolver, fs, callback) {
|
110 | this.buildMeta = { ...this.delegateData.buildMeta };
|
111 | this.buildInfo = {};
|
112 | this.dependencies.length = 0;
|
113 | this.delegatedSourceDependency = new DelegatedSourceDependency(
|
114 | this.sourceRequest
|
115 | );
|
116 | this.addDependency(this.delegatedSourceDependency);
|
117 | this.addDependency(
|
118 | new StaticExportsDependency(this.delegateData.exports || true, false)
|
119 | );
|
120 | callback();
|
121 | }
|
122 |
|
123 | |
124 |
|
125 |
|
126 |
|
127 | codeGeneration({ runtimeTemplate, moduleGraph, chunkGraph }) {
|
128 | const dep = (this.dependencies[0]);
|
129 | const sourceModule = moduleGraph.getModule(dep);
|
130 | let str;
|
131 |
|
132 | if (!sourceModule) {
|
133 | str = runtimeTemplate.throwMissingModuleErrorBlock({
|
134 | request: this.sourceRequest
|
135 | });
|
136 | } else {
|
137 | str = `module.exports = (${runtimeTemplate.moduleExports({
|
138 | module: sourceModule,
|
139 | chunkGraph,
|
140 | request: dep.request,
|
141 | runtimeRequirements: new Set()
|
142 | })})`;
|
143 |
|
144 | switch (this.delegationType) {
|
145 | case "require":
|
146 | str += `(${JSON.stringify(this.request)})`;
|
147 | break;
|
148 | case "object":
|
149 | str += `[${JSON.stringify(this.request)}]`;
|
150 | break;
|
151 | }
|
152 |
|
153 | str += ";";
|
154 | }
|
155 |
|
156 | const sources = new Map();
|
157 | if (this.useSourceMap || this.useSimpleSourceMap) {
|
158 | sources.set("javascript", new OriginalSource(str, this.identifier()));
|
159 | } else {
|
160 | sources.set("javascript", new RawSource(str));
|
161 | }
|
162 |
|
163 | return {
|
164 | sources,
|
165 | runtimeRequirements: RUNTIME_REQUIREMENTS
|
166 | };
|
167 | }
|
168 |
|
169 | |
170 |
|
171 |
|
172 |
|
173 | size(type) {
|
174 | return 42;
|
175 | }
|
176 |
|
177 | |
178 |
|
179 |
|
180 |
|
181 |
|
182 | updateHash(hash, context) {
|
183 | hash.update(this.delegationType);
|
184 | hash.update(JSON.stringify(this.request));
|
185 | super.updateHash(hash, context);
|
186 | }
|
187 |
|
188 | serialize(context) {
|
189 | const { write } = context;
|
190 |
|
191 | write(this.sourceRequest);
|
192 | write(this.delegateData);
|
193 | write(this.delegationType);
|
194 | write(this.userRequest);
|
195 | write(this.originalRequest);
|
196 | super.serialize(context);
|
197 | }
|
198 |
|
199 | static deserialize(context) {
|
200 | const { read } = context;
|
201 | const obj = new DelegatedModule(
|
202 | read(),
|
203 | read(),
|
204 | read(),
|
205 | read(),
|
206 | read()
|
207 | );
|
208 | obj.deserialize(context);
|
209 | return obj;
|
210 | }
|
211 |
|
212 | |
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 | updateCacheModule(module) {
|
220 | super.updateCacheModule(module);
|
221 | const m = (module);
|
222 | this.delegationType = m.delegationType;
|
223 | this.userRequest = m.userRequest;
|
224 | this.originalRequest = m.originalRequest;
|
225 | this.delegateData = m.delegateData;
|
226 | }
|
227 |
|
228 | |
229 |
|
230 |
|
231 | cleanupForCache() {
|
232 | super.cleanupForCache();
|
233 | this.delegateData = undefined;
|
234 | }
|
235 | }
|
236 |
|
237 | makeSerializable(DelegatedModule, "webpack/lib/DelegatedModule");
|
238 |
|
239 | module.exports = DelegatedModule;
|