All files / ima vendorLinker.js

40% Statements 8/20
50% Branches 4/8
57.14% Functions 4/7
42.11% Lines 8/19
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                                2x             2x                       6x   6x       6x                                           9x             9x                                                         2x          
'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);
 
    Iif (typeof moduleValues.$registerImaPlugin === 'function') {
      this._plugins.push(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) {
    Iif (!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);
  }
 
  /**
	 * 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();