-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
54 lines (40 loc) · 1.5 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
var util = require('util');
// through2 is a thin wrapper around node transform streams
var through2 = require('through2');
var gutil = require('gulp-util');
var PluginError = gutil.PluginError;
var prefixTemplate = '<dom-module id="%s"><template><style>\n';
// Suffix never changes
var suffixString = '\n</style></template></dom-module>';
var suffixBuffer = new Buffer(suffixString);
// Plugin level function(dealing with files)
function polymerizeCss(options) {
// Creating a stream through which each file will pass
return through2.obj(function(file, enc, cb) {
if (file.isNull()) {
// return empty file
return cb(null, file);
}
// Get styleId and set it onto the prefix string
var styleId = typeof options.styleId === 'function' ?
options.styleId(file) : options.styleId;
var prefixString = util.format(prefixTemplate, styleId);
if (file.isBuffer()) {
var prefixBuffer = new Buffer(prefixString);
// concat stuff
var res = Buffer.concat([
prefixBuffer,
file.contents,
suffixBuffer
]);
// set contents onto the vinyl file
file.contents = res;
}
if (file.isStream()) {
throw new PluginError('gulp-polymerize-css', 'Streams not currently supported');
}
cb(null, file);
});
}
// Exporting the plugin main function
module.exports = polymerizeCss;