LESS - 编程式用法
LESS 中编程式用法的核心是 less.render 函数。该函数在 LESS 中的使用格式如下 −
less.render(input_data, options)
.then(function(output) {
//code here
},
function(error) {
});
该函数也可以按以下方式编写 −
less.render(css, options, function(error, output) {})
options 是一个可选参数,当不指定 callback 时返回 promise,当指定 callback 时也返回 promise。可以通过将文件读取为字符串并设置主文件的 filename 字段来显示文件。
sourceMap 选项允许设置 sourcemap 选项,例如 sourceMapURL、sourceMapBasepath、sourceMapRootpath、outputSourceFiles 和 sourceMapFileInline。需要注意的是,sourceMap 选项在 less.js 中不可用。
可以通过添加监听器来访问日志,格式如下所示 −
less.logger.addListener({
debug: function(message) {
},
info: function(message) {
},
warn: function(message) {
},
error: function(message) {
}
});
上述定义的函数均为可选的。如果显示错误,则会将错误传递给 less.render 中的 callback 或 promise。