diff --git a/index.js b/index.js index 8c02aac..19f3e5f 100644 --- a/index.js +++ b/index.js @@ -225,31 +225,42 @@ var parseJSON = function parseJSON(data, filename) { * Asynchronously walks the file list until a match is found. If * no matches are found, calls the callback with an error **/ -var readFirstFile = function readFirstFile(uri, filenames, css, cb, examinedFiles) { +var readFirstFile = function readFirstFile(uri, filenames, options, cb, examinedFiles) { var filename = filenames.shift(); examinedFiles = examinedFiles || []; examinedFiles.push(filename); - fs.readFile(filename, function(err, data) { - if (err) { - if (filenames.length) { - readFirstFile(uri, filenames, css, cb, examinedFiles); + fs.readFile(filename, (function (filename) { + return function(err, data) { + if (err) { + if (filenames.length) { + readFirstFile(uri, filenames, options, cb, examinedFiles); + } + else { + cb(new Error('Could not import `' + uri + '` from any of the following locations:\n ' + examinedFiles.join('\n '))); + } } else { - cb(new Error('Could not import `' + uri + '` from any of the following locations:\n ' + examinedFiles.join('\n '))); - } - } - else { - if ([ '.js', '.json', '.yml', '.yaml' ].indexOf(path.extname(filename)) !== -1) { - data = parseJSON(data, filename); + if ([ '.js', '.json', '.yml', '.yaml' ].indexOf(path.extname(filename)) !== -1) { + data = parseJSON(data, filename); + } + + var contents = data.toString(); + var tmp = options.importOnce.transformContent(filename, contents); + if (typeof tmp != 'undefined') { + contents = tmp; + } + + cb(null, { + 'contents': contents, + 'file': filename + }); } - cb(null, { - 'contents': data.toString(), - 'file': filename - }); - } - }); + }; + })(filename)); }; +var noop = function noop () {}; + // This is a bootstrap function for calling readFirstFile. var readAbstractFile = function readAbstractFile(uri, abstractName, cb) { var gfn = getFileNames.bind(this), @@ -267,7 +278,7 @@ var readAbstractFile = function readAbstractFile(uri, abstractName, cb) { files = files.concat(gbn(uri)); } - readFirstFile(uri, files, css, cb); + readFirstFile(uri, files, this.options, cb); }; /** @@ -299,6 +310,10 @@ var importer = function importer(uri, prev, done) { this.options.importOnce.css = false; } + if (typeof this.options.importOnce.transformContent != 'function') { + this.options.importOnce.transformContent = noop; + } + // Create an import cache if it doesn't exist if (!this._importOnceCache) { this._importOnceCache = {};