<% @identifier or= 'require' %>
(function(/*! Stitch !*/) {
  if (!this.<%= @identifier %>) {
    var modules = {}, cache = {}, require = function(name, root) {
      var path = expand(root, name), indexPath = expand(path, './index'), module, fn;
      module   = cache[path] || cache[indexPath]
      if (module) {
        return module.exports;
      } else if (fn = modules[path] || modules[path = indexPath]) {
        module = {id: path, exports: {}};
        try {
          cache[path] = module;
          fn(module.exports, function(name) {
            return require(name, dirname(path));
          }, module);
          return module.exports;
        } catch (err) {
          delete cache[path];
          throw err;
        }
      } else {
        throw 'module \'' + name + '\' not found';
      }
    }, expand = function(root, name) {
      var results = [], parts, part;
      if (/^\.\.?(\/|$)/.test(name)) {
        parts = [root, name].join('/').split('/');
      } else {
        parts = name.split('/');
      }
      for (var i = 0, length = parts.length; i < length; i++) {
        part = parts[i];
        if (part == '..') {
          results.pop();
        } else if (part != '.' && part != '') {
          results.push(part);
        }
      }
      return results.join('/');
    }, dirname = function(path) {
      return path.split('/').slice(0, -1).join('/');
    };
    this.<%= @identifier %> = function(name) {
      return require(name, '');
    }
    this.<%= @identifier %>.define = function(bundle) {
      for (var key in bundle)
        modules[key] = bundle[key];
    };
    this.<%= @identifier %>.modules = modules;
    this.<%= @identifier %>.cache   = cache;
  }
  return this.<%= @identifier %>.define;
}).call(this)({
  <%- (JSON.stringify(module.id) + ": function(exports, require, module) {#{module.compile()}}" for module in @modules).join(', ') %>
});


  var moduleList = [
    <% for module in @modules: %>
      <% moduleId = module.componentInfo?.namespace + "/" + module.componentInfo?.name + "/" + module.componentInfo?.index  %>
       <% if moduleId == module.id: %>
        {"path":"<%= module.id %>","iconColor": "<%= module.componentInfo.iconColor %>","iconLabel": "<%= module.componentInfo.iconLabel %>","name": "<%= module.componentInfo.name %>" },
        <% end %>
    <% end %>
    ]

  <% if @styleModules : %>
    //CSS Styles for Modules
    var css="<%- @styleModules %>";
    var head  = document.head || document.getElementsByTagName('head')[0];
    var style = document.createElement('style');

    style.type = 'text/css';

    if (style.styleSheet) {
      style.styleSheet.cssText = css;
    }
    else {
      style.appendChild(document.createTextNode(css));
    }

    head.appendChild(style);
  
   
  <% end %>