This repository has been archived by the owner on Jun 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathvendorLinker.js
116 lines (102 loc) · 2.93 KB
/
vendorLinker.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
'use strict';
/**
* Utility for linking vendor node modules with the application by exporting
* them to the IMA loader's modules.
*/
export class VendorLinker {
/**
* Initializes the vendor linker.
*/
constructor() {
/**
* Internal storage of loaded modules.
*
* @type {Map<string, Object<string, *>>}
*/
this._modules = new Map();
/**
* Internal storage of loaded IMA plugins.
*
* @type {Object<string, *>[]}
*/
this._plugins = [];
}
/**
* Sets the provided vendor node module to the internal registry of this
* vendor linker, and registers an IMA loader module of the same name,
* exporting the same values.
*
* @param {string} moduleName The name of the module.
* @param {Object<string, *>} moduleValues Values exported from the module.
*/
set(moduleName, moduleValues) {
this._modules.set(moduleName, moduleValues);
if (typeof moduleValues.$registerImaPlugin === 'function') {
this._plugins.push({ name: moduleName, module: moduleValues });
}
$IMA.Loader.register(moduleName, [], exports => ({
setters: [],
execute: () => {
// commonjs module compatibility
exports('default', moduleValues);
// ES2015 module compatibility
for (let key of Object.keys(moduleValues)) {
exports(key, moduleValues[key]);
}
}
}));
}
/**
* Returns the provided vendor node module from the internal registry of this
* vendor linker.
*
* @param {string} moduleName The name of the module.
* @param {?boolean} [imaInternalModule]
* @return {Object<string, *>} moduleValues Values exported from the module.
*/
get(moduleName, imaInternalModule) {
if (!this._modules.has(moduleName) && !imaInternalModule) {
throw new Error(
`The module '${moduleName}' is not registered.` +
`Add the module to vendors in build.js`
);
}
return this._modules.get(moduleName);
}
/**
* Clears all loaded modules and plugins from this vendor linker.
*
* @return {VendorLinker} This vendor linker.
*/
clear() {
this._modules.clear();
this._plugins = [];
return this;
}
/**
* Binds the vendor modules loaded in this vendor linker to the
* {@code Vendor} sub-namespace of the provided namespace.
*
* @param {Namespace} ns The namespace to which the vendor modules should
* be bound.
*/
bindToNamespace(ns) {
let nsVendor = ns.namespace('vendor');
for (let name of this._modules.keys()) {
let lib = this._modules.get(name);
if (typeof lib.$registerImaPlugin === 'function') {
lib.$registerImaPlugin(ns);
}
nsVendor[name] = lib;
}
}
/**
* Returns the loaded IMA plugins as an array of export objects.
*
* @return {Array<Object<string, *>>} The loaded IMA plugins.
*/
getImaPlugins() {
return this._plugins;
}
}
export default new VendorLinker();