Files
less.js/lib/less/plugins/function-importer.js
rjgotten d119e01807 Scoped @import (plugin) functions loading
- Limited @import (plugin) support to add/addMultiple of functions
- Altered @import (plugin) loading to support browser
- Support proper closure scoping of @import (plugin) loaded functions
2015-03-05 15:55:37 +01:00

36 lines
1004 B
JavaScript

var LessError = require('../less-error'),
tree = require("../tree");
var FunctionImporter = module.exports = function FunctionImporter(context, fileInfo) {
this.fileInfo = fileInfo;
};
FunctionImporter.prototype.eval = function(contents, callback) {
var loaded = {},
loader,
registry;
registry = {
add: function(name, func) {
loaded[name] = func;
},
addMultiple: function(functions) {
Object.keys(functions).forEach(function(name) {
loaded[name] = functions[name];
});
}
};
try {
loader = new Function("functions", "tree", "fileInfo", contents);
loader(registry, tree, this.fileInfo);
} catch(e) {
callback(new LessError({
message: "Plugin evaluation error: '" + e.name + ': ' + e.message.replace(/["]/g, "'") + "'" ,
filename: this.fileInfo.filename
}), null );
}
callback(null, { functions: loaded });
};