-
Notifications
You must be signed in to change notification settings - Fork 29
/
index.js
38 lines (31 loc) · 945 Bytes
/
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
const PluginError = require('plugin-error');
const through = require('through2');
const inlineCss = require('inline-css');
module.exports = opt => through.obj(function (file, enc, cb) {
const self = this;
const _opt = JSON.parse(JSON.stringify(opt || {}));
// 'url' option is required
// set it automatically if not provided
if (!_opt.url) {
_opt.url = `file://${file.path}`;
}
if (file.isNull()) {
cb(null, file);
return;
}
if (file.isStream()) {
cb(new PluginError('gulp-inline-css', 'Streaming not supported'));
return;
}
inlineCss(String(file.contents), _opt)
.then(html => {
file.contents = Buffer.from(html);
self.push(file);
return cb();
})
.catch(err => {
if (err) {
self.emit('error', new PluginError('gulp-inline-css', err));
}
});
});