-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
96 lines (82 loc) · 3.37 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
'use strict';
var glslunit = require('./lib/glsl-compiler'),
through = require('through'),
fs = require('fs'),
path = require('path'),
falafel = require('falafel'),
path = require('path'),
callerPath = require('caller-path');
module.exports = function(a, b) {
var base = callerPath();
if (base.match(new RegExp('node_modules\\' + path.sep + '(browserify|module-deps)'))) {
return browserify(a);
} else {
return node(a, b, base);
}
};
function browserify(file) {
var data = '',
dirname = path.dirname(file);
var stream = through(
function write (buf) { data += buf; },
function end() {
try {
var output = falafel(data, function (node) {
if (isRequireFor(node, 'glify')) {
node.update('undefined');
}
if (isCallFor(node, 'glify')) {
var filePath = path.join(dirname, getArgOfType(node, 0, 'Literal')),
fragmentPath = filePath.replace('.*.', '.fragment.'),
fragment = fs.readFileSync(fragmentPath, 'utf8'),
vertexPath = filePath.replace('.*.', '.vertex.'),
vertex = fs.readFileSync(vertexPath, 'utf8'),
prepend = getArgOfType(node, 1, 'Literal') || '';
prepend = 'precision mediump float;\n' + prepend;
try {
var compiled = compile(prepend + vertex, prepend + fragment);
node.update(JSON.stringify(compiled));
} catch(e) {
stream.emit('error', 'Error compiling ' + filePath + '\n' + e);
}
stream.emit('file', fragmentPath);
stream.emit('file', vertexPath);
}
});
this.queue(String(output));
this.queue(null);
} catch(e) {
stream.emit('error', 'Error falafeling ' + file + '\n' + e);
}
}
);
return stream;
}
function node(filePath, prepend, base) {
var fragmentPath = path.resolve(base, '..', filePath.replace('.*.', '.fragment.')),
fragment = fs.readFileSync(fragmentPath, 'utf8'),
vertexPath = path.resolve(base, '..', filePath.replace('.*.', '.vertex.')),
vertex = fs.readFileSync(vertexPath, 'utf8');
prepend = prepend || '';
prepend = '#version 120\n' + prepend;
return compile(prepend + vertex, prepend + fragment);
}
function compile(vertex, fragment) {
var compiler = new glslunit.compiler.DemoCompiler(vertex, fragment),
result = compiler.compileProgram();
return {
vertex: glslunit.Generator.getSourceCode(result.vertexAst),
fragment: glslunit.Generator.getSourceCode(result.fragmentAst)
};
}
function isCallFor(node, name) {
var callee = node.callee;
return node.type == 'CallExpression' && callee.type == 'Identifier' && callee.name == name;
}
function isRequireFor(node, moduleName) {
return isCallFor(node, 'require') && getArgOfType(node, 0, 'Literal') == moduleName;
}
function getArgOfType(node, index, type) {
var args = node.arguments;
return args[index] && args[index].type == type && args[index].value;
}