diff --git a/middleware.js b/middleware.js index 30f267d..36be2ed 100644 --- a/middleware.js +++ b/middleware.js @@ -106,113 +106,113 @@ module.exports = function(options) { } if (!/\.css$/.test(path)) { - next(); - } else { - var cssPath = join(dest, path), - sassPath = join(src, path.replace(/\.css$/, sassExtension)), - sassDir = dirname(sassPath); - - if (root) { - cssPath = join(root, dest, path.replace(new RegExp('^' + dest), '')); - sassPath = join(root, src, path - .replace(new RegExp('^' + dest), '') - .replace(/\.css$/, sassExtension)); + return next(); + } + + var cssPath = join(dest, path), + sassPath = join(src, path.replace(/\.css$/, sassExtension)), sassDir = dirname(sassPath); - } - if (debug) { - log('source', sassPath); - log('dest', options.response ? '' : cssPath); - } + if (root) { + cssPath = join(root, dest, path.replace(new RegExp('^' + dest), '')); + sassPath = join(root, src, path + .replace(new RegExp('^' + dest), '') + .replace(/\.css$/, sassExtension)); + sassDir = dirname(sassPath); + } - // When render is done, respond to the request accordingly - var done = function(err, result) { - var data; + if (debug) { + log('source', sassPath); + log('dest', options.response ? '' : cssPath); + } - if (err) { - var file = sassPath; - if (err.file && err.file != 'stdin') { - file = err.file; - } + // When render is done, respond to the request accordingly + var done = function(err, result) { + var data; + + if (err) { + var file = sassPath; + if (err.file && err.file != 'stdin') { + file = err.file; + } - var fileLineColumn = file + ':' + err.line + ':' + err.column; - data = err.message.replace(/^ +/, '') + '\n\nin ' + fileLineColumn; - if (debug) logError(data); + var fileLineColumn = file + ':' + err.line + ':' + err.column; + data = err.message.replace(/^ +/, '') + '\n\nin ' + fileLineColumn; + if (debug) logError(data); - error(err); + error(err); - return next(err); + return next(err); + } + + data = result.css; + + if (debug) { + log('render', options.response ? '' : sassPath); + + if (sourceMap) { + log('render', this.options.sourceMap); } + } + imports[sassPath] = result.stats.includedFiles; - data = result.css; + var cssDone = true; + var sourceMapDone = true; - if (debug) { - log('render', options.response ? '' : sassPath); + function doneWriting() { + if (!cssDone || !sourceMapDone) { + return; + } - if (sourceMap) { - log('render', this.options.sourceMap); - } + if (options.response === false) { + return next(sassMiddlewareError); } - imports[sassPath] = result.stats.includedFiles; - var cssDone = true; - var sourceMapDone = true; + res.writeHead(200, { + 'Content-Type': 'text/css', + 'Cache-Control': 'max-age=0' + }); + res.end(data); + } - function doneWriting() { - if (!cssDone || !sourceMapDone) { - return; - } + // If response is falsey, also write to file + if (options.response) { + return doneWriting(); + } - if (options.response === false) { - return next(sassMiddlewareError); - } + cssDone = false; + sourceMapDone = !sourceMap; - res.writeHead(200, { - 'Content-Type': 'text/css', - 'Cache-Control': 'max-age=0' - }); - res.end(data); + mkdirp(dirname(cssPath), '0700', function(err) { + if (err) { + return error(err); } - // If response is falsey, also write to file - if (options.response) { - return doneWriting(); - } + fs.writeFile(cssPath, data, 'utf8', function(err) { + if (err) { + return error(err); + } - cssDone = false; - sourceMapDone = !sourceMap; + cssDone = true; + doneWriting(); + }); + }); - mkdirp(dirname(cssPath), '0700', function(err) { + if (sourceMap) { + var sourceMapPath = this.options.sourceMap; + mkdirp(dirname(sourceMapPath), '0700', function(err) { if (err) { return error(err); } - fs.writeFile(cssPath, data, 'utf8', function(err) { + fs.writeFile(sourceMapPath, result.map, 'utf8', function(err) { if (err) { return error(err); } - - cssDone = true; + sourceMapDone = true; doneWriting(); }); }); - - if (sourceMap) { - var sourceMapPath = this.options.sourceMap; - mkdirp(dirname(sourceMapPath), '0700', function(err) { - if (err) { - return error(err); - } - - fs.writeFile(sourceMapPath, result.map, 'utf8', function(err) { - if (err) { - return error(err); - } - sourceMapDone = true; - doneWriting(); - }); - }); - } } }