Skip to content

Instantly share code, notes, and snippets.

@dannymidnight
Last active August 29, 2015 14:03
Show Gist options
  • Save dannymidnight/1daac4791eb4acc2ee43 to your computer and use it in GitHub Desktop.
Save dannymidnight/1daac4791eb4acc2ee43 to your computer and use it in GitHub Desktop.
RequireJS dependency helper
var madge = require('madge'),
_ = require('underscore');
function loadTargets (graph, depends, targets) {
var parents = [],
modules = [];
depends.forEach(function(dep) {
parents = _.uniq(parents.concat(graph.depends(dep)));
});
if (parents.length) {
modules = _.difference(parents, Object.keys(targets));
parents = _.uniq(
parents.concat(
parents,
loadTargets(graph, modules, targets)
)
);
}
return parents;
};
module.exports = {
//
// Find a list of RequireJS targets that have a dependency
// on a given module.
//
// :param name string: Module name
// :param targets Array: List of RequireJS targets.
// :param options object
//
findTargetsWithDependency: function(name, targets, options) {
// Fetch dependency graph
var dependencyObject = madge(options.path, {
requireConfig: options.requireConfig,
format: options.format
});
var deps = loadTargets(dependencyObject, [name], targets);
return _.uniq(deps.concat(name));
}
};
@dannymidnight
Copy link
Author

Here are the requirejs targets

grunt.initConfig({
    requirejs: (function() {
      var targets = {};

      grunt.file.expand({cwd: 'static/js'}, 'pages/*.js')
        .forEach(function(file) {
          var name = file.replace('.js', '')
          targets[name] = {
            options: {
              optimize: "none",
              baseUrl: "./static/js",
              mainConfigFile: "static/js/main.js",
              include: ['../components/almond/almond.js', file],
              out: assets + "/dist/js/" + file
            }
          }
        });

      return targets;
    }())
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment