From cca173b8911c54afd9c1f4a6da1dc8b6768452be Mon Sep 17 00:00:00 2001 From: Dane Macaulay Date: Sun, 20 Jul 2014 10:25:46 -0500 Subject: [PATCH] Add support for non absolute sibling directories --- README.md | 9 +++++++++ lib/file.js | 8 ++++++++ lib/fileprocessor.js | 14 +++++++++++++- test/fixtures/sibling_path.html | 4 ++++ test/test-file.js | 9 +++++++++ 5 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/sibling_path.html diff --git a/README.md b/README.md index 69b737a..bc03d05 100644 --- a/README.md +++ b/README.md @@ -587,6 +587,15 @@ This will, on the fly, generate the following configuration: } ``` +Support is also available for sibling directories with relative paths + +```html + + + + +``` + ## License [BSD license](http://opensource.org/licenses/bsd-license.php) and copyright Google diff --git a/lib/file.js b/lib/file.js index 9e4ef1c..3f85ddf 100644 --- a/lib/file.js +++ b/lib/file.js @@ -80,6 +80,14 @@ var getBlocks = function (content) { // Alternate search path last.searchPath.push(build[2]); } + + // Handle assets in sibling directories, remove the prefix + // cause this will cause chaos in the .tmp directory + // readd the prefix in the file processor + if (last.dest.substring(0, 3) === '../') { + last.dest = last.dest.substr(3); + last.sibling = true; + } } // Check IE conditionals var isConditionalStart = l.match(/()?( -->)?/g); diff --git a/lib/fileprocessor.js b/lib/fileprocessor.js index 10a702b..09cf105 100644 --- a/lib/fileprocessor.js +++ b/lib/fileprocessor.js @@ -174,6 +174,7 @@ FileProcessor.prototype.replaceWithRevved = function replaceWithRevved(lines, as debug('Looking for revved version of ' + srcFile + ' in ', assetSearchPath); var file = self.finder.find(srcFile, assetSearchPath); + file = self.accountForSibling(file, srcFile); debug('Found file \'%s\'', file); @@ -188,7 +189,18 @@ FileProcessor.prototype.replaceWithRevved = function replaceWithRevved(lines, as return content; }; - +// find this file in our blocks array and update its filename if its a sibling +FileProcessor.prototype.accountForSibling = function (filename, srcFile) { + if (this.file && this.file.blocks) { + var sibling = _.find(this.file.blocks, function (block) { + return block.dest === srcFile && block.sibling; + }); + if (sibling) { + filename = '../'.concat(filename); + } + } + return filename; +}; FileProcessor.prototype.process = function (filename, assetSearchPath) { debug('processing file %s', filename, assetSearchPath); diff --git a/test/fixtures/sibling_path.html b/test/fixtures/sibling_path.html new file mode 100644 index 0000000..f22b2fb --- /dev/null +++ b/test/fixtures/sibling_path.html @@ -0,0 +1,4 @@ + + + + diff --git a/test/test-file.js b/test/test-file.js index 5c8119e..a6244ea 100644 --- a/test/test-file.js +++ b/test/test-file.js @@ -102,6 +102,15 @@ describe('File', function () { assert.equal('/scripts/foo.js', file.blocks[0].dest); }); + it('should strip ../ from block.dest and set block.sibling to true', function () { + var filename = __dirname + '/fixtures/sibling_path.html'; + var file = new File(filename); + + assert.equal(1, file.blocks.length); + assert.equal('scripts/foo.js', file.blocks[0].dest); + assert.equal(true, file.blocks[0].sibling); + }); + it('should detect the async attribute', function () { var filename = __dirname + '/fixtures/block_with_async.html'; var file = new File(filename);