This repository has been archived by the owner on Feb 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
54 lines (48 loc) · 1.65 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
const PluginError = require('plugin-error');
const through = require('through2');
const crass = require('crass');
/**
* Creates a PluginError with the provided message
*
* @param {string} details Contains further details regarding the issue
* @return {PluginError}
*/
const withError = details => new PluginError('gulp-crass-minifier', details);
module.exports = (options = {}) => through.obj(function (file, encoding, done) {
// Ignore empty files
if (file === null) return done(null, file);
// Streams are not supported. Suggest to use gulp-buffer or vinyl-buffer.
if (file.isStream()) {
return done(withError('Streaming is not supported. You might want to use gulp-buffer or vinyl-buffer to get around this issue.'));
}
// Merge options with default settings
options = Object.assign({
optimize: true,
pretty: false,
css4: false,
o1: true,
saveie: false,
browser_min: {
chrome: 39,
firefox: 31,
ie: 11,
opera: 26,
}
}, options);
// Minify CSS using Crass
try {
const parsedCode = crass.parse(file.contents.toString());
if (options.optimize) {
if (options.pretty) {
file.contents = new Buffer.from(parsedCode.optimize(options).pretty());
} else {
file.contents = new Buffer.from(parsedCode.optimize(options).toString());
}
} else if (options.pretty) {
file.contents = new Buffer.from(parsedCode.pretty());
}
return done(null, file);
} catch (error) {
return done(withError(error));
}
});