-
Notifications
You must be signed in to change notification settings - Fork 171
/
Copy pathless.js
225 lines (192 loc) · 7.03 KB
/
less.js
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
* grunt-contrib-less
* http://gruntjs.com/
*
* Copyright (c) 2016 Tyler Kellen, contributors
* Licensed under the MIT license.
*/
'use strict';
var path = require('path');
var _ = require('lodash');
var async = require('async');
var chalk = require('chalk');
var less = require('less');
module.exports = function(grunt) {
grunt.registerMultiTask('less', 'Compile LESS files to CSS', function() {
var done = this.async();
var options = this.options({
banner: ''
});
if (this.files.length < 1) {
grunt.verbose.warn('Destination not written because no source files were provided.');
}
var tally = {
sheets: 0,
maps: 0
};
async.eachSeries(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;
}
return true;
});
if (files.length === 0) {
if (f.src.length < 1) {
grunt.log.warn('Destination ' + chalk.cyan(destFile) + ' not written because no source files were found.');
}
// No src files, goto next target. Warn would have been issued above.
return nextFileObj();
}
var compiled = [];
var i = 0;
async.concatSeries(files, function(file, next) {
if (i++ > 0) {
options.banner = '';
}
compileLess(file, destFile, options)
.then(function(output) {
compiled.push(output.css);
if (options.sourceMap && !options.sourceMapFileInline) {
var sourceMapFilename = options.sourceMapFilename;
if (!sourceMapFilename) {
sourceMapFilename = destFile + '.map';
}
if (typeof sourceMapFilename === 'function') {
try {
sourceMapFilename = sourceMapFilename(destFile);
} catch (e) {
sourceMapFilename = destFile + '.map';
grunt.fail.warn(wrapError(e, 'Generating sourceMapFilename failed.'));
}
}
grunt.file.write(sourceMapFilename, output.map);
grunt.verbose.writeln('File ' + chalk.cyan(sourceMapFilename) + ' created.');
tally.maps++;
}
process.nextTick(next);
},
function(err) {
nextFileObj(err);
});
}, function() {
if (compiled.length < 1) {
grunt.log.warn('Destination ' + chalk.cyan(destFile) + ' not written because compiled files were empty.');
} else {
var allCss = compiled.join(options.compress ? '' : grunt.util.normalizelf(grunt.util.linefeed));
// Add a process step to modify the written CSS
if ( typeof options.process === 'function' ) {
allCss = options.process( allCss, destFile );
}
grunt.file.write(destFile, allCss);
grunt.verbose.writeln('File ' + chalk.cyan(destFile) + ' created');
tally.sheets++;
}
nextFileObj();
});
}, function () {
if (tally.sheets) {
grunt.log.ok(tally.sheets + ' ' + grunt.util.pluralize(tally.sheets, 'stylesheet/stylesheets') + ' created.');
}
if (tally.maps) {
grunt.log.ok(tally.maps + ' ' + grunt.util.pluralize(tally.maps, 'sourcemap/sourcemaps') + ' created.');
}
done();
});
});
var compileLess = function(srcFile, destFile, options) {
options = _.assign({filename: srcFile}, options);
options.paths = options.paths || [path.dirname(srcFile)];
if (typeof options.paths === 'function') {
try {
options.paths = options.paths(srcFile);
} catch (e) {
grunt.fail.warn(wrapError(e, 'Generating @import paths failed.'));
}
}
if (options.sourceMap && !options.sourceMapFileInline && !options.sourceMapFilename) {
options.sourceMapFilename = destFile + '.map';
} else if (options.sourceMap && !options.sourceMapFileInline && typeof options.sourceMapFilename === 'function') {
try {
options.sourceMapFilename = options.sourceMapFilename(destFile);
} catch (e) {
grunt.fail.warn(wrapError(e, 'Generating sourceMapFilename failed.'));
}
}
if (typeof options.sourceMapBasepath === 'function') {
try {
options.sourceMapBasepath = options.sourceMapBasepath(srcFile);
} catch (e) {
grunt.fail.warn(wrapError(e, 'Generating sourceMapBasepath failed.'));
}
}
if (typeof options.sourceMapURL === 'function') {
try {
options.sourceMapURL = options.sourceMapURL(destFile);
} catch (e) {
grunt.fail.warn(wrapError(e, 'Generating sourceMapURL failed.'));
}
}
if (typeof options.sourceMap === 'boolean' && options.sourceMap) {
options.sourceMap = {
sourceMapBasepath: options.sourceMapBasepath,
sourceMapFilename: options.sourceMapFilename,
sourceMapInputFilename: options.sourceMapInputFilename,
sourceMapFullFilename: options.sourceMapFullFilename,
sourceMapURL: options.sourceMapURL,
sourceMapRootpath: options.sourceMapRootpath,
outputSourceFiles: options.outputSourceFiles,
sourceMapFileInline: options.sourceMapFileInline
};
}
var srcCode = grunt.file.read(srcFile);
// Equivalent to --modify-vars option.
// Properties under options.modifyVars are appended as less variables
// to override global variables.
var modifyVarsOutput = parseVariableOptions(options.modifyVars);
if (modifyVarsOutput) {
srcCode += '\n' + modifyVarsOutput;
}
// Load custom functions
if (options.customFunctions) {
Object.keys(options.customFunctions).forEach(function(name) {
less.functions.functionRegistry.add(name.toLowerCase(), function() {
var args = [].slice.call(arguments);
args.unshift(less);
var res = options.customFunctions[name].apply(this, args);
return typeof res === 'object' ? res : new less.tree.Anonymous(res);
});
});
}
return less.render(srcCode, options)
.catch(function(err) {
lessError(err, srcFile);
throw err;
});
};
var parseVariableOptions = function(options) {
var pairs = _.toPairs(options);
var output = '';
pairs.forEach(function(pair) {
output += '@' + pair[0] + ':' + pair[1] + ';';
});
return output;
};
var formatLessError = function(e) {
var pos = '[' + 'L' + e.line + ':' + ('C' + e.column) + ']';
return e.filename + ': ' + pos + ' ' + e.message;
};
var lessError = function(e, file) {
var message = less.formatError ? less.formatError(e) : formatLessError(e);
grunt.log.error(message);
grunt.fail.warn('Error compiling ' + file);
};
var wrapError = function (e, message) {
var err = new Error(message);
err.origError = e;
return err;
};
};