less.js
3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* grunt-contrib-less
* http://gruntjs.com/
*
* Copyright (c) 2013 Tyler Kellen, contributors
* Licensed under the MIT license.
*/
'use strict';
module.exports = function(grunt) {
// Internal lib.
var contrib = require('grunt-lib-contrib').init(grunt);
var path = require('path');
var less = require('less');
var lessOptions = {
parse: ['paths', 'optimization', 'filename', 'strictImports', 'syncImport', 'dumpLineNumbers', 'relativeUrls', 'rootpath'],
render: ['compress', 'yuicompress', 'ieCompat', 'strictMath']
};
grunt.registerMultiTask('less', 'Compile LESS files to CSS', function() {
var done = this.async();
var options = this.options();
grunt.verbose.writeflags(options, 'Options');
if (this.files.length < 1) {
grunt.verbose.warn('Destination not written because no source files were provided.');
}
grunt.util.async.forEachSeries(this.files, function(f, nextFileObj) {
var destFile = f.dest;
var files = f.src.filter(function(filepath) {
// Warn on and remove invalid source files (if nonull was set).
if (!grunt.file.exists(filepath)) {
grunt.log.warn('Source file "' + filepath + '" not found.');
return false;
} else {
return true;
}
});
if (files.length === 0) {
if (f.src.length < 1) {
grunt.log.warn('Destination not written because no source files were found.');
}
// No src files, goto next target. Warn would have been issued above.
return nextFileObj();
}
var compiledMax = [], compiledMin = [];
grunt.util.async.concatSeries(files, function(file, next) {
compileLess(file, options, function(css, err) {
if (!err) {
if (css.max) {
compiledMax.push(css.max);
}
compiledMin.push(css.min);
next();
} else {
nextFileObj(err);
}
});
}, function() {
if (compiledMin.length < 1) {
grunt.log.warn('Destination not written because compiled files were empty.');
} else {
var min = compiledMin.join(options.yuicompress ? '' : grunt.util.normalizelf(grunt.util.linefeed));
grunt.file.write(destFile, min);
grunt.log.writeln('File ' + destFile.cyan + ' created.');
// ...and report some size information.
if (options.report) {
contrib.minMaxInfo(min, compiledMax.join(grunt.util.normalizelf(grunt.util.linefeed)), options.report);
}
}
nextFileObj();
});
}, done);
});
var compileLess = function(srcFile, options, callback) {
options = grunt.util._.extend({filename: srcFile}, options);
options.paths = options.paths || [path.dirname(srcFile)];
var css;
var srcCode = grunt.file.read(srcFile);
var parser = new less.Parser(grunt.util._.pick(options, lessOptions.parse));
parser.parse(srcCode, function(parse_err, tree) {
if (parse_err) {
lessError(parse_err);
callback('',true);
}
try {
css = minify(tree, grunt.util._.pick(options, lessOptions.render));
callback(css, null);
} catch (e) {
lessError(e);
callback(css, true);
}
});
};
var formatLessError = function(e) {
var pos = '[' + 'L' + e.line + ':' + ('C' + e.column) + ']';
return e.filename + ': ' + pos + ' ' + e.message;
};
var lessError = function(e) {
var message = less.formatError ? less.formatError(e) : formatLessError(e);
grunt.log.error(message);
grunt.fail.warn('Error compiling LESS.');
};
var minify = function (tree, options) {
var result = {
min: tree.toCSS(options)
};
if (!grunt.util._.isEmpty(options)) {
result.max = tree.toCSS();
}
return result;
};
};