Last active
December 11, 2015 13:38
-
-
Save jonschlinkert/4608902 to your computer and use it in GitHub Desktop.
testing grunt-contrib-less with external options
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "compile": false, | |
| "compress": false, | |
| "noIDs": true, | |
| "noJSPrefix": true, | |
| "noOverqualifying": true, | |
| "noUnderscores": true, | |
| "noUniversalSelectors": true, | |
| "zeroUnits": true, | |
| "strictPropertyOrder": true, | |
| "stripColors": true, | |
| "paths": ["../less"] | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * grunt-contrib-less | |
| * http://gruntjs.com/ | |
| * | |
| * Copyright (c) 2012 Tyler Kellen, contributors | |
| * Licensed under the MIT license. | |
| */ | |
| 'use strict'; | |
| module.exports = function(grunt) { | |
| // Project configuration. | |
| grunt.initConfig({ | |
| // Configuration to be run (and then tested). | |
| less: { | |
| compile: { | |
| options: { | |
| lessrc: '.lessrc', | |
| paths: ['../less'] | |
| }, | |
| files: { | |
| // 'tmp/individual/*.css': ['test/fixtures/style*.less'], | |
| 'tmp/theme1.css': ['theme1/theme1.less'] | |
| } | |
| } | |
| } | |
| }); | |
| // These plugins provide necessary tasks. | |
| grunt.loadNpmTasks('grunt-contrib-less'); | |
| // Actually load this plugin's task(s). | |
| //grunt.loadTasks('tasks'); | |
| // By default, lint and run all tests. | |
| grunt.registerTask('default', ['less']); | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * grunt-contrib-less | |
| * http://gruntjs.com/ | |
| * | |
| * Copyright (c) 2012 Tyler Kellen, contributors | |
| * Licensed under the MIT license. | |
| */ | |
| 'use strict'; | |
| module.exports = function(grunt) { | |
| var path = require('path'); | |
| var less = require('less'); | |
| var kindOf = grunt.util.kindOf; | |
| var _ = grunt.util._; | |
| var helpers = require('grunt-lib-contrib').init(grunt); | |
| var lessOptions = { | |
| parse: ['paths', 'optimization', 'filename', 'strictImports', 'dumpLineNumbers'], | |
| render: ['compress', 'yuicompress'] | |
| }; | |
| grunt.registerMultiTask('less', 'Compile LESS files to CSS', function() { | |
| var done = this.async(); | |
| // Merge task-specific and/or target-specific options with these defaults. | |
| var options = this.options(); | |
| // Read LESS options from a specified lessrc file. | |
| if (options.lessrc) { | |
| options = _.extends(options, grunt.file.readJSON(options.lessrc)); | |
| } | |
| grunt.verbose.writeflags(options, 'Options'); | |
| // get files | |
| var files = this.file.src; | |
| var destFile = this.file.dest; | |
| if (files.length === 0) { | |
| grunt.fail.warn('Unable to compile; no valid source files were found.'); | |
| done(); | |
| } | |
| // hack by chris to support compiling individual files | |
| if (helpers.isIndividualDest(destFile)) { | |
| var basePath = helpers.findBasePath(files, options.basePath); | |
| grunt.util.async.forEachSeries(files, function(file, next) { | |
| var newFileDest = helpers.buildIndividualDest(destFile, file, basePath, options.flatten); | |
| compileLess(file, options, function(css, err) { | |
| if(!err) { | |
| grunt.file.write(newFileDest, css||''); | |
| grunt.log.writeln('File ' + newFileDest.cyan + ' created.'); | |
| next(null); | |
| } else { | |
| done(false); | |
| } | |
| }); | |
| }, function() { | |
| done(); | |
| }); | |
| } else { | |
| // normal execution | |
| var compiled = []; | |
| grunt.util.async.concatSeries(files, function(file, next) { | |
| compileLess(file, options, function(css, err) { | |
| if(!err) { | |
| compiled.push(css); | |
| next(null); | |
| } else { | |
| done(false); | |
| } | |
| }); | |
| }, function() { | |
| grunt.file.write(destFile, compiled.join('\n')); | |
| grunt.log.writeln('File ' + destFile.cyan + ' created.'); | |
| 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 = tree.toCSS(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.'); | |
| }; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment