-
Notifications
You must be signed in to change notification settings - Fork 29
/
index.js
105 lines (82 loc) · 2.25 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
var PluginError = require('plugin-error');
var Vinyl = require('vinyl');
var through = require('through2');
var customizr = require('customizr');
module.exports = function(fileName, opt) {
'use strict';
// Set some defaults
var PLUGIN_NAME = 'gulp-modernizr';
var DEFAULT_FILE_NAME = 'modernizr.js';
// Ensure fileName exists
if (typeof fileName === 'undefined') {
fileName = opt ? opt.dest : DEFAULT_FILE_NAME;
} else if (typeof fileName === typeof {}) {
opt = fileName;
fileName = DEFAULT_FILE_NAME;
}
// Ensure opt exists
opt = opt || {};
// Enable string parsing in customizr
opt.useBuffers = true;
// Ensure crawl exists
if (opt.crawl === undefined) {
opt.crawl = true;
}
// Reset opt.files. Store buffers here.
opt.files = {
src: [],
};
// Per Gulp docs (https://github.com/gulpjs/gulp/blob/master/docs/writing-a-plugin/guidelines.md)
// "Your plugin shouldn't do things that other plugins are responsible for"
opt.uglify = false;
var stream;
function storeBuffers(file, enc, callback) {
// Return if null
if (file.isNull()) {
stream.push(file);
return callback();
}
// No stream support (yet?)
if (file.isStream()) {
stream.emit('error', new PluginError({
plugin: PLUGIN_NAME,
message: 'Streaming not supported',
}));
return callback();
}
// Save buffer for later use
opt.files.src.push(file);
return callback();
}
function generateModernizr(callback) {
if (opt.crawl && opt.files.src.length === 0) {
return callback();
}
// Remove files if crawl is set to false
if (!opt.crawl) {
opt.files.src = [];
}
// Call customizr
customizr(opt, function(data) {
// Sanity check
if (!data.result) {
return stream.emit('error', new PluginError({
plugin: PLUGIN_NAME,
message: 'No data returned',
}));
}
// Save result
var file = new Vinyl({
path: fileName,
base: undefined,
cwd: '',
contents: Buffer.from(data.result),
});
// Pass data along
stream.push(file);
return callback();
});
}
stream = through.obj(storeBuffers, generateModernizr);
return stream;
};