forked from jonkemp/gulp-useref
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
225 lines (187 loc) · 7.77 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
'use strict';
var gutil = require('gulp-util'),
through = require('through2'),
useref = require('useref');
module.exports = function (options) {
var opts = options || {},
path = require('path'),
concat = require('gulp-concat'),
gulpif = require('gulp-if'),
es = require('event-stream'),
types = opts.types || [ 'css', 'js' ],
isRelativeUrl = require('is-relative-url'),
vfs = require('vinyl-fs'),
transforms = Array.prototype.slice.call(arguments, 1),
unprocessed = 0,
end = false,
additionalFiles = [],
waitForAssets,
searchPath = opts.searchPath;
if (searchPath && Array.isArray(searchPath)) {
searchPath = '{' + searchPath.join(',') + '}';
}
// If any external streams were included, add matched files to src
if (opts.additionalStreams) {
if (!Array.isArray(opts.additionalStreams)) {
opts.additionalStreams = [ opts.additionalStreams ];
}
opts.additionalStreams = opts.additionalStreams.map(function (stream) {
// filters stream to select needed files
return stream
.pipe(es.through(function (file) {
additionalFiles.push(file);
}));
});
}
if (opts.additionalStreams) {
// If we have additional streams, wait for them to run before continuing
waitForAssets = es.merge(opts.additionalStreams).pipe(through.obj());
} else {
// Else, create a fake stream
waitForAssets = through.obj();
waitForAssets.emit('finish');
}
return through.obj(function (file, enc, cb) {
var self = this;
waitForAssets.pipe(es.wait(function () {
var output,
html,
assets,
// Cache the file base path relative to the cwd
// Use later when it could be dropped
_basePath = path.dirname(file.path);
if (file.isNull()) {
cb(null, file);
return;
}
if (file.isStream()) {
cb(new gutil.PluginError('gulp-useref', 'Streaming not supported'));
return;
}
output = useref(file.contents.toString(), opts);
html = output[0];
try {
file.contents = new Buffer(html);
self.push(file);
} catch (err) {
self.emit('error', new gutil.PluginError('gulp-useref', err));
}
if (opts.noAssets) {
cb();
return;
}
assets = output[1];
types.forEach(function (type) {
var files = assets[type];
if (!files) {
return;
}
Object.keys(files).forEach(function (name) {
var src,
globs,
filepaths = files[name].assets,
sortIndex = {},
i = 0,
sortedFiles = [],
unsortedFiles = [];
if (!filepaths.length) {
return;
}
unprocessed++;
// Get relative file paths and join with search paths to send to vinyl-fs
globs = filepaths
.filter(isRelativeUrl)
.map(function (filepath) {
var searchPaths,
pattern,
matches,
glob = require('glob');
if (files[name].searchPaths || searchPath) {
searchPaths = path.resolve(file.cwd, files[name].searchPaths || searchPath);
}
pattern = (searchPaths || _basePath) + path.sep + filepath;
matches = glob.sync(pattern, { nosort: true });
if (!matches.length) {
matches.push(pattern);
}
if (opts.transformPath) {
matches[0] = opts.transformPath(matches[0]);
}
return matches[0];
});
src = vfs.src(globs, {
base: _basePath,
nosort: true
});
src.on('error', function (err) {
self.emit('error', new Error(err));
});
// add files from external streams
additionalFiles.forEach(function (addFile) {
src.push(addFile);
});
// if we added additional files, reorder the stream
if (additionalFiles.length > 0) {
// Create a sort index so we don't iterate over the globs for every file
globs.forEach(function (filename) {
sortIndex[filename] = i++;
});
src = src.pipe(through.obj(function (srcFile, encoding, callback) {
var index = sortIndex[srcFile.path];
if (index === undefined) {
unsortedFiles.push(srcFile);
} else {
sortedFiles[index] = srcFile;
}
callback();
}, function (callback) {
sortedFiles.forEach(function (sorted) {
if (sorted !== undefined) {
this.push(sorted);
}
}, this);
unsortedFiles.forEach(function (unsorted) {
this.push(unsorted);
}, this);
callback();
}));
}
// If any external transforms were included, pipe all files to them first
transforms.forEach(function (fn) {
src = src.pipe(
fn(name)
.on('error', function () {
this.emit('end');
})
);
});
// Add assets to the stream
// If noconcat option is false, concat the files first.
src
.pipe(gulpif(!opts.noconcat, concat(name)))
.pipe(through.obj(function (newFile, encoding, callback) {
// specify an output path relative to the cwd
if (opts.base) {
newFile.path = path.join(opts.base, name);
}
// add file to the asset stream
self.push(newFile);
callback();
}))
.on('finish', function () {
if (--unprocessed === 0 && end) {
// end the asset stream
self.emit('end');
}
});
});
});
cb();
}));
}, function () {
end = true;
if (unprocessed === 0) {
this.emit('end');
}
});
};