-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulp-polymer-style.js
47 lines (34 loc) · 1.2 KB
/
gulp-polymer-style.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
var through = require('through2');
var gutil = require('gulp-util');
var PluginError = gutil.PluginError;
var template =
"<dom-module id=\":moduleName\">\n"+
" <template>\n"+
" <style>\n"+
" :content\n"+
" </style>\n"+
" </template>\n"+
"</dom-module>";
function getModuleName(path) {
var normalizedPath = path.replace(/\\/g, '/');
var index = normalizedPath.lastIndexOf('/');
var fileName = normalizedPath.substring(index+1);
return fileName.split('.')[0].replace(/[_\s]/g, '-')
}
module.exports = function() {
return through.obj(function(file, encoding, callback) {
if(file.isNull()) {
return callback(null, file);
}
if(file.isStream()) {
return callback(new PluginError('gulp-polymer-style', 'Streams not supported yet.'));
}
var contents = file.contents.toString(encoding);
var moduleName = getModuleName(file.path);
var output = template.replace(':moduleName', moduleName)
.replace(':content', contents);
file.contents = new Buffer(output);
file.path = gutil.replaceExtension(file.path, '.html');
callback(null, file);
});
}