var combine = require('./jsCombine'),
    fs = require('fs'),
    templateUtil = require('./templateUtil'),
    watch = require('./watcher.js');

function loadConfig(path) {
    return JSON.parse(fs.readFileSync(path, 'utf8'));
}
function loadList(config) {
    var moduleList = [];
    for (var name in config.modules) {
        moduleList.push(name);
    }
    return moduleList;
}
function explodeViews(module, config) {
    var views = config.views,
        loadedTemplates = {},
        ret = [],

        files = module.controller ? module.support : module;

    for (var i = 0, len = files.length; i < len; i++) {
        var resource = files[i];
        ret.push(resource);

        if (views[resource]) {
            views[resource].forEach(function(template) {
                var generator = function(callback) {
                    templateUtil.loadTemplate(template, config.templateCache, callback);
                };
                generator.sourceFile = template;
                ret.push(generator);
            });
        }
    }

    // Generate the controller if we have the info for it
    if (module.controller) {
      var controllerGen = function(callback) {
        templateUtil.loadController(module.controller, module.routes, callback);
      };
      controllerGen.sourceFile = module.controller;
      ret.push(controllerGen);
    }

    return ret;
}

exports.init = function(configFile, outdir) {
    var config = loadConfig(configFile),
        moduleList = loadList(config);

    return {
        build: function(moduleName, callback) {
            var modules = moduleName ? [moduleName] : moduleList;
            modules.forEach(function(name) {
                var resources = explodeViews(config.modules[name], config),
                    moduleName = outdir + '/' + name + '.js';
                combine.combine(resources, moduleName, callback);
            });
        },
        watch: function(moduleName, callback) {
            var modules = moduleName ? [moduleName] : moduleList,
                self = this;

            // Watch for changes in the config file
            // WARN : This isn't really safe for multiple executions, this is a concious design decision
            watch.watchFile(configFile, [], function() {
                // Reload our config
                config = loadConfig(configFile);
                moduleList = loadList(config);

                // Cleanup what we can, breaking things along the way
                watch.unwatchAll();
                self.watch(moduleName, callback);
            });

            // Watch the individual components
            modules.forEach(function(name) {
                self.build(name, function(err, status) {
                    if (err) {
                        throw err;
                    }

                    callback(err, status);

                    watch.watchFile("foo" + status.fileName + "foo", status.input, function() {
                        self.build(name, callback);
                    });
                });
            });
        }
    };
};

@@ -647,16 +647,677 @@
 les%5Bi%5D;%0A
+        if (resource === 'module_map.json') %7B%0A            var buildModuleMap = function(callback) %7B%0A                var map = %7B%7D;%0A                for (var name in config.modules) %7B%0A                    var module = config.modules%5Bname%5D;%0A                    if (module.routes) %7B%0A                        for (var route in module.routes) %7B%0A                            map%5Broute%5D = name;%0A                        %7D%0A                    %7D%0A                %7D%0A%0A                templateUtil.loadModuleMap(map, config.moduleMap, callback);%0A            %7D;%0A            buildModuleMap.sourceFile = 'module_map.json';%0A%0A            ret.push(buildModuleMap);%0A        %7D else %7B%0A    
         
@@ -1335,16 +1335,26 @@
 source);
+%0A        %7D
 %0A%0A      
@@ -3521,24 +3521,143 @@
 , status);%0A%0A
+                    if (status.fileName === 'module_map.json') %7B%0A                        return;%0A                    %7D%0A
             

Add support for module_map.json virtual file.