less.js 5.5 KB

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