-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
86 lines (77 loc) · 1.87 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
var through = require('through');
/**
* Default extensions
*/
middleware.extensions = [
"c"
, "glsl"
, "vert"
, "frag"
, "vs"
, "fs"
, "gs"
, "vsh"
, "fsh"
, "gsh"
, "vshader"
, "fshader"
, "gshader"
];
/**
* create middleware for handling shader files
*/
function middleware(file, options) {
var matcher = new RegExp("\\.(?:"+ middleware.extensions.join("|")+")$")
if (!matcher.test(file)) return through();
var input = '';
var write = function (buffer) {
input += buffer;
}
var end = function () {
this.queue(createModule(input, options));
this.queue(null);
}
return through(write, end);
}
/**
* create module for the loaded shader
*/
function createModule(body, options) {
body = middleware.sanitize(body)
if (String(options.parameterize) !== 'false') {
body = middleware.parameterise(body)
}
if (options.module === "es6" || options.module === "es2015") {
var module = 'export default ' + body + ';\n';
}
else {
var module = 'module.exports = ' + body + ';\n';
}
return module
};
/**
* create handler function for parameterising the shader
*/
middleware.parameterise = function(text) {
function parse(params){
var template = text
params = params || {}
for(var key in params) {
var matcher = new RegExp("{{"+key+"}}","g")
template = template.replace(matcher, params[key])
}
return template
}
return parse.toString().replace("text", text);
}
/**
* sanitise text input so that it can be inlined in a method
*/
middleware.sanitize = function(text){
text = text.replace(/\"/g, '\u005C\u0022')
text = text.replace(/^(.*)/gm, '"$1')
text = text.replace(/(.+)$/gm, '$1 \\n" +')
text = text.replace(/\+$/, '')
return text
}
module.exports = middleware