Skip to content

Commit

Permalink
Added support for absolute paths in annotations. You can also pass a …
Browse files Browse the repository at this point in the history
…function in `options.preprocessor` to modify the contents of file before it is searched for dependencies.
  • Loading branch information
Alex Doronin committed Mar 24, 2015
1 parent fe1ca23 commit bab107d
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,7 @@ Type: `Boolean`

Whether to just continue instead of emitting an error if circular dependencies are detected (defaults to ```false```).

#### options.preprocessor
Type: `Function`

This function allows to modify file contents before it is searched for dependencies.
11 changes: 10 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,17 @@ function resolveDependencies(config) {

content = targetFile.contents.toString('utf8');

if (config.preprocessor) {
content = config.preprocessor(content);
}

while (match = pattern.exec(content)) {
filePath = path.join(path.dirname(targetFile.path), match[1]);
filePath = match[1];

// Augment file path if it's not absolute
if (filePath && filePath[0] !== '/') {
filePath = path.join(path.dirname(targetFile.path), filePath);
}

// Check for circular dependencies
try {
Expand Down
6 changes: 6 additions & 0 deletions test/expected/main2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
console.log('test2.js');

/**
* @requires test/test2.js
*/
console.log('main2.js');
4 changes: 4 additions & 0 deletions test/fixtures/main2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* @requires test/test2.js
*/
console.log('main2.js');
1 change: 1 addition & 0 deletions test/fixtures/test/test2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('test2.js');
31 changes: 31 additions & 0 deletions test/main.js
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var gulp = require('gulp'),
fs = require('fs'),
path = require('path'),
es = require('event-stream'),
assert = require('assert'),
concat = require('gulp-concat'),
Expand All @@ -24,6 +25,36 @@ describe('gulp-resolve-dependencies', function() {
}));
});

it('should use preprocessor and generate concatenated JS file', function(done) {
function toAbsolutePath(content) {
return content.replace(/\* @requires [\s-]*(.*\.js)/g, function(match, p1, offset, string) {
var absolutePath = '* @requires ' + path.join(__dirname, 'fixtures', p1);

return absolutePath;
});
}

gulp.src(__dirname + '/fixtures/main2.js')
.pipe(resolveDependencies({
preprocessor: toAbsolutePath
}))
.pipe(concat('main2.js'))
.pipe(gulp.dest(__dirname + '/results/'))
.pipe(es.wait(function() {
var expected = fs.readFileSync(__dirname + '/expected/main2.js', 'utf8');

assert.equal(
fs.readFileSync(__dirname + '/results/main2.js', 'utf8'),
expected
);

fs.unlinkSync(__dirname + '/results/main2.js');
fs.rmdirSync(__dirname + '/results/');

done();
}));
});

it('should throw error due to circular dependency', function(done) {
gulp.src(__dirname + '/circular/a.js')
.pipe(resolveDependencies())
Expand Down

0 comments on commit bab107d

Please sign in to comment.