Skip to content
Luc Claustres edited this page Dec 22, 2015 · 3 revisions

This small service in core is responsible to inject all required dependencies for a module, you will notice a 'hard-coded' use case when installing dependencies in bower_components in order to avoid too much long pathes :

'use strict';

var _ = require('lodash');
var winston = require('winston');
var path =  require('path');

/**
 * @ngdoc object
 * @name core.server.AssetsManager
 * @param {String} moduleName the name of the module we want to retrieve assets from
 * @description Use this object to retrieve the assets from an assets.json file of a module
 * and declare what needs to be aggregated in the index page (CSS, JS, AngularJS dependencies)
 **/
var AssetsManager = module.exports = function (assets, moduleName) {
    this.moduleName = moduleName;
    this.moduleAssets = assets[moduleName];

    if ( !this.moduleAssets ) {
        winston.log('error', 'Malformed assets.json for module ' + moduleName);
    }
};

/**
 * @ngdoc function
 * @name getCssFiles
 * @methodOf core.server.AssetsManager
 * @returns {array} the list of CSS files to be included (relative to root directory)
 */
AssetsManager.prototype.getCssFiles = function () {
    var css = this.moduleAssets.css;
    if ( !css ) {
        winston.log('error', 'Malformed css part in assets.json for module ' + this.moduleName);
    }
    var keys = Object.keys(css);
    if ( !keys || !keys.length ) {
        winston.log('error', 'Malformed css part in assets.json for module ' + this.moduleName);
    }

    return css[keys[0]];
};

/**
 * @ngdoc function
 * @name getJsFiles
 * @methodOf core.server.AssetsManager
 * @returns {array} the list of JS files to be included (relative to root directory)
 */
AssetsManager.prototype.getJsFiles = function () {
    var js = this.moduleAssets.js;
    if ( !js ) {
        winston.log('error', 'Malformed js part in assets.json for module ' + this.moduleName);
    }
    var keys = Object.keys(js);
    if ( !keys || !keys.length ) {
        winston.log('error', 'Malformed js part in assets.json for module ' + this.moduleName);
    }

    return js[keys[0]];
};

/**
 * @ngdoc function
 * @name getAngularDependencies
 * @methodOf core.server.AssetsManager
 * @returns {array} the list of AngularJS modules to be used as dependencies
 */
AssetsManager.prototype.getAngularDependencies = function () {
    if ( !this.moduleAssets.angularDependencies ) {
        winston.log('error', 'Malformed angular dependencies part in assets.json for module ' + this.moduleName);
    }
    return this.moduleAssets.angularDependencies;
};

/**
 * @ngdoc function
 * @name aggregateAssets
 * @methodOf core.server.AssetsManager
 * @param {module} module the MEAN.io module object we want to aggregate assets in (CSS, JS and AngularJS)
*/
AssetsManager.prototype.aggregateAssets = function (module) {
    module.angularDependencies(this.getAngularDependencies());
    _.forEach(this.getCssFiles(), function(filepath) {
        // Pathes are by default relative to public/assets and we install all bower dependencies at the top level to avoid clashes
        if ( filepath.indexOf('bower_components') === 0 ) {
            module.aggregateAsset('css', '../../../../../../' + filepath);
        } // Otherwise as we use a path relative to the top-level dir for phonegap we have to simply extract the basename for local module dependencies
        else {
            module.aggregateAsset('css', path.basename(filepath));
        }
    });
    var weight = -999;
    _.forEach(this.getJsFiles(), function(filepath) {
        // Pathes are relative to public/assets and we install all bower dependencies at the top level to avoid clashes
        if ( filepath.indexOf('bower_components') === 0 ) {
            module.aggregateAsset('js', '../../../../../../' + filepath, {global: true, weight: weight});
        }  // Otherwise as we use a path relative to the top-level dir for phonegap we have to simply extract the basename for local module dependencies
        else {
            module.aggregateAsset('js', path.basename(filepath));
        }
        weight++;
    });
};
Clone this wiki locally