UNPKG

3.14 kBJavaScriptView Raw
1'use strict';
2
3/**
4 * Utility for linking vendor node modules with the application by exporting
5 * them to the IMA loader's modules.
6 */
7
8Object.defineProperty(exports, "__esModule", {
9 value: true
10});
11class VendorLinker {
12 /**
13 * Initializes the vendor linker.
14 */
15 constructor() {
16 /**
17 * Internal storage of loaded modules.
18 *
19 * @type {Map<string, Object<string, *>>}
20 */
21 this._modules = new Map();
22
23 /**
24 * Internal storage of loaded IMA plugins.
25 *
26 * @type {Object<string, *>[]}
27 */
28 this._plugins = [];
29 }
30
31 /**
32 * Sets the provided vendor node module to the internal registry of this
33 * vendor linker, and registers an IMA loader module of the same name,
34 * exporting the same values.
35 *
36 * @param {string} moduleName The name of the module.
37 * @param {Object<string, *>} moduleValues Values exported from the module.
38 */
39 set(moduleName, moduleValues) {
40 this._modules.set(moduleName, moduleValues);
41
42 if (typeof moduleValues.$registerImaPlugin === 'function') {
43 this._plugins.push(moduleValues);
44 }
45
46 $IMA.Loader.register(moduleName, [], exports => ({
47 setters: [],
48 execute: () => {
49 // commonjs module compatibility
50 exports('default', moduleValues);
51 // ES2015 module compatibility
52 for (let key of Object.keys(moduleValues)) {
53 exports(key, moduleValues[key]);
54 }
55 }
56 }));
57 }
58
59 /**
60 * Returns the provided vendor node module from the internal registry of this
61 * vendor linker.
62 *
63 * @param {string} moduleName The name of the module.
64 * @param {?boolean} [imaInternalModule]
65 * @return {Object<string, *>} moduleValues Values exported from the module.
66 */
67 get(moduleName, imaInternalModule) {
68 if (!this._modules.has(moduleName) && !imaInternalModule) {
69 throw new Error(`The module '${moduleName}' is not registered.` + `Add the module to vendors in build.js`);
70 }
71
72 return this._modules.get(moduleName);
73 }
74
75 /**
76 * Binds the vendor modules loaded in this vendor linker to the
77 * {@code Vendor} sub-namespace of the provided namespace.
78 *
79 * @param {Namespace} ns The namespace to which the vendor modules should
80 * be bound.
81 */
82 bindToNamespace(ns) {
83 let nsVendor = ns.namespace('vendor');
84 for (let name of this._modules.keys()) {
85 let lib = this._modules.get(name);
86
87 if (typeof lib.$registerImaPlugin === 'function') {
88 lib.$registerImaPlugin(ns);
89 }
90
91 nsVendor[name] = lib;
92 }
93 }
94
95 /**
96 * Returns the loaded IMA plugins as an array of export objects.
97 *
98 * @return {Array<Object<string, *>>} The loaded IMA plugins.
99 */
100 getImaPlugins() {
101 return this._plugins;
102 }
103}
104
105exports.VendorLinker = VendorLinker;
106exports.default = new VendorLinker();
107
108typeof $IMA !== 'undefined' && $IMA !== null && $IMA.Loader && $IMA.Loader.register('ima/vendorLinker', [], function (_export, _context) {
109 'use strict';
110 return {
111 setters: [],
112 execute: function () {
113 _export('VendorLinker', exports.VendorLinker);
114 _export('default', exports.default);
115 }
116 };
117});