express-compiless
Advanced tools
Comparing version 0.2.1 to 0.3.0
var Path = require('path'), | ||
crypto = require('crypto'), | ||
fs = require('fs'), | ||
async = require('async'), | ||
_ = require('underscore'), | ||
less = require('less'), | ||
seq = require('seq'), | ||
flattenLessText = require('./flattenLessText'); | ||
passError = require('passerror'), | ||
less = require('less'); | ||
@@ -50,7 +50,5 @@ require('express-hijackresponse'); | ||
if (matchContentType || (/\.less(?:\?.*)?$/.test(req.url) && contentType === 'application/octet-stream')) { | ||
function formatError(err) { // err can either be a string, an error object, or an array of those | ||
function formatError(err) { // err can either be a string or an error object | ||
if (typeof err === 'string') { | ||
return err; | ||
} else if (Array.isArray(err)) { | ||
return err.map(formatError).join('\n'); | ||
} else { | ||
@@ -84,56 +82,40 @@ // Assume error object | ||
isSeenByFileName = {}, | ||
errors = []; | ||
lessOptions = {paths: [baseDir]}, | ||
parser = new less.Parser(lessOptions); | ||
flattenLessText(lessText, baseDir, baseDir, isSeenByFileName, errors, function (err, flattenedLessText) { | ||
if (err) { | ||
errors.push(err); | ||
} | ||
seq(Object.keys(isSeenByFileName)) | ||
.parEach(function (fileName) { | ||
fs.stat(fileName, this.into(fileName)); | ||
}) | ||
.unflatten() | ||
.seq(function (fileNames) { | ||
var oldETag = res.getHeader('ETag'); | ||
if (oldETag) { | ||
var etagFragments = [oldETag.replace(/^"|"$/g, '')]; | ||
if (fileNames.length) { | ||
var importedFileStats = []; | ||
fileNames.sort().forEach(function (fileName) { | ||
var stats = this.vars[fileName]; | ||
importedFileStats.push(fileName, String(stats.mtime.getTime()), String(stats.size)); | ||
}, this); | ||
etagFragments.push(crypto.createHash('md5').update(importedFileStats.join('-')).digest('hex').substr(0, 16)); | ||
} | ||
var newETag = '"' + etagFragments.join('-') + '-compiless"'; | ||
res.setHeader('ETag', newETag); | ||
if (ifNoneMatch && ifNoneMatch.indexOf(newETag) !== -1) { | ||
return res.send(304); | ||
} | ||
parser.parse(lessText, passError(sendErrorResponse, function (root) { | ||
var importedFileNames = parser.imports && parser.imports.files ? Object.keys(parser.imports.files) : [], | ||
statsByFileName = {}; | ||
async.eachLimit(importedFileNames, 10, function (importedFileName, cb) { | ||
fs.stat(importedFileName, passError(cb, function (stats) { | ||
statsByFileName[importedFileName] = stats; | ||
cb(); | ||
})); | ||
}, passError(sendErrorResponse, function () { | ||
var oldETag = res.getHeader('ETag'); | ||
if (oldETag) { | ||
var etagFragments = [oldETag.replace(/^"|"$/g, '')]; | ||
if (importedFileNames.length) { | ||
var importedFileStats = []; | ||
importedFileNames.sort().forEach(function (importedFileName) { | ||
var stats = statsByFileName[importedFileName]; | ||
importedFileStats.push(importedFileName, String(stats.mtime.getTime()), String(stats.size)); | ||
}, this); | ||
etagFragments.push(crypto.createHash('md5').update(importedFileStats.join('-')).digest('hex').substr(0, 16)); | ||
} | ||
var newETag = '"' + etagFragments.join('-') + '-compiless"'; | ||
res.setHeader('ETag', newETag); | ||
// Unfortunately less.render throws errors in async code instead of passing it to our callback. | ||
// Hopefully the solution for https://github.com/cloudhead/less.js/issues/462 will remedy this. | ||
try { | ||
less.render(flattenedLessText, {paths: [baseDir]}, function (err, cssText) { | ||
if (err) { | ||
errors.push(err); | ||
} | ||
if (errors.length) { | ||
return sendErrorResponse(errors, cssText); | ||
} | ||
cssText = fileNames.map(function (fileName) { | ||
return ".compilessinclude {background-image: url(" + Path.relative(baseDir, fileName) + "); display: none;}\n"; | ||
}).join("") + cssText; | ||
res.setHeader('Content-Type', 'text/css'); | ||
res.setHeader('Content-Length', Buffer.byteLength(cssText)); | ||
res.end(cssText); | ||
}); | ||
} catch (err) { | ||
sendErrorResponse(err); | ||
if (ifNoneMatch && ifNoneMatch.indexOf(newETag) !== -1) { | ||
return res.send(304); | ||
} | ||
}); | ||
}); | ||
} | ||
var cssText = importedFileNames.map(function (importedFileName) { | ||
return ".compilessinclude {background-image: url(" + Path.relative(baseDir, importedFileName) + "); display: none;}\n"; | ||
}).join("") + root.toCSS(); | ||
res.setHeader('Content-Type', 'text/css'); | ||
res.setHeader('Content-Length', Buffer.byteLength(cssText)); | ||
res.end(cssText); | ||
})); | ||
})); | ||
}); | ||
@@ -140,0 +122,0 @@ } else { |
{ | ||
"name": "express-compiless", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"description": "Express middleware that compiles less files to css on the way out.", | ||
@@ -24,7 +24,7 @@ "main": "lib/compiless.js", | ||
"dependencies": { | ||
"async": "=0.2.9", | ||
"bufferjs": "=1.1.0", | ||
"express-hijackresponse": "=0.1.2", | ||
"less": "=1.3.3", | ||
"passerror": "=0.0.1", | ||
"seq": "=0.3.5", | ||
"less": "=1.4.1", | ||
"passerror": "=0.0.2", | ||
"underscore": "=1.4.2" | ||
@@ -31,0 +31,0 @@ }, |
@@ -55,3 +55,3 @@ var express = require('express'), | ||
expect(response.headers['content-type']).to.equal('text/css'); | ||
expect(body).to.match(/body:before \{.*Error.*\/importerror\.less.*ENOENT.*notfound\.less/); | ||
expect(body).to.match(/body:before \{.*Error.*\/importerror\.less.*notfound\.less/); | ||
done(); | ||
@@ -65,3 +65,3 @@ })); | ||
expect(response.headers['content-type']).to.equal('text/css'); | ||
expect(body).to.match(/body:before \{.*Error.*\/secondlevelimporterror\.less.*ENOENT.*notfound\.less/); | ||
expect(body).to.match(/body:before \{.*Error.*\/secondlevelimporterror\.less.*notfound\.less/); | ||
done(); | ||
@@ -68,0 +68,0 @@ })); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1
14772
15
182
+ Addedasync@=0.2.9
- Removedseq@=0.3.5
Updatedless@=1.4.1
Updatedpasserror@=0.0.2