forked from Modernizr/gulp-modernizr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
108 lines (84 loc) · 2.16 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
106
107
108
var path = require('path'),
PluginError = require('plugin-error'),
Vinyl = require('vinyl'),
through = require('through2'),
customizr = require('customizr');
module.exports = function (fileName, opt) {
'use strict';
// Set some defaults
var PLUGIN_NAME = 'gulp-modernizr',
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;
// Set crawl to false, Gulp is providing files & data
opt.crawl = false;
// 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;
// Save first file for metadata purposes
var firstFile;
//
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();
}
// Set first file
if (!firstFile) {
firstFile = file;
}
// Save buffer for later use
opt.files.src.push(file);
return callback();
}
function generateModernizr(callback) {
if (opt.files.src.length === 0) {
return callback();
}
// 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: path.join(firstFile.base, fileName),
base: firstFile.base,
cwd: firstFile.cwd,
contents: new Buffer(data.result)
});
// Pass data along
stream.push(file);
return callback();
});
}
stream = through.obj(storeBuffers, generateModernizr);
return stream;
};