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