Dependency Management Got Awesome
CommonJS and AMD Compliant dependency loader for modern web apps
Inject.addContentRule
0.5.x
OLD: This is an old version of documentation. You probably want the most recent version of this
document, from the sidebar on the right.
Inject.addContentRule(matchesPath, rule, options);
This function allows you to add a rule to transform content after it has been downloaded. Common applications are shimming a file (to give it CommonJS support), altering a file to fix a bug, or simply adding debugging information.
Examples
// Put everything assigned to "window" into module.exports
// great for "shimming" non AMD/CommonJS compatible files
Inject.addContentRule(/.*/, function(next, contents) {
next([
'(function() {',
' var window = {};',
contents,
' for (var prop in window) {',
' if (window.hasOwnProperty(prop)) {',
' module.exports[prop] = window[prop];',
' }',
' }',
'}())'
]);
});
// compile coffeescript (the rule-based way)
Inject.addContentRule(/+.*\.coffee$/, function(next, contents) {
next(CoffeeScript.compile(contents));
});
matchesPath
The matchesPath
parameter can either be a string or a regex. If a string, an exact match on the Module ID is required in order to perform the fetch alternative. If matchesPath
is a regular expression, then the path is tested against the regex.
rule
The rule
parameter is a function with the following signature:
function(next, contents)
- next: A continuation function. A fetch rule must call
next()
to continue the loading process. Failure to do so will result in a transformation “hanging” and eventually timing out. Call next in the format ofnext(error || null, contents || null)
- contents: The contents up to this point. If a previous transformation provided data, it will be available here. This should be the most recent state of the file during transformation.
options
A collection of options for this rule. Supported options are:
- weight: Assigns a weight to this rule. Larger numbered rules run first.
The family of rules Inject supports: