Skip to content

Commit

Permalink
Merge pull request #798 from adaptlearning/less/ABU-680
Browse files Browse the repository at this point in the history
Added custom less compiler and sourcemapper
  • Loading branch information
oliverfoster committed Nov 12, 2015
2 parents c54a1cc + 465f887 commit 22b165f
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 35 deletions.
17 changes: 0 additions & 17 deletions grunt/config/concat.js

This file was deleted.

50 changes: 43 additions & 7 deletions grunt/config/less.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,46 @@
module.exports = {
options:{
compress:true
},
dist: {
files: {
'<%= outputdir %>adapt/css/adapt.css' : '<%= sourcedir %>less/adapt.less'
module.exports = function (grunt, options) {
return {
dev: {
options:{
mandatory: [
'<%= sourcedir %>core/less/*.less'
],
src: [
'<%= sourcedir %>menu/<%= menu %>/**/*.less',
'<%= sourcedir %>components/**/*.less',
'<%= sourcedir %>extensions/**/*.less',
'<%= sourcedir %>theme/<%= theme %>/**/*.less'
],
sourcemaps:true,
compress:false,
dest: '<%= outputdir %>adapt/css/',
cssFilename: "adapt.css",
mapFilename: "adapt.css.map",
filter: function(filepath) {
return grunt.config('helpers').includedFilter(filepath);
}
}
},
compile: {
options: {
mandatory: [
'<%= sourcedir %>core/less/*.less'
],
src: [
'<%= sourcedir %>menu/<%= menu %>/**/*.less',
'<%= sourcedir %>components/**/*.less',
'<%= sourcedir %>extensions/**/*.less',
'<%= sourcedir %>theme/<%= theme %>/**/*.less'
],
sourcemaps: false,
compress:true,
dest: '<%= outputdir %>adapt/css/',
cssFilename: "adapt.css",
mapFilename: "adapt.css.map",
filter: function(filepath) {
return grunt.config('helpers').includedFilter(filepath);
}
}
}
}
}
2 changes: 1 addition & 1 deletion grunt/config/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
module.exports = {
less: {
files: ['<%= sourcedir %>**/*.less'],
tasks: ['concat', 'less']
tasks: ['less:dev']
},
handlebars: {
files: ['<%= sourcedir %>**/*.hbs'],
Expand Down
3 changes: 1 addition & 2 deletions grunt/tasks/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ module.exports = function(grunt) {
'check-json',
'clean:output',
'copy',
'concat',
'less',
'less:compile',
'handlebars',
'bower',
'requirejs-bundle',
Expand Down
6 changes: 4 additions & 2 deletions grunt/tasks/create-json-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ module.exports = function(grunt) {
customItems.forEach(function (customItem) {
// As any theme folder may be used, we need to first find the location of the
// theme.json file
var customItemJsonFile;
grunt.file.recurse(grunt.config('sourcedir') + customItem + '/', function(abspath, rootdir, subdir, filename) {
if (filename == customItem + '.json') {
customItemJsonFile = rootdir + subdir + '/' + filename;
}
});

if (customItemJsonFile == '') {
grunt.fail.fatal('Unable to locate ' + customItem + '.json, please ensure a valid ' + customItem + ' exists');
if (!customItemJsonFile) {
return;
//grunt.fail.fatal('Unable to locate ' + customItem + '.json, please ensure a valid ' + customItem + ' exists');
}

var customItemJson = grunt.file.readJSON(customItemJsonFile);
Expand Down
3 changes: 1 addition & 2 deletions grunt/tasks/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ module.exports = function(grunt) {
'jsonlint',
'check-json',
'copy',
'concat',
'less',
'less:dev',
'handlebars',
'bower',
'requirejs-bundle',
Expand Down
64 changes: 64 additions & 0 deletions grunt/tasks/less.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
module.exports = function(grunt) {
grunt.registerMultiTask('less', 'Compile LESS files to CSS', function() {
var less = require('less');
var _ = require('underscore');
var path = require("path");
var done = this.async();
var options = this.options({});

var imports = "";

if (options.mandatory) {
for (var i = 0, l = options.mandatory.length; i < l; i++) {
var src = options.mandatory[i];
grunt.file.expand({}, src).forEach(function(path) {
imports+= "@import '" + path + "';\n";
});
}
}

if (options.src) {
for (var i = 0, l = options.src.length; i < l; i++) {
var src = options.src[i];
grunt.file.expand({filter: options.filter}, src).forEach(function(path) {
imports+= "@import '" + path + "';\n";
});
}
}

var sourcemaps;
if (options.sourcemaps) {
sourcemaps = {
"sourceMap": {
"sourceMapFileInline": false,
"outputSourceFiles": true,
"sourceMapBasepath": "src",
"sourceMapURL": options.mapFilename,
}
};
} else {
var sourceMapPath = path.join(options.dest, options.mapFilename);
if (grunt.file.exists(sourceMapPath)) grunt.file.delete(sourceMapPath, {force:true});
if (grunt.file.exists(sourceMapPath+".imports")) grunt.file.delete(sourceMapPath+".imports", {force:true});
}

var lessOptions = _.extend({ "compress": options.compress }, sourcemaps);

less.render(imports, lessOptions, complete);

function complete(error, output) {
if (error) {
grunt.fail.fatal(JSON.stringify(error, false, " "));
return;
}

grunt.file.write(path.join(options.dest, options.cssFilename), output.css);

if (output.map) {
grunt.file.write(path.join(options.dest, options.mapFilename)+".imports", imports);
grunt.file.write(path.join(options.dest, options.mapFilename), output.map);
}
done();
}
});
}
3 changes: 1 addition & 2 deletions grunt/tasks/server-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ module.exports = function(grunt) {
grunt.task.run([
'_log-vars',
'copy',
'concat',
'less',
'less:' + requireMode,
'handlebars',
'bower',
'requirejs-bundle',
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,16 @@
"grunt-bower-requirejs": "~1.1.0",
"grunt-concurrent": "~1.0.0",
"grunt-contrib-clean": "~0.6.0",
"grunt-contrib-concat": "~0.5.0",
"grunt-contrib-connect": "~0.8.0",
"grunt-contrib-copy": "~0.6.0",
"grunt-contrib-handlebars": "^0.9.2",
"grunt-contrib-less": "~0.11.4",
"grunt-contrib-requirejs": "~0.4.4",
"grunt-contrib-watch": "~0.6.1",
"grunt-jsonlint": "~1.0.4",
"grunt-open": "~0.2.3",
"grunt-requirejs-bundle": "*",
"jit-grunt": "^0.9.1",
"less": "^2.5.3",
"load-grunt-config": "^0.17.2",
"lodash": "~2.4.1",
"matchdep": "~0.3.0",
Expand Down

0 comments on commit 22b165f

Please sign in to comment.