Skip to content

Commit

Permalink
#1 checking modified dates of file before minifying
Browse files Browse the repository at this point in the history
  • Loading branch information
tracend committed Jul 16, 2014
1 parent cd7051f commit 793b258
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
14 changes: 14 additions & 0 deletions lib/util.js
@@ -1,4 +1,5 @@
var _ = require("underscore"),
fs = require('fs'),
path = require('path');

module.exports = {
Expand Down Expand Up @@ -64,6 +65,19 @@ module.exports = {
}
}
return destination;
},

// compares a file against a list of dependencies to check if there are updates
hasUpdates: function(master, deps){
var update = false;
var last = fs.statSync(master).mtime.getTime();
// assume deps are in an array
for( var i in deps ){
var file = deps[i];
var modified = fs.statSync(file).mtime.getTime();
if( modified > last ) update = true;
}
return update;
}

}
4 changes: 4 additions & 0 deletions tasks/js.js
@@ -1,4 +1,5 @@
var fs = require("fs"),
u = require("../lib/util"),
_ = require("underscore");

module.exports = function (grunt) {
Expand Down Expand Up @@ -40,6 +41,9 @@ module.exports = function (grunt) {
continue;
}
}
// - proceed only if there's a newer file
var updates = u.hasUpdates(lib, scripts);
if( !updates ) continue;

// in all other cases add the task
config.uglify[group] = {
Expand Down
24 changes: 23 additions & 1 deletion tasks/lessc.js
@@ -1,4 +1,5 @@
var fs = require("fs"),
u = require("../lib/util"),
_ = require("underscore");

module.exports = function (grunt) {
Expand Down Expand Up @@ -46,11 +47,15 @@ module.exports = function (grunt) {
var now = (new Date()).getTime();
var modified = fs.statSync(lib).mtime.getTime();
// stop now if it's too early
console.log("less: now - modified", (now - modified) );
if( now - modified < this.data.timeout ){
continue;
}
}
// - proceed only if there's a newer file
var files = getLessFiles( styles );
var updates = u.hasUpdates(lib, files);
if( !updates ) continue;

// in all other cases add the task
config.less.compile.files[lib] = styles;

Expand All @@ -70,3 +75,20 @@ module.exports = function (grunt) {


};


// Helpers

getLessFiles = function( files ){
var result = [];
for( var i in files ){
var file = files[i];
var path = file.substring(0,file.lastIndexOf("/")+1);
var contents = fs.readdirSync( path ); // get all files in the dir..
// add the path to the contents
for( var j in contents ){
result.push( path + contents[j] );
}
}
return result;
}

0 comments on commit 793b258

Please sign in to comment.