-
Notifications
You must be signed in to change notification settings - Fork 90
/
index.js
197 lines (161 loc) · 5.57 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
const path = require('path');
const PluginError = require('plugin-error');
const es = require('event-stream');
const through = require('through2');
const useref = require('useref');
const getGlobs = require('./lib/getGlobs');
const addFilesFromExtStreams = require('./lib/addFilesFromExtStreams');
const addHtmlToStream = require('./lib/addHtmlToStream');
const unprocessedCounter = require('./lib/unprocessedCounter')();
const end = require('./lib/end')();
const additionalFiles = [];
let transforms;
let pluginOptions;
function handleAdditionalStreams(additionalStreams) {
let _additionalStreams = additionalStreams;
if (!Array.isArray(additionalStreams)) {
_additionalStreams = [ additionalStreams ];
}
// filters stream to select needed files
return _additionalStreams.map(stream => stream.pipe(es.through(file => {
additionalFiles.push(file);
})));
}
function addAssetsToStream(paths, files) {
const self = this;
const gulpif = require('gulp-if');
const concat = require('gulp-concat');
const vfs = require('vinyl-fs');
const extend = require('extend');
let src;
let globs;
const name = paths.name;
const basePath = paths.basePath;
const filepaths = files[name].assets;
const type = paths.type;
const options = extend({}, pluginOptions);
const gulpConcatOptions = {};
if (!filepaths.length) {
return;
}
unprocessedCounter.increment();
// Get relative file paths and join with search paths to send to vinyl-fs
globs = filepaths
.filter(url => !/^(?:\w+:)?\/\//.test(url)) // test if url is relative
.map(filepath => {
paths.filepath = filepath;
return getGlobs(paths, files);
});
src = vfs.src(globs, {
base: basePath,
nosort: true
})
.on('error', err => {
self.emit('error', new Error(err));
});
// add files from external streams
src = addFilesFromExtStreams.call(self, additionalFiles, globs, src);
// If any external transforms were included, pipe all files to them first
transforms.forEach(fn => {
src = src.pipe(fn(name));
});
// option for newLine in gulp-concat
if (Object.prototype.hasOwnProperty.call(options, 'newLine')) {
if (options.newLine === ';' && type === 'css') {
options.newLine = null;
}
gulpConcatOptions.newLine = options.newLine;
}
// Add assets to the stream
// If noconcat option is false, concat the files first.
src
.pipe(gulpif(!options.noconcat, concat(name, gulpConcatOptions)))
.pipe(through.obj((newFile, encoding, callback) => {
// specify an output path relative to the cwd
if (options.base) {
newFile.path = path.join(options.base, name);
newFile.base = options.base;
}
// add file to the asset stream
self.push(newFile);
callback();
}))
.on('finish', () => {
const unprocessed = unprocessedCounter.decrement();
if (unprocessed === 0 && end.get()) {
// end the asset stream
end.fn();
}
});
}
function processAssets({ cwd }, basePath, data) {
const self = this;
const types = pluginOptions.types || [ 'css', 'js' ];
types.forEach(type => {
const files = data[type];
let name;
if (!files) {
return;
}
for (name in files) {
addAssetsToStream.call(self, {
name,
basePath,
searchPath: pluginOptions.searchPath,
cwd,
transformPath: pluginOptions.transformPath,
type
}, files);
}
});
}
module.exports = function (options) {
const opts = options || {};
let waitForAssets;
let additionalStreams;
pluginOptions = opts;
transforms = Array.prototype.slice.call(arguments, 1);
// If any external streams were included, add matched files to src
if (opts.additionalStreams) {
additionalStreams = handleAdditionalStreams(opts.additionalStreams);
// If we have additional streams, wait for them to run before continuing
waitForAssets = es.merge(additionalStreams).pipe(through.obj());
} else {
// Else, create a fake stream
waitForAssets = through.obj();
}
return through.obj(function (file, enc, cb) {
const self = this;
waitForAssets.pipe(es.wait(() => {
let output;
// Cache the file base path relative to the cwd
// Use later when it could be dropped
const _basePath = path.dirname(file.path);
if (file.isNull()) {
return cb(null, file);
}
if (file.isStream()) {
return cb(new PluginError('gulp-useref', 'Streaming not supported'));
}
output = useref(file.contents.toString(), opts);
addHtmlToStream.call(self, file, output[0]);
if (!opts.noAssets) {
processAssets.call(self, file, _basePath, output[1]);
}
return cb();
}));
// If no external streams were included,
// emit 'end' on the empty stream
if (!additionalStreams) {
waitForAssets.emit('end');
}
}, cb => {
const unprocessed = unprocessedCounter.get();
let fn = () => {};
end.set(cb);
if (unprocessed === 0) {
fn = cb;
}
return fn();
});
};