1 | var less = require('less');
|
2 | var sysPath = require('path');
|
3 | var progeny = require('progeny');
|
4 |
|
5 | function LESSCompiler(config) {
|
6 | if (config == null) config = {};
|
7 | if (config.plugins == null) config.plugins = {};
|
8 |
|
9 | this.config = config.plugins.less || {};
|
10 | this.rootPath = config.paths.root;
|
11 | this.optimize = config.optimize;
|
12 | this.getDependencies = progeny({rootPath: this.rootPath});
|
13 | }
|
14 |
|
15 | LESSCompiler.prototype.brunchPlugin = true;
|
16 | LESSCompiler.prototype.type = 'stylesheet';
|
17 | LESSCompiler.prototype.extension = 'less';
|
18 |
|
19 | LESSCompiler.prototype.compile = function(params, callback) {
|
20 | var data = params.data;
|
21 | var path = params.path;
|
22 |
|
23 | var parser = new less.Parser({
|
24 | paths: [this.rootPath, sysPath.dirname(path)],
|
25 | filename: path,
|
26 | dumpLineNumbers: !this.optimize && this.config.dumpLineNumbers
|
27 | });
|
28 |
|
29 | parser.parse(data, function(error, tree) {
|
30 | if (error != null) return callback(error.message);
|
31 | var result, err;
|
32 | try {
|
33 | result = tree.toCSS();
|
34 | } catch (ex) {
|
35 | err = '' + ex.type + 'Error:' + ex.message;
|
36 | if (ex.filename) {
|
37 | err += ' in "' + ex.filename + ':' + ex.line + ':' + ex.column + '"';
|
38 | }
|
39 | }
|
40 |
|
41 | return callback(err, {data: result});
|
42 | });
|
43 | };
|
44 |
|
45 | module.exports = LESSCompiler;
|