-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
232 lines (186 loc) · 5.43 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/* eslint-disable no-redeclare */
var hash = require('murmurhash-js/murmurhash3_gc')
var trim = require('glsl-token-whitespace-trim')
var tokenize = require('glsl-tokenizer/string')
var inject = require('glsl-inject-defines')
var defines = require('glsl-token-defines')
var descope = require('glsl-token-descope')
var clean = require('./lib/clean-suffixes')
var string = require('glsl-token-string')
var scope = require('glsl-token-scope')
var depth = require('glsl-token-depth')
var topoSort = require('./lib/topo-sort')
var copy = require('shallow-copy')
module.exports = function (deps) {
return inject(Bundle(deps).src, {
GLSLIFY: 1
})
}
function Bundle (deps) {
if (!(this instanceof Bundle)) return new Bundle(deps)
// Reorder dependencies topologically
deps = topoSort(deps)
this.depList = deps
this.depIndex = indexBy(deps, 'id')
this.exported = {}
this.cache = {}
this.varCounter = 0
this.src = []
for (var i = 0; i < deps.length; i++) {
this.preprocess(deps[i])
}
for (var i = 0; i < deps.length; i++) {
if (deps[i].entry) {
this.src = this.src.concat(this.bundle(deps[i]))
}
}
this.src = string(this.src)
this.src = string(clean(trim(tokenize(this.src))))
}
var proto = Bundle.prototype
proto.preprocess = function (dep) {
var tokens = tokenize(dep.source)
var imports = []
var exports = null
depth(tokens)
scope(tokens)
for (var i = 0; i < tokens.length; i++) {
var token = tokens[i]
if (token.type !== 'preprocessor') continue
if (!glslifyPreprocessor(token.data)) continue
var exported = glslifyExport(token.data)
var imported = glslifyImport(token.data)
if (exported) {
exports = exported[1]
tokens.splice(i--, 1)
} else if (imported) {
var name = imported[1]
var maps = imported[2].split(/\s?,\s?/g)
var path = maps.shift()
.trim()
.replace(/^'|'$/g, '')
.replace(/^"|"$/g, '')
var target = this.depIndex[dep.deps[path]]
imports.push({
name: name,
path: path,
target: target,
maps: toMapping(maps),
index: i
})
tokens.splice(i--, 1)
}
}
var eof = tokens[tokens.length - 1]
if (eof && eof.type === 'eof') {
tokens.pop()
}
if (dep.entry) {
exports = exports || 'main'
}
if (!exports) {
throw new Error(dep.file + ' does not export any symbols')
}
dep.parsed = {
tokens: tokens,
imports: imports,
exports: exports
}
}
proto.bundle = function (entry) {
var resolved = {}
var result = resolve(entry, [])[1]
return result
function resolve (dep, bindings) {
// Compute suffix for module
bindings.sort()
var ident = bindings.join(':') + ':' + dep.id
var suffix = '_' + hash(ident)
if (dep.entry) {
suffix = ''
}
// Test if export is already resolved
var exportName = dep.parsed.exports + suffix
if (resolved[exportName]) {
return [exportName, []]
}
// Initialize map for variable renamings based on bindings
var rename = {}
for (var i = 0; i < bindings.length; ++i) {
var binding = bindings[i]
rename[binding[0]] = binding[1]
}
// Resolve all dependencies
var imports = dep.parsed.imports
var edits = []
for (var i = 0; i < imports.length; ++i) {
var data = imports[i]
var importMaps = data.maps
var importName = data.name
var importTarget = data.target
var importBindings = Object.keys(importMaps).map(function (id) {
var value = importMaps[id]
// floats/ints should not be renamed
if (value.match(/^\d+(?:\.\d+?)?$/g)) {
return [id, value]
}
// properties (uVec.x, ray.origin, ray.origin.xy etc.) should
// have their host identifiers renamed
var parent = value.match(/^([^.]+)\.(.+)$/)
if (parent) {
return [id, (rename[parent[1]] || (parent[1] + suffix)) + '.' + parent[2]]
}
return [id, rename[value] || (value + suffix)]
})
var importTokens = resolve(importTarget, importBindings)
rename[importName] = importTokens[0]
edits.push([data.index, importTokens[1]])
}
// Rename tokens
var parsedTokens = dep.parsed.tokens.map(copy)
var parsedDefs = defines(parsedTokens)
var tokens = descope(parsedTokens, function (local, token) {
if (parsedDefs[local]) return local
if (rename[local]) return rename[local]
return local + suffix
})
// Insert edits
edits.sort(function (a, b) {
return b[0] - a[0]
})
for (var i = 0; i < edits.length; ++i) {
var edit = edits[i]
tokens = tokens.slice(0, edit[0])
.concat(edit[1])
.concat(tokens.slice(edit[0]))
}
resolved[exportName] = true
return [exportName, tokens]
}
}
function glslifyPreprocessor (data) {
return /#pragma glslify:/.test(data)
}
function glslifyExport (data) {
return /#pragma glslify:\s*export\(([^)]+)\)/.exec(data)
}
function glslifyImport (data) {
return /#pragma glslify:\s*([^=\s]+)\s*=\s*require\(([^)]+)\)/.exec(data)
}
function indexBy (deps, key) {
return deps.reduce(function (deps, entry) {
deps[entry[key]] = entry
return deps
}, {})
}
function toMapping (maps) {
if (!maps) return false
return maps.reduce(function (mapping, defn) {
defn = defn.split(/\s?=\s?/g)
var expr = defn.pop()
defn.forEach(function (key) {
mapping[key] = expr
})
return mapping
}, {})
}