Merge pull request #71 from mde/deps

Add dependency tracking support
This commit is contained in:
Matthew Eernisse
2015-02-22 21:47:22 -08:00
2 changed files with 13 additions and 2 deletions

View File

@@ -370,6 +370,7 @@ function Template(text, opts) {
this.truncate = false;
this.currentLine = 1;
this.source = '';
this.dependencies = [];
options.client = opts.client || false;
options.escapeFunction = opts.escape || utils.escapeXML;
options.compileDebug = opts.compileDebug !== false;
@@ -466,12 +467,13 @@ Template.prototype = {
}
if (opts.client) {
fn.dependencies = this.dependencies;
return fn;
}
// Return a callable function which will execute the function
// created by the source-code, with the passed data as locals
return function (data) {
var returnedFn = function (data) {
var include = function (path, includeData) {
var d = utils.shallowCopy({}, data);
if (includeData) {
@@ -481,7 +483,8 @@ Template.prototype = {
};
return fn(data || {}, escape, include, rethrow);
};
returnedFn.dependencies = this.dependencies;
return returnedFn;
}
, generateSource: function () {
@@ -512,6 +515,8 @@ Template.prototype = {
includeSrc = includeSource(include[1], includeOpts);
includeSrc = ' ; (function(){' + '\n' + includeSrc + ' ; })()' + '\n';
self.source += includeSrc;
self.dependencies.push(exports.resolveInclude(
include[1], includeOpts.filename));
}
else {
self.scanLine(line);

View File

@@ -735,6 +735,12 @@ suite('preprocessor include', function () {
fixture('menu_preprocessor.html'));
});
test('tracks dependency correctly', function () {
var file = 'test/fixtures/menu_preprocessor.ejs'
, fn = ejs.compile(fixture('menu_preprocessor.ejs'), {filename: file});
assert(fn.dependencies.length);
});
test('include arbitrary files as-is', function () {
var file = 'test/fixtures/include_preprocessor.css.ejs';
assert.equal(ejs.render(fixture('include_preprocessor.css.ejs'), {pets: users}, {filename: file}),