Skip to content
This repository has been archived by the owner on Apr 20, 2018. It is now read-only.

Add support for non absolute sibling directories #406

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,15 @@ This will, on the fly, generate the following configuration:
}
```

Support is also available for sibling directories with relative paths

```html
<!-- build:js ../assets/js/optimized.js -->
<script src="../assets/js/foo.js"></script>
<script src="../assets/js/bar.js"></script>
<!-- endbuild -->
```

## License

[BSD license](http://opensource.org/licenses/bsd-license.php) and copyright Google
8 changes: 8 additions & 0 deletions lib/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(/(<!--\[if.*\]>)(<!-->)?( -->)?/g);
Expand Down
14 changes: 13 additions & 1 deletion lib/fileprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/sibling_path.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<!-- build:js ../scripts/foo.js -->
<script src="../scripts/bar.js"></script>
<script src="../scripts/baz.js"></script>
<!-- endbuild -->
9 changes: 9 additions & 0 deletions test/test-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down