ProvideSharedPlugin.js
6.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra and Zackary Jackson @ScriptedAlchemy
*/
"use strict";
const WebpackError = require("../WebpackError");
const { parseOptions } = require("../container/options");
const createSchemaValidation = require("../util/create-schema-validation");
const ProvideForSharedDependency = require("./ProvideForSharedDependency");
const ProvideSharedDependency = require("./ProvideSharedDependency");
const ProvideSharedModuleFactory = require("./ProvideSharedModuleFactory");
/** @typedef {import("../../declarations/plugins/sharing/ProvideSharedPlugin").ProvideSharedPluginOptions} ProvideSharedPluginOptions */
/** @typedef {import("../Compilation")} Compilation */
/** @typedef {import("../Compiler")} Compiler */
const validate = createSchemaValidation(
require("../../schemas/plugins/sharing/ProvideSharedPlugin.check.js"),
() => require("../../schemas/plugins/sharing/ProvideSharedPlugin.json"),
{
name: "Provide Shared Plugin",
baseDataPath: "options"
}
);
/**
* @typedef {Object} ProvideOptions
* @property {string} shareKey
* @property {string} shareScope
* @property {string | undefined | false} version
* @property {boolean} eager
*/
/** @typedef {Map<string, { config: ProvideOptions, version: string | undefined | false }>} ResolvedProvideMap */
class ProvideSharedPlugin {
/**
* @param {ProvideSharedPluginOptions} options options
*/
constructor(options) {
validate(options);
/** @type {[string, ProvideOptions][]} */
this._provides = parseOptions(
options.provides,
item => {
if (Array.isArray(item))
throw new Error("Unexpected array of provides");
/** @type {ProvideOptions} */
const result = {
shareKey: item,
version: undefined,
shareScope: options.shareScope || "default",
eager: false
};
return result;
},
item => ({
shareKey: item.shareKey,
version: item.version,
shareScope: item.shareScope || options.shareScope || "default",
eager: !!item.eager
})
);
this._provides.sort(([a], [b]) => {
if (a < b) return -1;
if (b < a) return 1;
return 0;
});
}
/**
* Apply the plugin
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
/** @type {WeakMap<Compilation, ResolvedProvideMap>} */
const compilationData = new WeakMap();
compiler.hooks.compilation.tap(
"ProvideSharedPlugin",
(compilation, { normalModuleFactory }) => {
/** @type {ResolvedProvideMap} */
const resolvedProvideMap = new Map();
/** @type {Map<string, ProvideOptions>} */
const matchProvides = new Map();
/** @type {Map<string, ProvideOptions>} */
const prefixMatchProvides = new Map();
for (const [request, config] of this._provides) {
if (/^(\/|[A-Za-z]:\\|\\\\|\.\.?(\/|$))/.test(request)) {
// relative request
resolvedProvideMap.set(request, {
config,
version: config.version
});
} else if (/^(\/|[A-Za-z]:\\|\\\\)/.test(request)) {
// absolute path
resolvedProvideMap.set(request, {
config,
version: config.version
});
} else if (request.endsWith("/")) {
// module request prefix
prefixMatchProvides.set(request, config);
} else {
// module request
matchProvides.set(request, config);
}
}
compilationData.set(compilation, resolvedProvideMap);
const provideSharedModule = (
key,
config,
resource,
resourceResolveData
) => {
let version = config.version;
if (version === undefined) {
let details = "";
if (!resourceResolveData) {
details = `No resolve data provided from resolver.`;
} else {
const descriptionFileData =
resourceResolveData.descriptionFileData;
if (!descriptionFileData) {
details =
"No description file (usually package.json) found. Add description file with name and version, or manually specify version in shared config.";
} else if (!descriptionFileData.version) {
details = `No version in description file (usually package.json). Add version to description file ${resourceResolveData.descriptionFilePath}, or manually specify version in shared config.`;
} else {
version = descriptionFileData.version;
}
}
if (!version) {
const error = new WebpackError(
`No version specified and unable to automatically determine one. ${details}`
);
error.file = `shared module ${key} -> ${resource}`;
compilation.warnings.push(error);
}
}
resolvedProvideMap.set(resource, {
config,
version
});
};
normalModuleFactory.hooks.module.tap(
"ProvideSharedPlugin",
(module, { resource, resourceResolveData }, resolveData) => {
if (resolvedProvideMap.has(resource)) {
return module;
}
const { request } = resolveData;
{
const config = matchProvides.get(request);
if (config !== undefined) {
provideSharedModule(
request,
config,
resource,
resourceResolveData
);
resolveData.cacheable = false;
}
}
for (const [prefix, config] of prefixMatchProvides) {
if (request.startsWith(prefix)) {
const remainder = request.slice(prefix.length);
provideSharedModule(
resource,
{
...config,
shareKey: config.shareKey + remainder
},
resource,
resourceResolveData
);
resolveData.cacheable = false;
}
}
return module;
}
);
}
);
compiler.hooks.finishMake.tapPromise("ProvideSharedPlugin", compilation => {
const resolvedProvideMap = compilationData.get(compilation);
if (!resolvedProvideMap) return Promise.resolve();
return Promise.all(
Array.from(
resolvedProvideMap,
([resource, { config, version }]) =>
new Promise((resolve, reject) => {
compilation.addInclude(
compiler.context,
new ProvideSharedDependency(
config.shareScope,
config.shareKey,
version || false,
resource,
config.eager
),
{
name: undefined
},
err => {
if (err) return reject(err);
resolve();
}
);
})
)
).then(() => {});
});
compiler.hooks.compilation.tap(
"ProvideSharedPlugin",
(compilation, { normalModuleFactory }) => {
compilation.dependencyFactories.set(
ProvideForSharedDependency,
normalModuleFactory
);
compilation.dependencyFactories.set(
ProvideSharedDependency,
new ProvideSharedModuleFactory()
);
}
);
}
}
module.exports = ProvideSharedPlugin;