1 | 'use strict';
|
2 |
|
3 | var gutil = require('gulp-util'),
|
4 | through = require('through2'),
|
5 | inlineCss = require('inline-css');
|
6 |
|
7 | module.exports = function (opt) {
|
8 | return through.obj(function (file, enc, cb) {
|
9 | var self = this,
|
10 | _opt = JSON.parse(JSON.stringify(opt || {}));
|
11 |
|
12 |
|
13 |
|
14 | if (!_opt.url) {
|
15 | _opt.url = 'file://' + file.path;
|
16 | }
|
17 |
|
18 | if (file.isNull()) {
|
19 | cb(null, file);
|
20 | return;
|
21 | }
|
22 |
|
23 | if (file.isStream()) {
|
24 | cb(new gutil.PluginError('gulp-inline-css', 'Streaming not supported'));
|
25 | return;
|
26 | }
|
27 |
|
28 | inlineCss(file.contents, _opt)
|
29 | .then(function (html) {
|
30 | file.contents = new Buffer(String(html));
|
31 |
|
32 | self.push(file);
|
33 |
|
34 | return cb();
|
35 | })
|
36 | .catch(function (err) {
|
37 | if (err) {
|
38 | self.emit('error', new gutil.PluginError('gulp-inline-css', err));
|
39 | }
|
40 | });
|
41 | });
|
42 | };
|