forked from stackgl/glsl-min-stream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (49 loc) · 1.61 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
module.exports = minifier
var lang = require('cssauron-glsl')
, is_variable = lang('decl > decllist > ident')
, is_function = lang('decl > function > ident')
, is_named_struct = lang('struct > ident')
, through = require('through')
, shortest = require('shortest')
function minifier(safe_words, mutate_storages) {
safe_words = safe_words || ['main']
var _ = {}
, seen_names = {}
, counter
for(var i = 0, len = safe_words.length; i < len; ++i)
_[safe_words[i]] = true
safe_words = _
counter = shortest()
return through(mutate)
function mutate(node) {
// remove unnecessary grouping operators
if(is_unnecessary_group(node)) return
if(node.parent) for(var current = node.parent; current.parent; current = current.parent) {
if(is_unnecessary_group(current)) {
current.parent.children[current.parent.children.indexOf(current)] = current.children[0]
current.children[0].parent = current.parent
}
}
if(should_mutate(node)) {
var t = node.parent.parent.children[1]
if(mutate_storages || (t.type === 'placeholder' || t.token.data === 'const')) {
var x = seen_names[node.token.data] || counter()
seen_names[node.token.data] = x
node.data = x
}
}
this.emit('data', node)
}
function should_mutate(node) {
var base = (
is_variable(node) ||
is_function(node) ||
is_named_struct(node)
)
return base &&
!safe_words.hasOwnProperty(node.token.data)
}
function is_unnecessary_group(node) {
return node.type === 'group' && node.children[0].lbp >= node.parent.lbp
}
}