1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const { RawSource } = require("webpack-sources");
|
9 | const AsyncDependenciesBlock = require("../AsyncDependenciesBlock");
|
10 | const Module = require("../Module");
|
11 | const RuntimeGlobals = require("../RuntimeGlobals");
|
12 | const makeSerializable = require("../util/makeSerializable");
|
13 | const { rangeToString, stringifyHoley } = require("../util/semver");
|
14 | const ConsumeSharedFallbackDependency = require("./ConsumeSharedFallbackDependency");
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | const TYPES = new Set(["consume-shared"]);
|
46 |
|
47 | class ConsumeSharedModule extends Module {
|
48 | |
49 |
|
50 |
|
51 |
|
52 | constructor(context, options) {
|
53 | super("consume-shared-module", context);
|
54 | this.options = options;
|
55 | }
|
56 |
|
57 | |
58 |
|
59 |
|
60 | identifier() {
|
61 | const {
|
62 | shareKey,
|
63 | shareScope,
|
64 | importResolved,
|
65 | requiredVersion,
|
66 | strictVersion,
|
67 | singleton,
|
68 | eager
|
69 | } = this.options;
|
70 | return `consume-shared-module|${shareScope}|${shareKey}|${
|
71 | requiredVersion && rangeToString(requiredVersion)
|
72 | }|${strictVersion}|${importResolved}|${singleton}|${eager}`;
|
73 | }
|
74 |
|
75 | |
76 |
|
77 |
|
78 |
|
79 | readableIdentifier(requestShortener) {
|
80 | const {
|
81 | shareKey,
|
82 | shareScope,
|
83 | importResolved,
|
84 | requiredVersion,
|
85 | strictVersion,
|
86 | singleton,
|
87 | eager
|
88 | } = this.options;
|
89 | return `consume shared module (${shareScope}) ${shareKey}@${
|
90 | requiredVersion ? rangeToString(requiredVersion) : "*"
|
91 | }${strictVersion ? " (strict)" : ""}${singleton ? " (singleton)" : ""}${
|
92 | importResolved
|
93 | ? ` (fallback: ${requestShortener.shorten(importResolved)})`
|
94 | : ""
|
95 | }${eager ? " (eager)" : ""}`;
|
96 | }
|
97 |
|
98 | |
99 |
|
100 |
|
101 |
|
102 | libIdent(options) {
|
103 | const { shareKey, shareScope, import: request } = this.options;
|
104 | return `webpack/sharing/consume/${shareScope}/${shareKey}${
|
105 | request ? `/${request}` : ""
|
106 | }`;
|
107 | }
|
108 |
|
109 | |
110 |
|
111 |
|
112 |
|
113 |
|
114 | needBuild(context, callback) {
|
115 | callback(null, !this.buildInfo);
|
116 | }
|
117 |
|
118 | |
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 | build(options, compilation, resolver, fs, callback) {
|
127 | this.buildMeta = {};
|
128 | this.buildInfo = {};
|
129 | if (this.options.import) {
|
130 | const dep = new ConsumeSharedFallbackDependency(this.options.import);
|
131 | if (this.options.eager) {
|
132 | this.addDependency(dep);
|
133 | } else {
|
134 | const block = new AsyncDependenciesBlock({});
|
135 | block.addDependency(dep);
|
136 | this.addBlock(block);
|
137 | }
|
138 | }
|
139 | callback();
|
140 | }
|
141 |
|
142 | |
143 |
|
144 |
|
145 | getSourceTypes() {
|
146 | return TYPES;
|
147 | }
|
148 |
|
149 | |
150 |
|
151 |
|
152 |
|
153 | size(type) {
|
154 | return 42;
|
155 | }
|
156 |
|
157 | |
158 |
|
159 |
|
160 |
|
161 |
|
162 | updateHash(hash, context) {
|
163 | hash.update(JSON.stringify(this.options));
|
164 | super.updateHash(hash, context);
|
165 | }
|
166 |
|
167 | |
168 |
|
169 |
|
170 |
|
171 | codeGeneration({ chunkGraph, moduleGraph, runtimeTemplate }) {
|
172 | const runtimeRequirements = new Set([RuntimeGlobals.shareScopeMap]);
|
173 | const {
|
174 | shareScope,
|
175 | shareKey,
|
176 | strictVersion,
|
177 | requiredVersion,
|
178 | import: request,
|
179 | singleton,
|
180 | eager
|
181 | } = this.options;
|
182 | let fallbackCode;
|
183 | if (request) {
|
184 | if (eager) {
|
185 | const dep = this.dependencies[0];
|
186 | fallbackCode = runtimeTemplate.syncModuleFactory({
|
187 | dependency: dep,
|
188 | chunkGraph,
|
189 | runtimeRequirements,
|
190 | request: this.options.import
|
191 | });
|
192 | } else {
|
193 | const block = this.blocks[0];
|
194 | fallbackCode = runtimeTemplate.asyncModuleFactory({
|
195 | block,
|
196 | chunkGraph,
|
197 | runtimeRequirements,
|
198 | request: this.options.import
|
199 | });
|
200 | }
|
201 | }
|
202 | let fn = "load";
|
203 | const args = [JSON.stringify(shareScope), JSON.stringify(shareKey)];
|
204 | if (requiredVersion) {
|
205 | if (strictVersion) {
|
206 | fn += "Strict";
|
207 | }
|
208 | if (singleton) {
|
209 | fn += "Singleton";
|
210 | }
|
211 | args.push(stringifyHoley(requiredVersion));
|
212 | fn += "VersionCheck";
|
213 | }
|
214 | if (fallbackCode) {
|
215 | fn += "Fallback";
|
216 | args.push(fallbackCode);
|
217 | }
|
218 | const code = runtimeTemplate.returningFunction(`${fn}(${args.join(", ")})`);
|
219 | const sources = new Map();
|
220 | sources.set("consume-shared", new RawSource(code));
|
221 | return {
|
222 | runtimeRequirements,
|
223 | sources
|
224 | };
|
225 | }
|
226 |
|
227 | serialize(context) {
|
228 | const { write } = context;
|
229 | write(this.options);
|
230 | super.serialize(context);
|
231 | }
|
232 |
|
233 | deserialize(context) {
|
234 | const { read } = context;
|
235 | this.options = read();
|
236 | super.deserialize(context);
|
237 | }
|
238 | }
|
239 |
|
240 | makeSerializable(
|
241 | ConsumeSharedModule,
|
242 | "webpack/lib/sharing/ConsumeSharedModule"
|
243 | );
|
244 |
|
245 | module.exports = ConsumeSharedModule;
|