Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
cgross committed Mar 8, 2014
1 parent f5d1d9d commit d876c41
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 3 deletions.
19 changes: 19 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"node": true,
"esnext": true,
"bitwise": true,
"camelcase": true,
"curly": true,
"eqeqeq": true,
"immed": true,
"indent": 4,
"newcap": true,
"noarg": true,
"quotmark": "single",
"regexp": true,
"undef": true,
"unused": true,
"strict": true,
"trailing": true,
"smarttabs": true
}
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
language: node_js
node_js:
- '0.10'
- '0.11'
56 changes: 53 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,54 @@
gulp-dom-src
============
# gulp-dom-src [![Build Status](https://travis-ci.org/cgross/gulp-dom-src.png?branch=master)](https://travis-ci.org/cgross/gulp-dom-src)

> Create a gulp stream from script, link, or any set of tags in an HTML file.
## Example

```js
var gulp = require('gulp');
var domSrc = require('gulp-dom-src');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');

gulp.task('default', function () {
domSrc({ file: 'index.html', selector: 'script', attribute: 'src' })
.pipe(concat('app.full.min.js'))
.pipe(uglify())
.pipe(gulp.dest('dist/'));
});
```

## API

### domSrc(config)


#### config.file

Type: `String`

The name of the HTML file to read the tags from.


#### config.selector

Type: `String`

Any valid CSS selector.


#### config.attribute

Type: `String`

The name of the attribute that contains the file path. Typically `src` for `script` tags and `href` for `link`s.


#### config.options

Type: `Object`
Default: `{}`

Options passed through to the underlying `vinyl-fs`. Can include options like `read` and `buffer`.


Create a gulp stream from script, link, or any set of tags in an HTML file.
20 changes: 20 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var vinylFs = require('vinyl-fs');
var cheerio = require('cheerio');
var fs = require('fs');
var path = require('path');

module.exports = function(config){

var html = fs.readFileSync(path.join(process.cwd(),config.file),'utf8');
var $ = cheerio.load(html);

var files = $(config.selector).map(function(i,elem){
return $(elem).attr(config.attribute);
}).toArray().filter(function(item){
return (item !== undefined && item.substring(0,4) !== 'http' && item.substring(0,2) !== '//');
}).map(function(item){
return path.join(path.dirname(config.file),item);
});

return vinylFs.src(files,config.options || {});
};
30 changes: 30 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "gulp-dom-src",
"version": "0.1.0",
"description": "Create a gulp stream from script/link tags in an HTML file.",
"main": "index.js",
"scripts": {
"test": "mocha"
},
"repository": {
"type": "git",
"url": "https://github.com/cgross/gulp-dom-src"
},
"keywords": [
"gulpplugin",
"html",
"dom"
],
"author": "Chris Gross",
"license": "MIT",
"dependencies": {
"vinyl-fs": "~0.1.0",
"cheerio": "~0.13.1"
},
"devDependencies": {
"mocha": "~1.17.1",
"should": "~3.1.3",
"through2": "~0.4.1",
"buffer-equal": "0.0.0"
}
}
13 changes: 13 additions & 0 deletions test/fixture/fixture.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<html>
<body>
<script src="one.js"></script>
<script>
alert('hello');
</script>
<script src="two.js"/></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js"></script>
<script src="three.js"/></script>
</body>
</html>
1 change: 1 addition & 0 deletions test/fixture/one.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
one
1 change: 1 addition & 0 deletions test/fixture/three.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
three
1 change: 1 addition & 0 deletions test/fixture/two.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
two
34 changes: 34 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
var path = require('path');
var fs = require('fs');
var should = require('should');
var domSrc = require('../index');
var vfs = require('vinyl-fs');
var through = require('through2');

var dataWrap = function(fn) {
return function(data, enc, cb) {
fn(data);
cb();
};
};

it('should read the script tags src into the stream', function (done) {

var onEnd = function(){
buffered.length.should.equal(3);
should.exist(buffered[0].stat);
buffered[0].path.should.equal(path.resolve('test/fixture/one.js'));
buffered[1].path.should.equal(path.resolve('test/fixture/two.js'));
buffered[2].path.should.equal(path.resolve('test/fixture/three.js'));
done();
};

var stream = domSrc({file:'test/fixture/fixture.html',selector:'script',attribute:'src'});

var buffered = [];
var bufferStream = through.obj(dataWrap(buffered.push.bind(buffered)), onEnd);
stream.pipe(bufferStream);

});


0 comments on commit d876c41

Please sign in to comment.