Skip to content

Commit 0ef6b5b

Browse files
committed
Fixing the imports stored for cache invalidation to have the path and mtime again.
Closes #123
1 parent ecad4fa commit 0ef6b5b

File tree

4 files changed

+48
-8
lines changed

4 files changed

+48
-8
lines changed

lib/middleware.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,30 @@ module.exports = less.middleware = function(source, options){
196196
return next(err);
197197
}
198198

199+
// Determine the imports used and check modified times.
200+
var imports = [];
201+
output.imports.forEach(function(imported) {
202+
var currentImport = {
203+
path: imported,
204+
mtime: null
205+
};
206+
207+
imports.push(currentImport);
208+
209+
// Update the mtime of the import async.
210+
fs.stat(imported, function(err, lessStats){
211+
if (err) {
212+
return error(err);
213+
}
214+
215+
currentImport.mtime = lessStats.mtime;
216+
});
217+
});
218+
199219
// Store the less paths for simple cache invalidation.
200220
lessFiles[lessPath] = {
201221
mtime: Date.now(),
202-
imports: output.imports
222+
imports: imports
203223
};
204224

205225
if(output.map) {

test/fixtures/cacheFile-exp.json

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
{
22
"{$}/import.less": {
33
"imports": [
4-
"{$}/import-header.less",
5-
"{$}/import-widget.less",
6-
"{$}/import-color.less"
4+
{
5+
"path": "{$}/import-header.less",
6+
"mtime": null
7+
},
8+
{
9+
"path": "{$}/import-widget.less",
10+
"mtime": null
11+
},
12+
{
13+
"path": "{$}/import-color.less",
14+
"mtime": null
15+
}
716
]
817
}
918
}

test/fixtures/cacheFile-exp2.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
{
22
"{$}/import.less": {
33
"imports": [
4-
"{$}/import-color.less"
4+
{
5+
"path": "{$}/import-color.less",
6+
"mtime": null
7+
}
58
]
69
}
710
}

test/test-middleware.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,14 @@ describe('middleware', function(){
202202
}
203203

204204
var checkCacheFile = function(cacheFile, expectedFile){
205+
var sortByPath = function(a, b) {
206+
var keyA = a.path;
207+
var keyB = b.path;
208+
if(keyA < keyB) return -1;
209+
if(keyA > keyB) return 1;
210+
return 0;
211+
};
212+
205213
return function(){
206214
// Force cacheFile write.
207215
middleware._saveCacheToFile();
@@ -212,13 +220,13 @@ describe('middleware', function(){
212220
for (var file in cacheFileExpected) {
213221
assert(cacheFileOutput[file] != undefined);
214222

215-
var expectedImports = cacheFileExpected[file].imports.sort();
216-
var outputImports = cacheFileOutput[file].imports.sort();
223+
var expectedImports = cacheFileExpected[file].imports.sort(sortByPath);
224+
var outputImports = cacheFileOutput[file].imports.sort(sortByPath);
217225

218226
assert.equal(outputImports.length, expectedImports.length);
219227

220228
for (var i = 0; i < expectedImports.length; i++) {
221-
assert.equal(expectedImports[i], outputImports[i]);
229+
assert.equal(expectedImports[i].path, outputImports[i].path);
222230
}
223231
}
224232
}

0 commit comments

Comments
 (0)