less.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * grunt-contrib-less
  3. * http://gruntjs.com/
  4. *
  5. * Copyright (c) 2016 Tyler Kellen, contributors
  6. * Licensed under the MIT license.
  7. */
  8. 'use strict';
  9. var path = require('path');
  10. var _ = require('lodash');
  11. var async = require('async');
  12. var chalk = require('chalk');
  13. var less = require('less');
  14. module.exports = function(grunt) {
  15. grunt.registerMultiTask('less', 'Compile LESS files to CSS', function() {
  16. var done = this.async();
  17. var options = this.options({
  18. banner: ''
  19. });
  20. if (this.files.length < 1) {
  21. grunt.verbose.warn('Destination not written because no source files were provided.');
  22. }
  23. var tally = {
  24. sheets: 0,
  25. maps: 0
  26. };
  27. async.eachSeries(this.files, function(f, nextFileObj) {
  28. var destFile = f.dest;
  29. var files = f.src.filter(function(filepath) {
  30. // Warn on and remove invalid source files (if nonull was set).
  31. if (!grunt.file.exists(filepath)) {
  32. grunt.log.warn('Source file "' + filepath + '" not found.');
  33. return false;
  34. }
  35. return true;
  36. });
  37. if (files.length === 0) {
  38. if (f.src.length < 1) {
  39. grunt.log.warn('Destination ' + chalk.cyan(destFile) + ' not written because no source files were found.');
  40. }
  41. // No src files, goto next target. Warn would have been issued above.
  42. return nextFileObj();
  43. }
  44. var compiled = [];
  45. var i = 0;
  46. async.concatSeries(files, function(file, next) {
  47. if (i++ > 0) {
  48. options.banner = '';
  49. }
  50. compileLess(file, destFile, options)
  51. .then(function(output) {
  52. compiled.push(output.css);
  53. if (options.sourceMap && !options.sourceMapFileInline) {
  54. var sourceMapFilename = options.sourceMapFilename;
  55. if (!sourceMapFilename) {
  56. sourceMapFilename = destFile + '.map';
  57. }
  58. grunt.file.write(sourceMapFilename, output.map);
  59. grunt.verbose.writeln('File ' + chalk.cyan(sourceMapFilename) + ' created.');
  60. tally.maps++;
  61. }
  62. process.nextTick(next);
  63. },
  64. function(err) {
  65. nextFileObj(err);
  66. });
  67. }, function() {
  68. if (compiled.length < 1) {
  69. grunt.log.warn('Destination ' + chalk.cyan(destFile) + ' not written because compiled files were empty.');
  70. } else {
  71. var allCss = compiled.join(options.compress ? '' : grunt.util.normalizelf(grunt.util.linefeed));
  72. grunt.file.write(destFile, allCss);
  73. grunt.verbose.writeln('File ' + chalk.cyan(destFile) + ' created');
  74. tally.sheets++;
  75. }
  76. nextFileObj();
  77. });
  78. }, function () {
  79. if (tally.sheets) {
  80. grunt.log.ok(tally.sheets + ' ' + grunt.util.pluralize(tally.sheets, 'stylesheet/stylesheets') + ' created.');
  81. }
  82. if (tally.maps) {
  83. grunt.log.ok(tally.maps + ' ' + grunt.util.pluralize(tally.maps, 'sourcemap/sourcemaps') + ' created.');
  84. }
  85. done();
  86. });
  87. });
  88. var compileLess = function(srcFile, destFile, options) {
  89. options = _.assign({filename: srcFile}, options);
  90. options.paths = options.paths || [path.dirname(srcFile)];
  91. if (typeof options.paths === 'function') {
  92. try {
  93. options.paths = options.paths(srcFile);
  94. } catch (e) {
  95. grunt.fail.warn(wrapError(e, 'Generating @import paths failed.'));
  96. }
  97. }
  98. if (options.sourceMap && !options.sourceMapFileInline && !options.sourceMapFilename) {
  99. options.sourceMapFilename = path.basename(destFile) + '.map';
  100. }
  101. if (typeof options.sourceMapBasepath === 'function') {
  102. try {
  103. options.sourceMapBasepath = options.sourceMapBasepath(srcFile);
  104. } catch (e) {
  105. grunt.fail.warn(wrapError(e, 'Generating sourceMapBasepath failed.'));
  106. }
  107. }
  108. if (typeof options.sourceMap === 'boolean' && options.sourceMap) {
  109. options.sourceMap = {
  110. sourceMapBasepath: options.sourceMapBasepath,
  111. sourceMapFilename: options.sourceMapFilename,
  112. sourceMapInputFilename: options.sourceMapInputFilename,
  113. sourceMapFullFilename: options.sourceMapFullFilename,
  114. sourceMapURL: options.sourceMapURL,
  115. sourceMapRootpath: options.sourceMapRootpath,
  116. outputSourceFiles: options.outputSourceFiles,
  117. sourceMapFileInline: options.sourceMapFileInline
  118. };
  119. }
  120. var srcCode = grunt.file.read(srcFile);
  121. // Equivalent to --modify-vars option.
  122. // Properties under options.modifyVars are appended as less variables
  123. // to override global variables.
  124. var modifyVarsOutput = parseVariableOptions(options.modifyVars);
  125. if (modifyVarsOutput) {
  126. srcCode += '\n' + modifyVarsOutput;
  127. }
  128. // Load custom functions
  129. if (options.customFunctions) {
  130. Object.keys(options.customFunctions).forEach(function(name) {
  131. less.functions.functionRegistry.add(name.toLowerCase(), function() {
  132. var args = [].slice.call(arguments);
  133. args.unshift(less);
  134. var res = options.customFunctions[name].apply(this, args);
  135. return typeof res === 'object' ? res : new less.tree.Anonymous(res);
  136. });
  137. });
  138. }
  139. return less.render(srcCode, options)
  140. .catch(function(err) {
  141. lessError(err, srcFile);
  142. throw err;
  143. });
  144. };
  145. var parseVariableOptions = function(options) {
  146. var pairs = _.toPairs(options);
  147. var output = '';
  148. pairs.forEach(function(pair) {
  149. output += '@' + pair[0] + ':' + pair[1] + ';';
  150. });
  151. return output;
  152. };
  153. var formatLessError = function(e) {
  154. var pos = '[' + 'L' + e.line + ':' + ('C' + e.column) + ']';
  155. return e.filename + ': ' + pos + ' ' + e.message;
  156. };
  157. var lessError = function(e, file) {
  158. var message = less.formatError ? less.formatError(e) : formatLessError(e);
  159. grunt.log.error(message);
  160. grunt.fail.warn('Error compiling ' + file);
  161. };
  162. var wrapError = function (e, message) {
  163. var err = new Error(message);
  164. err.origError = e;
  165. return err;
  166. };
  167. };