1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const AsyncDependenciesBlock = require("../AsyncDependenciesBlock");
|
9 | const Module = require("../Module");
|
10 | const RuntimeGlobals = require("../RuntimeGlobals");
|
11 | const makeSerializable = require("../util/makeSerializable");
|
12 | const ProvideForSharedDependency = require("./ProvideForSharedDependency");
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | const TYPES = new Set(["share-init"]);
|
30 |
|
31 | class ProvideSharedModule extends Module {
|
32 | |
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | constructor(shareScope, name, version, request, eager) {
|
40 | super("provide-module");
|
41 | this._shareScope = shareScope;
|
42 | this._name = name;
|
43 | this._version = version;
|
44 | this._request = request;
|
45 | this._eager = eager;
|
46 | }
|
47 |
|
48 | |
49 |
|
50 |
|
51 | identifier() {
|
52 | return `provide module (${this._shareScope}) ${this._name}@${this._version} = ${this._request}`;
|
53 | }
|
54 |
|
55 | |
56 |
|
57 |
|
58 |
|
59 | readableIdentifier(requestShortener) {
|
60 | return `provide shared module (${this._shareScope}) ${this._name}@${
|
61 | this._version
|
62 | } = ${requestShortener.shorten(this._request)}`;
|
63 | }
|
64 |
|
65 | |
66 |
|
67 |
|
68 |
|
69 | libIdent(options) {
|
70 | return `webpack/sharing/provide/${this._shareScope}/${this._name}`;
|
71 | }
|
72 |
|
73 | |
74 |
|
75 |
|
76 |
|
77 |
|
78 | needBuild(context, callback) {
|
79 | callback(null, !this.buildInfo);
|
80 | }
|
81 |
|
82 | |
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | build(options, compilation, resolver, fs, callback) {
|
91 | this.buildMeta = {};
|
92 | this.buildInfo = {
|
93 | strict: true
|
94 | };
|
95 |
|
96 | this.clearDependenciesAndBlocks();
|
97 | const dep = new ProvideForSharedDependency(this._request);
|
98 | if (this._eager) {
|
99 | this.addDependency(dep);
|
100 | } else {
|
101 | const block = new AsyncDependenciesBlock({});
|
102 | block.addDependency(dep);
|
103 | this.addBlock(block);
|
104 | }
|
105 |
|
106 | callback();
|
107 | }
|
108 |
|
109 | |
110 |
|
111 |
|
112 |
|
113 | size(type) {
|
114 | return 42;
|
115 | }
|
116 |
|
117 | |
118 |
|
119 |
|
120 | getSourceTypes() {
|
121 | return TYPES;
|
122 | }
|
123 |
|
124 | |
125 |
|
126 |
|
127 |
|
128 | codeGeneration({ runtimeTemplate, moduleGraph, chunkGraph }) {
|
129 | const runtimeRequirements = new Set([RuntimeGlobals.initializeSharing]);
|
130 | const code = `register(${JSON.stringify(this._name)}, ${JSON.stringify(
|
131 | this._version || "0"
|
132 | )}, ${
|
133 | this._eager
|
134 | ? runtimeTemplate.syncModuleFactory({
|
135 | dependency: this.dependencies[0],
|
136 | chunkGraph,
|
137 | request: this._request,
|
138 | runtimeRequirements
|
139 | })
|
140 | : runtimeTemplate.asyncModuleFactory({
|
141 | block: this.blocks[0],
|
142 | chunkGraph,
|
143 | request: this._request,
|
144 | runtimeRequirements
|
145 | })
|
146 | }${this._eager ? ", 1" : ""});`;
|
147 | const sources = new Map();
|
148 | const data = new Map();
|
149 | data.set("share-init", [
|
150 | {
|
151 | shareScope: this._shareScope,
|
152 | initStage: 10,
|
153 | init: code
|
154 | }
|
155 | ]);
|
156 | return { sources, data, runtimeRequirements };
|
157 | }
|
158 |
|
159 | serialize(context) {
|
160 | const { write } = context;
|
161 | write(this._shareScope);
|
162 | write(this._name);
|
163 | write(this._version);
|
164 | write(this._request);
|
165 | write(this._eager);
|
166 | super.serialize(context);
|
167 | }
|
168 |
|
169 | static deserialize(context) {
|
170 | const { read } = context;
|
171 | const obj = new ProvideSharedModule(read(), read(), read(), read(), read());
|
172 | obj.deserialize(context);
|
173 | return obj;
|
174 | }
|
175 | }
|
176 |
|
177 | makeSerializable(
|
178 | ProvideSharedModule,
|
179 | "webpack/lib/sharing/ProvideSharedModule"
|
180 | );
|
181 |
|
182 | module.exports = ProvideSharedModule;
|