forked from kentliau/gulp-dot-precompiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
122 lines (105 loc) · 3.41 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
var through = require('through2');
var dot = require('dot');
var gutil = require('gulp-util');
var _ = require('lodash');
var path = require('path');
var PluginError = gutil.PluginError;
var fs = require('fs');
var defs = {};
const PLUGIN_NAME = 'gulp-dot-precompiler';
function getTemplateName(root, name, extension, separator) {
var parts = name.split(path.sep);
if(root.length !== 0 )
{
parts.unshift(root);
}
if(extension.length !== 0)
{
parts[parts.length-1] = parts[parts.length-1] + extension;
}
return parts.join(separator);
}
function getTemplateCode(content,templateSettings,defs) {
var compiled;
try {
compiled = dot.template(content,templateSettings,defs).toString();
} catch(err){
console.log(err);
return err;
}
return compiled;
}
function readStream(stream, done) {
var buffer = '';
stream.on('data', function (chunk) {
buffer += chunk;
}).on('end', function () {
done(null, buffer);
}).on('error', function (error) {
done(error);
});
}
function gulpDotify(options) {
options = options || {};
_.defaults(options, {
root: '',
separator: '.',
extension: '',
dictionary: 'render',
formatter: function(root, name, extension, separator) {
var parts = name.split(path.sep);
if (root.length) {
parts.unshift(root);
}
if (extension.length) {
parts[parts.length-1] = parts[parts.length - 1] + extension;
}
return parts.join(separator);
},
//doT.js setting
templateSettings: {
evaluate: /\{\{([\s\S]+?(\}?)+)\}\}/g,
interpolate: /\{\{=([\s\S]+?)\}\}/g,
encode: /\{\{!([\s\S]+?)\}\}/g,
use: /\{\{#([\s\S]+?)\}\}/g,
useParams: /(^|[^\w$])def(?:\.|\[[\'\"])([\w$\.]+)(?:[\'\"]\])?\s*\:\s*([\w$\.]+|\"[^\"]+\"|\'[^\']+\'|\{[^\}]+\})/g,
define: /\{\{##\s*([\w\.$]+)\s*(\:|=)([\s\S]+?)#\}\}/g,
defineParams: /^\s*([\w$]+):([\s\S]+)/,
conditional: /\{\{\?(\?)?\s*([\s\S]*?)\s*\}\}/g,
iterate: /\{\{~\s*(?:\}\}|([\s\S]+?)\s*\:\s*([\w$]+)\s*(?:\:\s*([\w$]+))?\s*\}\})/g,
varname: 'data',
strip: true,
append: true,
selfcontained: false
}
});
var stream = through.obj(function (file, enc, callback) {
var complete = function (error, contents) {
if (error) {
throw new PluginError(PLUGIN_NAME, error);
}
defs.loadfile = function(include_path) {
current_path = (file.path).substr(0, (file.path).lastIndexOf('/')+1 );
return fs.readFileSync(current_path + include_path);
};
var relative_path = file.relative;
var trimmed_ext = relative_path.substr(0, relative_path.lastIndexOf('.')) || relative_path;
var name = options.formatter(options.root, trimmed_ext, options.extension, options.separator);
var code = getTemplateCode(contents,options.templateSettings,defs);
if(typeof code !== "string")
{
this.emit('error', new PluginError(PLUGIN_NAME, code));
}
file.contents = new Buffer([options.dictionary, '["', name, '"] = ', code, ';'].join(''));
this.push(file);
return callback();
}.bind(this);
if (file.isBuffer()) {
complete(null, file.contents.toString());
} else if (file.isStream()) {
readStream(file.contents, complete);
}
});
return stream;
};
module.exports = gulpDotify;