-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathindex.js
More file actions
46 lines (40 loc) · 1.47 KB
/
Copy pathindex.js
File metadata and controls
46 lines (40 loc) · 1.47 KB
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
'use strict';
var nano = require('cssnano'),
assign = require('object-assign'),
PluginError = require('gulp-util').PluginError,
Transform = require('stream').Transform,
applySourceMap = require('vinyl-sourcemaps-apply'),
PLUGIN_NAME = 'gulp-cssnano';
module.exports = function(options) {
options = options || {};
var stream = new Transform({objectMode: true});
stream._transform = function(file, encoding, cb) {
if (file.isNull()) {
return cb(null, file);
}
if (file.isStream()) {
var error = 'Streaming not supported';
return cb(new PluginError(PLUGIN_NAME, error));
} else if (file.isBuffer()) {
try {
var result = nano(String(file.contents), assign(options, {
map: (file.sourceMap) ? {annotation: false} : false,
from: file.relative,
to: file.relative
}));
if (result.map && file.sourceMap) {
applySourceMap(file, String(result.map));
file.contents = new Buffer(result.css);
} else {
file.contents = new Buffer(result);
}
this.push(file);
} catch (e) {
var p = new PluginError(PLUGIN_NAME, e, {fileName: file.path});
this.emit('error', p);
}
cb();
}
};
return stream;
};