-
Notifications
You must be signed in to change notification settings - Fork 24
/
DojoAMDModuleFactoryPlugin.js
393 lines (370 loc) · 14.5 KB
/
DojoAMDModuleFactoryPlugin.js
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
* (C) Copyright HCL Technologies Ltd. 2018, 2019
* (C) Copyright IBM Corp. 2012, 2016 All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const {pluginName, getPluginProps} = require('./DojoAMDPlugin');
const path = require("path");
const querystring = require("querystring");
/*
* Initializes/updates/queries the absMid property on the specified object. absMid is
* a defined property with the following behaviors:
*
* 1. The property is associated with an array of objects with each object having a name
* and an isProvisional property. By convention, non-provisional entries occur
* before provisional entries in the array.
* 2. If the property is assigned a string value (e.g. data.absMid = "foo"), then
* an entry is added to the beginning of the array with the name property set to the
* assigned value and the isProvisional property set to false.
* 3. If the property value is referenced (e.g. let absMid = data.absMid), then the
* name property of the first array entry is provided.
* 4. If absMidsArray is provided to this function, then the value will replace the
* existing array associated with the property. This facilitates copying the
* property from one object to another. Note that the array is shallow copied, so
* changes to the provided array after it has been used to intialize an absMid
* property will not be reflected in the new property.
* 5. If this function is called and the data object already has an absMid property of
* type string and absMidArray is undefined, then it becomes the name of the only
* entry in the array with isProvisional set to false.
* 6. This function returns a reference to the array associated with the absMid
* property. Copying the property with it's associated data and behavior to another
* object is accomplished as follows:
* initAbsMidProp(target, initAbsMidProp(source));
*/
function initAbsMidProp(data, absMidsArray) {
if (absMidsArray) {
// If we're array data is provided, then delete the existing prop
delete data.absMid;
}
const existing = data.absMid;
if (existing && typeof existing === 'string') {
// Determine if the existing prop is one of ours or a plain old string
const control = {control: 'getAbsMids'};
data.absMid = control; // magic happens here if prop is one of ours
if (control.absMidsArray) {
// absMids property was assigned to control object, so already
// initialized. Just return the array. Note that we can't get
// here if absMidsArray was provided, so nothing else to do.
return control.absMidsArray;
} else {
// property was a plain old string. Restore the value so we can
// use it to intialized the array below.
data.absMid = existing;
}
}
const absMids = absMidsArray ? absMidsArray.slice() : [];
// Define property setter and getter
Object.defineProperty(data, 'absMid', {
configurable: true,
get() {
return absMids[0] && absMids[0].name;
},
set(name) {
// If assigned a control object with magic name and property, then
// add an 'absMidsArray' property to the control object.
if (typeof name === 'object' && name.control === 'getAbsMids') {
name.absMidsArray = absMids;
return;
}
if (!name || typeof name !== 'string') {
throw new Error(`Illegal absMid: ${name} must a non empty string.`);
}
absMids.unshift({name: name, isProvisional: false});
}
});
// If existing value of absMid property was a string,
// then add a non-provisional entry to the array.
if (existing && typeof existing === 'string') {
absMids.unshift({name: existing, isProvisional: false});
}
return absMids;
}
module.exports = class DojoAMDModuleFactoryPlugin {
constructor(options) {
this.options = options;
this._modules = new Map();
}
apply(compiler) {
this.compiler = compiler;
this.pluginProps = getPluginProps(compiler);
compiler.hooks.normalModuleFactory.tap(pluginName, factory => {
const context = Object.create(this, {factory: {value: factory}});
compiler.hooks.run.tapAsync(pluginName, this.run.bind(this));
compiler.hooks.watchRun.tapAsync(pluginName, this.run.bind(this));
factory.hooks.beforeResolve.tapAsync(pluginName, this.beforeResolve.bind(context));
factory.hooks.resolve.tapAsync(pluginName, this.resolver.bind(context));
if (Array.isArray(this.options.locales) && !this.options.locales.includes('*')) {
factory.hooks.afterResolve.tapAsync(pluginName, this.afterResolve.bind(context));
}
});
compiler.hooks.compilation.tap(pluginName, (compilation, params) => {
if (!this.options.isSkipCompilation(compilation)) {
const context = Object.create(this, {
compilation: {value: compilation},
factory: {value: params.normalModuleFactory}
});
params.normalModuleFactory.hooks.module.tap(pluginName, this.module.bind(context));
compilation.hooks.seal.tap(pluginName, this.trimAbsMids.bind(context));
compilation.hooks.buildModule.tap(pluginName, this.buildModule.bind(context));
}
});
}
run(__, callback) {
this._modules = new Map();
callback();
}
/*
* Returns true if the passed request is an absMid. If input is a string,
* then the input is split using '!' delimiter and each part is tested. if
* request is an array, then each element of the array is tested.
*/
isAbsMid(request) {
const parts = Array.isArray(request) ? request : request.split('!');
return parts.every(part => !/^[./\\]/.test(part) && !path.isAbsolute(part));
}
toAbsMid(request, issuerAbsMid, dojoRequire) {
var result = request;
if (request) {
const segments = [];
let context;
try {
// extract context path from issuerAbsMid (it might be a loader expression)
const parts = issuerAbsMid && issuerAbsMid.split('!') || [];
while (!context && parts.length) context = parts.pop();
if (context && context.indexOf('/') === -1) {
context = null;
}
request.split("!").forEach((segment) => {
segments.push(dojoRequire.toAbsMid(segment, context ? {mid: context} : null));
});
result = segments.join("!");
} catch (ignore) {
}
}
return result;
}
processAbsMidQueryArgs(data) {
// Parse the absMid query args from request and add them to the data
// object. Any such query args are also removed from the request.
const parts = data.request.split("!");
parts.forEach((part, i) => {
let idx = part.indexOf("?");
if (idx !== -1) {
let request = part.substring(0, idx);
let query = querystring.parse(part.substring(idx+1));
let absMids = query.absMid;
if (absMids) {
if (!Array.isArray(absMids)) {
absMids = [absMids];
}
absMids.forEach(absMid => {
absMid && this.addAbsMid(data, absMid);
});
delete query.absMid;
if (Object.keys(query).length) {
request = request + "?" + querystring.stringify(query);
}
parts[i] = request;
}
}
});
data.request = parts.join("!");
}
/*
* Adds an absMid alias for the module. Keeps non-provisional absMids
* ahead of provisional absMids in the array.
*/
addAbsMid(data, absMid, isProvisional) {
if (!absMid || typeof absMid !== 'string') {
throw new Error(`Illegal absMid: ${absMid} must a non empty string.`);
}
const absMids = initAbsMidProp(data);
const idx = absMids.findIndex(elem => elem.name === absMid);
if (idx !== -1) {
if (isProvisional && !absMids[idx].isProvisonal) {
return;
}
absMids.splice(idx, 1);
}
let insertIdx = 0;
if (isProvisional) {
insertIdx = absMids.findIndex(entry => entry.isProvisional);
if (insertIdx === -1) {
insertIdx = absMids.length;
}
}
absMids.splice(insertIdx, 0, {name: absMid, isProvisional: isProvisional});
}
/*
* Filters the absMids for a module
*/
filterAbsMids(data, callback) {
let toKeep = [];
const absMids = initAbsMidProp(data);
absMids.forEach(absMid => {
if (callback(absMid.name, absMid.isProvisional)) {
toKeep.push(absMid);
}
});
delete data.absMid;
toKeep.reverse().forEach(absMid => {
this.addAbsMid(data, absMid.name, absMid.isProvisional);
});
}
/*
* Removes absMids for non-Dojo modules in the compilation. Non-Dojo modules are
* modules which don't have the isAMD flag set, or modules to which non-provisional
* absMids have been added (e.g. loaders)
*/
trimAbsMids() {
this.compilation.modules.forEach(module => {
let shouldAdd = false;
this.filterAbsMids(module, (absMid__, isProvisional) => {
return shouldAdd = shouldAdd || module.isAMD || !isProvisional;
});
});
}
addAbsMidsFromRequest(data) {
/*
* Determines the absMid aliases for this module and adds them to the data object. An absMid can
* be derived from the request path, or from query args in the request.
* absMid aliases allow the module to be accessed at runtime using computed names who's values
* cannot be determined at build time.
*/
if (data) {
this.processAbsMidQueryArgs(data);
if (data.request.charAt(0) !== '!') {
let context;
if (data.dependencies) {
data.dependencies.some(dep => context = dep.issuerModule && dep.issuerModule.absMid);
}
let absMid = this.toAbsMid(data.request, context, this.pluginProps.dojoRequire);
data.request = absMid;
// If no remaining relative or absolute paths, then set absMid
if (this.isAbsMid(absMid)) {
this.addAbsMid(data, absMid, true);
}
}
}
}
beforeResolve(data, callback) {
const dep = data.dependencies && data.dependencies[0];
if (dep && dep.usingGlobalRequire && data.request.startsWith('.')) {
// Global require with relative path. Dojo resolves against the page.
// We'll resolve against the compiler context or config defined global context
const globalPath = this.options.getGlobalContext(this.compiler);
data.request = path.resolve(globalPath, data.request);
var relPath = path.relative(globalPath, data.request).replace(/\\/g,'/');
if (this.isAbsMid(relPath)) {
relPath = "./" + relPath;
}
this.addAbsMid(data, relPath, true);
}
this.addAbsMidsFromRequest(data, true);
return callback(null);
}
afterResolve(data, callback) {
if (/\/nls\/[^/]*\.js$/.test(data.createData.resource?.replace(/[/\\]/g, '/'))) {
data.createData.loaders.push({
loader: path.resolve(__dirname, "..", "loaders", "dojo", "i18nRootModifier"),
options: `bundledLocales=${this.options.locales.join('|')}`
});
}
callback();
}
resolver(data, callback) {
// Add absMids from the request. Note that we do it both here and in 'beforeResolve'
// so that we get the absMid aliases for both pre- and post- replaced module identifiers.
// This allows the same module to be referenced at runtime by either name
// (e.g. 'dojo/selector/_loader!default' and 'dojo/selector/lite').
this.addAbsMidsFromRequest(data);
data.contextInfo.originalRequest = data.request;
const resolveFn = this.factory.hooks.resolve.taps.find(elem => elem.name === 'NormalModuleFactory').fn;
return resolveFn(data, (err) => {
if (err && data.originalRequest) {
data.request = data.originalRequest;
}
callback(err ? false : undefined);
});
}
/*
* Last chance to add absMid before module is built. We may succeed here
* where we failed previously now that the issuer module is available (it
* may have an absMid we can use for the context).
*/
buildModule(module) {
// Need to reset this for watch mode so require calls prior to the define function
// will be treated as cjs require when a module is re-compiled.
delete module.isAMD;
if (module.absMid || !module.originalRequest) return;
const dojoRequire = this.pluginProps.dojoRequire;
const issuer = this.compilation.moduleGraph.getIssuer(module);
const issuerAbsMid = issuer && issuer.absMid;
let absMid = this.toAbsMid(module.originalRequest, issuerAbsMid, dojoRequire);
// Any remaining relative paths, try to resolve against baseUrl
const parts = absMid.split('!')
.map(part => {
if (part.charAt(0) === '.') {
const res = module.resource.replace(/\.jsx?$/, '');
if (path.resolve(dojoRequire.baseUrl, part) === res) {
var relative = path.relative(dojoRequire.baseUrl, res).replace(/[\\]/g, '/');
if (relative.indexOf('/') !== -1) {
return relative;
}
}
}
return part;
});
// If no remaining relative or absolute paths, then set absMid
if (this.isAbsMid(parts)) {
this.addAbsMid(module, parts.join('!'), true);
}
}
module(module, createData__, resolveData) {
if (!this._modules.get(this.compilation)) {
this._modules.set(this.compilation, {});
}
const absMids = initAbsMidProp(resolveData);
if (absMids.length) {
initAbsMidProp(module, absMids);
}
module.originalRequest = resolveData.contextInfo?.originalRequest;
// If the module already exists in the compilation, then copy the absMid data from
// this module to the existing module since this module will be discarded.
// Note: don't use initAbsMidProp as it would destroy the exising module's absMids.
const existing = this._modules.get(this.compilation)[module.request];
if (existing) {
this.filterAbsMids(module, (absMid, isProvisional) => {
this.addAbsMid(existing, absMid, isProvisional);
return true;
});
return existing;
}
// Add functions to the module object for adding/filtering absMids (for use by loaders)
module.addAbsMid = (absMid, isProvisional) => {
if (absMid) {
this.addAbsMid(module, absMid, isProvisional);
} else {
// This has the effect of making the index 0 absMid non-provisional,
// assuring that this module's absMids will be exported to the client.
module.absMid = module.absMid;
}
};
module.filterAbsMids = callback => {
this.filterAbsMids(module, callback);
};
this._modules.get(this.compilation)[module.request] = module;
return module;
}
};