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 `${
|
105 | this.layer ? `(${this.layer})/` : ""
|
106 | }webpack/sharing/consume/${shareScope}/${shareKey}${
|
107 | request ? `/${request}` : ""
|
108 | }`;
|
109 | }
|
110 |
|
111 | |
112 |
|
113 |
|
114 |
|
115 |
|
116 | needBuild(context, callback) {
|
117 | callback(null, !this.buildInfo);
|
118 | }
|
119 |
|
120 | |
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 | build(options, compilation, resolver, fs, callback) {
|
129 | this.buildMeta = {};
|
130 | this.buildInfo = {};
|
131 | if (this.options.import) {
|
132 | const dep = new ConsumeSharedFallbackDependency(this.options.import);
|
133 | if (this.options.eager) {
|
134 | this.addDependency(dep);
|
135 | } else {
|
136 | const block = new AsyncDependenciesBlock({});
|
137 | block.addDependency(dep);
|
138 | this.addBlock(block);
|
139 | }
|
140 | }
|
141 | callback();
|
142 | }
|
143 |
|
144 | |
145 |
|
146 |
|
147 | getSourceTypes() {
|
148 | return TYPES;
|
149 | }
|
150 |
|
151 | |
152 |
|
153 |
|
154 |
|
155 | size(type) {
|
156 | return 42;
|
157 | }
|
158 |
|
159 | |
160 |
|
161 |
|
162 |
|
163 |
|
164 | updateHash(hash, context) {
|
165 | hash.update(JSON.stringify(this.options));
|
166 | super.updateHash(hash, context);
|
167 | }
|
168 |
|
169 | |
170 |
|
171 |
|
172 |
|
173 | codeGeneration({ chunkGraph, moduleGraph, runtimeTemplate }) {
|
174 | const runtimeRequirements = new Set([RuntimeGlobals.shareScopeMap]);
|
175 | const {
|
176 | shareScope,
|
177 | shareKey,
|
178 | strictVersion,
|
179 | requiredVersion,
|
180 | import: request,
|
181 | singleton,
|
182 | eager
|
183 | } = this.options;
|
184 | let fallbackCode;
|
185 | if (request) {
|
186 | if (eager) {
|
187 | const dep = this.dependencies[0];
|
188 | fallbackCode = runtimeTemplate.syncModuleFactory({
|
189 | dependency: dep,
|
190 | chunkGraph,
|
191 | runtimeRequirements,
|
192 | request: this.options.import
|
193 | });
|
194 | } else {
|
195 | const block = this.blocks[0];
|
196 | fallbackCode = runtimeTemplate.asyncModuleFactory({
|
197 | block,
|
198 | chunkGraph,
|
199 | runtimeRequirements,
|
200 | request: this.options.import
|
201 | });
|
202 | }
|
203 | }
|
204 | let fn = "load";
|
205 | const args = [JSON.stringify(shareScope), JSON.stringify(shareKey)];
|
206 | if (requiredVersion) {
|
207 | if (strictVersion) {
|
208 | fn += "Strict";
|
209 | }
|
210 | if (singleton) {
|
211 | fn += "Singleton";
|
212 | }
|
213 | args.push(stringifyHoley(requiredVersion));
|
214 | fn += "VersionCheck";
|
215 | } else {
|
216 | if (singleton) {
|
217 | fn += "Singleton";
|
218 | }
|
219 | }
|
220 | if (fallbackCode) {
|
221 | fn += "Fallback";
|
222 | args.push(fallbackCode);
|
223 | }
|
224 | const code = runtimeTemplate.returningFunction(`${fn}(${args.join(", ")})`);
|
225 | const sources = new Map();
|
226 | sources.set("consume-shared", new RawSource(code));
|
227 | return {
|
228 | runtimeRequirements,
|
229 | sources
|
230 | };
|
231 | }
|
232 |
|
233 | serialize(context) {
|
234 | const { write } = context;
|
235 | write(this.options);
|
236 | super.serialize(context);
|
237 | }
|
238 |
|
239 | deserialize(context) {
|
240 | const { read } = context;
|
241 | this.options = read();
|
242 | super.deserialize(context);
|
243 | }
|
244 | }
|
245 |
|
246 | makeSerializable(
|
247 | ConsumeSharedModule,
|
248 | "webpack/lib/sharing/ConsumeSharedModule"
|
249 | );
|
250 |
|
251 | module.exports = ConsumeSharedModule;
|