-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
63 lines (50 loc) · 1.51 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
'use strict';
var path = require('path');
var gutil = require('gulp-util');
var through = require('through2');
var pleeease = require('pleeease');
var applySourceMap = require('vinyl-sourcemaps-apply');
var PLUGIN_NAME = 'gulp-pleeease';
module.exports = function (opts) {
opts = opts ? opts : {};
function bufferContents (file, enc, cb) {
var outFile = opts.out || file.relative;
if (file.isNull() || !file.contents.toString()) {
cb(null, file);
return;
}
if (file.isStream()) {
cb(new gutil.PluginError(PLUGIN_NAME, 'Streaming not supported'));
return;
}
try {
if (file.sourceMap) {
opts.sourcemaps = {
map : {
inline: false,
annotation: false
}
};
}
if (opts.sourcemaps) {
opts.sourcemaps.from = file.relative;
opts.sourcemaps.to = outFile;
}
pleeease.process(file.contents.toString(), opts).then(function (result) {
file.contents = new Buffer(result.css || result);
file.path = path.join(file.base, outFile);
if (file.sourceMap && result.map) {
applySourceMap(file, result.map.toString());
}
cb(null, file);
}, error);
} catch (err) {
error(err);
}
function error (err) {
cb(new gutil.PluginError(PLUGIN_NAME, err, {fileName: file.path}));
this.emit('error', new gutil.PluginError(PLUGIN_NAME, err));
}
}
return through.obj(bufferContents);
};