Skip to content

Commit 3e017a5

Browse files
committed
Initial commit
0 parents  commit 3e017a5

File tree

12 files changed

+289
-0
lines changed

12 files changed

+289
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.DS_Store
2+
node_modules
3+
test/results
4+
npm-debug.log

.npmignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.DS_Store
2+
test/results

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
language: node_js
2+
node_js:
3+
- 0.10
4+
5+
script:
6+
- npm test

LICENCE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License
2+
3+
Copyright (c) 2013 Thomas Jaggi
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# gulp-resolve-dependencies
2+
> Resolve dependency directives in assets, e.g. ```@depend``` ([Juicer](https://github.com/cjohansen/juicer)) or ```//= require``` ([Sprockets](https://github.com/sstephenson/sprockets))). Inspired by [grunt-concat-in-order](https://github.com/miensol/grunt-concat-in-order). Useful in combination with [gulp-concat](https://github.com/wearefractal/gulp-concat).
3+
4+
## Usage
5+
6+
First, install `gulp-resolve-dependencies` as a development dependency:
7+
8+
```shell
9+
npm install --save-dev gulp-resolve-dependencies
10+
```
11+
12+
Then, add it to your `gulpfile.js` (probably together with [gulp-concat](https://github.com/wearefractal/gulp-concat)):
13+
14+
```javascript
15+
var resolveDependencies = require('gulp-resolve-dependencies');
16+
var concat = require('gulp-concat');
17+
18+
gulp.task('js', function(){
19+
gulp.src(['app/assets/js/main.js'])
20+
.pipe(resolveDependencies({
21+
pattern: /\* @depend (.*?\.js)/g
22+
})
23+
.pipe(concat())
24+
.pipe(gulp.dest('dest/assets/js/'));
25+
});
26+
```
27+
28+
And use the directives in your JS files (dependencies can be nested, they are handled recursively):
29+
30+
```javascript
31+
/**
32+
* @depend libs/jquery/jquery.js
33+
* @depend ../modules/slideshow/slideshow.js
34+
*/
35+
36+
(function(window, document, $, undefined) {
37+
'use strict';
38+
39+
$(document).on('ready', function() {
40+
$('.slideshow').slideshow();
41+
});
42+
43+
})(window, document, jQuery);
44+
```
45+
46+
**Warning**: This might not be very efficient (especially in case of nested dependencies). Some kind of caching mechanism could come in handy.
47+
48+
49+
## API
50+
51+
### resolveDependencies(options)
52+
53+
#### options.pattern
54+
Type: `RegExp`
55+
56+
The matching pattern (optional, defaults to ```/\* @depend (.*?\.js)/g``).
57+
58+
#### options.log
59+
Type: `Boolean`
60+
61+
Whether to log the resolved dependencies (optional, defaults to ```false```.
62+

index.js

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
'use strict';
2+
3+
var fs = require('fs'),
4+
path = require('path'),
5+
gutil = require('gulp-util'),
6+
_ = require('lodash'),
7+
Stream = require('stream');
8+
9+
var PLUGIN_NAME = 'gulp-resolve-dependencies';
10+
11+
function resolveDependencies(config) {
12+
var stream,
13+
fileCache = [],
14+
filesReturned = [],
15+
getFiles = function(targetFile) {
16+
var pattern = _.clone(config, true).pattern,
17+
files = [],
18+
content,
19+
match,
20+
relFilePath,
21+
filePath,
22+
file,
23+
dependencies;
24+
25+
// Skip if already added to dependencies
26+
if (_.indexOf(fileCache, targetFile.path) !== -1) {
27+
return false;
28+
} else {
29+
fileCache.push(targetFile.path);
30+
}
31+
32+
content = targetFile.contents.toString('utf8');
33+
34+
while (match = pattern.exec(content)) {
35+
filePath = path.join(path.dirname(targetFile.path), match[1]);
36+
37+
// Skip if already added to dependencies
38+
if (_.indexOf(fileCache, filePath) !== -1) {
39+
continue;
40+
}
41+
42+
// Create new file
43+
file = new gutil.File({
44+
base: targetFile.base,
45+
path: filePath,
46+
contents: fs.readFileSync(filePath)
47+
});
48+
49+
// Get new dependencies
50+
while (dependencies = getFiles(file)) {
51+
files = files.concat(dependencies);
52+
}
53+
}
54+
55+
// Add file itself
56+
files.push(targetFile);
57+
58+
return files;
59+
};
60+
61+
// Set default values
62+
config = _.merge({
63+
pattern: /\* @depend (.*?\.js)/g,
64+
log: false
65+
}, config);
66+
67+
// Happy streaming
68+
stream = Stream.Transform({
69+
objectMode: true
70+
});
71+
72+
stream._transform = function(file, unused, cb) {
73+
var files;
74+
75+
if (file.isNull()) {
76+
this.push(file);
77+
78+
return cb();
79+
}
80+
81+
files = getFiles(file);
82+
83+
if (!files) {
84+
return cb();
85+
}
86+
87+
// Add dependencies and file itself to stream
88+
files.forEach(_.bind(function(file) {
89+
this.push(file);
90+
}, this));
91+
92+
if (config.log) {
93+
filesReturned = filesReturned.concat(files.map(function(file) {
94+
return file.path;
95+
}));
96+
}
97+
98+
cb();
99+
};
100+
101+
stream._flush = function(cb) {
102+
if (config.log) {
103+
gutil.log('[' + gutil.colors.green(PLUGIN_NAME) + '] Files returned to stream:', filesReturned);
104+
}
105+
106+
cb();
107+
};
108+
109+
return stream;
110+
};
111+
112+
module.exports = resolveDependencies;

package.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "gulp-resolve-dependencies",
3+
"version": "0.0.1",
4+
"description": "Resolve dependency directives in assets (e.g. \"@depend\" or \"//= require\" in JavaScript)",
5+
"license": "MIT",
6+
"repository": "backflip/gulp-resolve-dependencies",
7+
"author": "Thomas Jaggi",
8+
"engines": {
9+
"node": ">=0.10.0"
10+
},
11+
"scripts": {
12+
"test": "mocha"
13+
},
14+
"main": "./index.js",
15+
"keywords": [
16+
"gulpplugin",
17+
"gulp",
18+
"js",
19+
"dependencies",
20+
"depend",
21+
"depends",
22+
"concat",
23+
"order",
24+
"juicer",
25+
"sprockets"
26+
],
27+
"dependencies": {
28+
"gulp-util": "~2.2.0",
29+
"lodash": "~2.4.1"
30+
},
31+
"devDependencies": {
32+
"mocha": "*",
33+
"event-stream": "~3.1.0",
34+
"gulp": "~3.5.2"
35+
}
36+
}

test/expected/main.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
console.log('lib.js');
2+
3+
/**
4+
* @depend ../libs/lib.js
5+
*/
6+
console.log('test.js');
7+
8+
/**
9+
* @depend test/test.js
10+
*/
11+
console.log('main.js');

test/fixtures/libs/lib.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('lib.js');

test/fixtures/main.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* @depend test/test.js
3+
*/
4+
console.log('main.js');

0 commit comments

Comments
 (0)