Last active
August 29, 2015 14:03
-
-
Save dannymidnight/1daac4791eb4acc2ee43 to your computer and use it in GitHub Desktop.
RequireJS dependency helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
}; |
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
How to hook this into a
Grunfile.js