-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
91 lines (72 loc) · 2.31 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
'use strict';
var Concat = require('concat-with-sourcemaps');
var extend = require('object-assign');
var through = require('through2');
var gutil = require('gulp-util');
var stream = require('stream');
var path = require('path');
var fs = require('fs');
module.exports = function (tattoo, data) {
// Checks that there is a tattoo passed
if (!tattoo) {
throw new gutil.PluginError('gulp-tattoo', '`tattoo` required');
}
function BlockComment(tattoo,extension){
var comment;
if(extension == '.html'){
comment = "<!--\n" + tattoo + "\n-->\n";
}else if(extension == '.css'){
comment = "/* \n" + tattoo + "\n*/\n";
}
return comment
}
function TransformStream(file, enc, cb){
var filename;
var concat;
// Checks that the files passed in Stream or Buffer
if (file.isNull()) {
return cb(null, file);
}
// Defined type of file that is passed in
if (typeof file === 'string') {
filename = file;
} else if (typeof file.path === 'string') {
filename = path.basename(file.path);
} else {
filename = '';
}
var tattooInk;
if(data ===false){
tattooInk = tattoo;
}else{
// Use Gulp Template and add in data object to tattoo
tattooInk = gutil.template(tattoo, extend({file : file, filename: filename}, data));
}
var ext = path.extname(filename);
concat = new Concat(true, filename);
var tattooInk = BlockComment(tattooInk,ext);
if (file.isBuffer()){
concat.add(filename, new Buffer(tattooInk));
}else if(gutil.isStream(file) ){
var stream = through();
stream.write(new Buffer(tattooInk));
stream.on('error', this.emit.bind(this, 'error'));
file.contents = file.contents.pipe(stream);
}
// add sourcemap
concat.add(file.relative, file.contents, file.sourceMap);
// make sure streaming content is preserved
if (file.contents && !gutil.isStream(file.contents)) {
file.contents = concat.content;
}
// apply source map
if (concat.sourceMapping) {
file.sourceMap = JSON.parse(concat.sourceMap);
}
// make sure the file goes through the next gulp plugin
this.push(file);
// tell the stream engine that we are done with this file
cb();
}
return through.obj(TransformStream);
};