express-compiless
Advanced tools
Comparing version 0.0.8 to 0.0.9
@@ -9,3 +9,3 @@ var Path = require('path'), | ||
// Returns flattened less text, accumulates seen files in the isSeenByFileName object provided by the caller. | ||
module.exports = function flattenLessText(lessText, baseDir, isSeenByFileName, cb) { | ||
module.exports = function flattenLessText(lessText, baseDir, isSeenByFileName, errors, cb) { | ||
lessText = lessText.replace(/\/\*[\s\S]*?\*\//g, '').replace(/\/\/[^\r\n]*/, ''); // Remove comments | ||
@@ -18,12 +18,21 @@ seq(lessText.split(importRegExp)) | ||
var importedFileName = Path.resolve(baseDir.replace(/\/*$/, '/'), matchImport[2] || matchImport[4]); | ||
isSeenByFileName[importedFileName] = true; | ||
fs.readFile(importedFileName, 'utf-8', passError(cb, function (importedLessText) { | ||
flattenLessText(importedLessText, Path.dirname(importedFileName), isSeenByFileName, passError(cb, function (lessText) { | ||
cb(null, lessText); | ||
})); | ||
})); | ||
fs.readFile(importedFileName, 'utf-8', function (err, importedLessText) { | ||
if (err) { | ||
errors.push(err); | ||
cb(null, ''); | ||
} else { | ||
isSeenByFileName[importedFileName] = true; | ||
flattenLessText(importedLessText, Path.dirname(importedFileName), isSeenByFileName, errors, passError(cb, function (lessText) { | ||
cb(null, lessText); | ||
})); | ||
} | ||
}); | ||
} else { | ||
this(null, chunk); | ||
cb(null, chunk); | ||
} | ||
}) | ||
.catch(function (err) { | ||
// Why doesn't this work?! | ||
cb(err); | ||
}) | ||
.seq(function () { | ||
@@ -30,0 +39,0 @@ cb(null, this.stack.join('')); |
var Path = require('path'), | ||
crypto = require('crypto'), | ||
fs = require('fs'), | ||
_ = require('underscore'), | ||
less = require('less'), | ||
@@ -34,2 +35,9 @@ seq = require('seq'), | ||
if (matchContentType || (/\.less(?:\?.*)?$/.test(req.url) && contentType === 'application/octet-stream')) { | ||
function sendErrorResponse(errorMessage, cssText) { | ||
errorMessage = "express-compiless: Error compiling " + req.originalUrl + ":\n" + errorMessage; | ||
res.removeHeader('Content-Length'); | ||
res.setHeader('Content-Type', 'text/css'); | ||
res.send("body:before {display: block; white-space: pre; font-family: 'Courier New', monospace; font-size: 24px; color: black; margin: 10px; padding: 10px; border: 4px dashed red; margin-bottom: 10px; content: '" + errorMessage.replace(/'/g, "\\'").replace(/\n/g, '\\00000a') + "'}\n" + (cssText || '')); | ||
} | ||
// If there's an ETag, make sure it's different from the original one so the downstream middleware | ||
@@ -49,5 +57,10 @@ // won't reply with a false positive 304 on the same URL after compiless has been enabled or disabled: | ||
baseDir = Path.resolve(options.root, req.url.replace(/\/[^\/]*(?:\?.*)?$/, '/').substr(1)), | ||
isSeenByFileName = {}; | ||
isSeenByFileName = {}, | ||
errors = []; | ||
flattenLessText(lessText, baseDir, isSeenByFileName, function (err, flattenedLessText) { | ||
flattenLessText(lessText, baseDir, isSeenByFileName, errors, function (err, flattenedLessText) { | ||
if (err) { | ||
errors.push(err); | ||
} | ||
seq(Object.keys(isSeenByFileName)) | ||
@@ -80,6 +93,9 @@ .parEach(function (fileName) { | ||
// Hopefully the solution for https://github.com/cloudhead/less.js/issues/462 will remedy this. | ||
less.render(lessText, {paths: [baseDir]}, function (err, cssText) { | ||
less.render(flattenedLessText, {paths: [baseDir]}, function (err, cssText) { | ||
if (err) { | ||
return res.send(500); | ||
errors.push(err); | ||
} | ||
if (errors.length) { | ||
return sendErrorResponse(_.pluck(errors, 'message').join("\n"), cssText); | ||
} | ||
cssText = fileNames.map(function (fileName) { | ||
@@ -86,0 +102,0 @@ return ".compilessinclude {background-image: url(" + Path.relative(baseDir, fileName) + "); display: none;}\n"; |
{ | ||
"name": "express-compiless", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"description": "Express middleware that compiles less files to css on the way out.", | ||
@@ -27,3 +27,4 @@ "main": "lib/index.js", | ||
"passerror": "=0.0.1", | ||
"seq": "=0.3.5" | ||
"seq": "=0.3.5", | ||
"underscore": "=1.4.2" | ||
}, | ||
@@ -30,0 +31,0 @@ "devDependencies": { |
@@ -20,5 +20,5 @@ express-compiless | ||
response never clashes with the original ETag. That prevents the | ||
middleware issuing the original response to be confused into sending a | ||
false positive `304 Not Modified` if compiless is turned off or | ||
removed from the stack later. | ||
middleware issuing the original response from being confused into | ||
sending a false positive `304 Not Modified` if compiless is turned | ||
off or removed from the stack later. | ||
@@ -25,0 +25,0 @@ |
@@ -42,9 +42,7 @@ var express = require('express'), | ||
// Unfortunately less.render throws instead of passing the error to our callback. | ||
// Hopefully the solution for https://github.com/cloudhead/less.js/issues/462 will remedy this. | ||
/* | ||
it('request for less file with syntax error', function (done) { | ||
request(baseUrl + '/syntaxerror.less', passError(done, function (response, body) { | ||
expect(response.statusCode).to.equal(500); | ||
expect(response.statusCode).to.equal(200); | ||
expect(response.headers['content-type']).to.equal('text/css'); | ||
expect(body).to.match(/^body:before \{.*Error.*\/syntaxerror\.less.*missing closing `\}`/); | ||
done(); | ||
@@ -56,7 +54,17 @@ })); | ||
request(baseUrl + '/importerror.less', passError(done, function (response, body) { | ||
expect(response.statusCode).to.equal(500); | ||
expect(response.statusCode).to.equal(200); | ||
expect(response.headers['content-type']).to.equal('text/css'); | ||
expect(body).to.match(/^body:before \{.*Error.*\/importerror\.less.*ENOENT.*notfound\.less/); | ||
done(); | ||
})); | ||
}); | ||
*/ | ||
it('request for less file with second level @import error', function (done) { | ||
request(baseUrl + '/secondlevelimporterror.less', passError(done, function (response, body) { | ||
expect(response.statusCode).to.equal(200); | ||
expect(response.headers['content-type']).to.equal('text/css'); | ||
expect(body).to.match(/^body:before \{.*Error.*\/secondlevelimporterror\.less.*ENOENT.*notfound\.less/); | ||
done(); | ||
})); | ||
}); | ||
}); |
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
15534
13
209
5
+ Addedunderscore@=1.4.2