1 |
|
2 | (function() {
|
3 | var LESSCompiler, less, sysPath,
|
4 | __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
|
5 |
|
6 | less = require('less');
|
7 |
|
8 | sysPath = require('path');
|
9 |
|
10 | module.exports = LESSCompiler = (function() {
|
11 | LESSCompiler.prototype.brunchPlugin = true;
|
12 |
|
13 | LESSCompiler.prototype.type = 'stylesheet';
|
14 |
|
15 | LESSCompiler.prototype.extension = 'less';
|
16 |
|
17 | LESSCompiler.prototype._dependencyRegExp = /^ *@import ['"](.*)['"]/;
|
18 |
|
19 | function LESSCompiler(config) {
|
20 | this.config = config;
|
21 | this.getDependencies = __bind(this.getDependencies, this);
|
22 | null;
|
23 | }
|
24 |
|
25 | LESSCompiler.prototype.compile = function(data, path, callback) {
|
26 | var parser,
|
27 | _this = this;
|
28 |
|
29 | parser = new less.Parser({
|
30 | paths: [this.config.paths.root, sysPath.dirname(path)],
|
31 | filename: path
|
32 | });
|
33 | return parser.parse(data, function(error, tree) {
|
34 | var err, ex, result;
|
35 |
|
36 | if (error != null) {
|
37 | return callback(error.message);
|
38 | }
|
39 | result = null;
|
40 | err = null;
|
41 | try {
|
42 | result = tree.toCSS();
|
43 | } catch (_error) {
|
44 | ex = _error;
|
45 | err = "" + ex.type + "Error:" + ex.message;
|
46 | if (ex.filename) {
|
47 | err += " in '" + ex.filename + ":" + ex.line + ":" + ex.column + "'";
|
48 | }
|
49 | }
|
50 | return callback(err, result);
|
51 | });
|
52 | };
|
53 |
|
54 | LESSCompiler.prototype.getDependencies = function(data, path, callback) {
|
55 | var dependencies, parent,
|
56 | _this = this;
|
57 |
|
58 | parent = sysPath.dirname(path);
|
59 | dependencies = data.split('\n').map(function(line) {
|
60 | return line.match(_this._dependencyRegExp);
|
61 | }).filter(function(match) {
|
62 | return (match != null ? match.length : void 0) > 0;
|
63 | }).map(function(match) {
|
64 | return match[1];
|
65 | }).filter(function(path) {
|
66 | return !!path && path !== 'nib';
|
67 | }).map(function(path) {
|
68 | if (sysPath.extname(path) !== ("." + _this.extension)) {
|
69 | return path + ("." + _this.extension);
|
70 | } else {
|
71 | return path;
|
72 | }
|
73 | }).map(function(path) {
|
74 | if (path.charAt(0) === '/') {
|
75 | return sysPath.join(_this.config.paths.root, path.slice(1));
|
76 | } else {
|
77 | return sysPath.join(parent, path);
|
78 | }
|
79 | });
|
80 | return process.nextTick(function() {
|
81 | return callback(null, dependencies);
|
82 | });
|
83 | };
|
84 |
|
85 | return LESSCompiler;
|
86 |
|
87 | })();
|
88 |
|
89 | }).call(this);
|