Skip to content

Commit

Permalink
Code clean up.
Browse files Browse the repository at this point in the history
  • Loading branch information
discojonathan committed Aug 4, 2015
1 parent fa193e7 commit 7f7d8d8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,18 +133,18 @@ Use additional streams as sources of assets. Useful for combining gulp-useref wi

```javascript
// create stream of virtual files
var tsStream = gulp.src("src/**/*.ts")
var tsStream = gulp.src('src/**/*.ts')
.pipe(ts());

// pass stream to assets, multiple streams are supported
var assets = useref.assets({ additionalStreams: [tsStream] });

// use gulp-useref normally
return gulp.src("src/index.html")
return gulp.src('src/index.html')
.pipe(assets)
.pipe(assets.restore())
.pipe(useref())
.pipe(gulp.dest("dist"));
.pipe(gulp.dest('dist'));
```

### stream.restore()
Expand Down
14 changes: 8 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ module.exports.assets = function (opts) {
restoreStream = through.obj(),
unprocessed = 0,
end = false,
additionalFiles = [];
additionalFiles = [],
waitForAssets;

// If any external streams were included, add matched files to src
if (opts.additionalStreams) {
Expand All @@ -74,7 +75,6 @@ module.exports.assets = function (opts) {
});
}

var waitForAssets;
if (opts.additionalStreams) {
// If we have additional streams, wait for them to run before continuing
waitForAssets = es.merge(opts.additionalStreams).pipe(through.obj());
Expand All @@ -86,6 +86,7 @@ module.exports.assets = function (opts) {

var assetStream = through.obj(function (file, enc, cb) {
var self = this;

waitForAssets.pipe(es.wait(function () {
var output = useref(file.contents.toString());
var assets = output[1];
Expand Down Expand Up @@ -150,18 +151,19 @@ module.exports.assets = function (opts) {

// if we added additional files, reorder the stream
if (additionalFiles.length > 0) {
var sortIndex = {};
var i = 0;
var sortIndex = {},
i = 0,
sortedFiles = [],
unsortedFiles = [];

// Create a sort index so we don't iterate over the globs for every file
globs.forEach(function (glob) {
sortIndex[glob] = i++;
});

var sortedFiles = [];
var unsortedFiles = [];
src = src.pipe(through.obj(function (file, enc, cb) {
var index = sortIndex[file.path];

if (index === undefined) {
unsortedFiles.push(file);
} else {
Expand Down

0 comments on commit 7f7d8d8

Please sign in to comment.