-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathglslify-babel.js
207 lines (191 loc) · 6.6 KB
/
glslify-babel.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
var dirname = require('path').dirname
var glslifyHack = require('./lib/glslify-sync-hack')
module.exports = function (babel) {
function resolveImport (identifier, declaration) {
if (declaration.type === 'ImportDeclaration' &&
declaration.source &&
declaration.source.type === 'StringLiteral') {
return declaration.source.value
}
return ''
}
function resolveRequire (path, identifier, declaration) {
if (declaration.type === 'VariableDeclaration') {
var children = declaration.declarations
for (var i = 0; i < children.length; ++i) {
if (children[i].id === identifier) {
var rhs = children[i].init
if (rhs &&
rhs.type === 'CallExpression' &&
rhs.callee.type === 'Identifier' &&
rhs.callee.name === 'require' &&
!path.scope.getBinding('require') &&
rhs.arguments.length === 1 &&
rhs.arguments[0].type === 'StringLiteral') {
return rhs.arguments[0].value
}
}
}
}
return ''
}
function resolveModule (path, name) {
var binding = path.scope.getBinding(name)
if (!binding) {
return ''
}
if (!binding.constant) {
return ''
}
switch (binding.kind) {
case 'module':
return resolveImport(binding.identifier, binding.path.parent)
case 'var':
case 'let':
case 'const':
return resolveRequire(path, binding.identifier, binding.path.parent)
default:
return ''
}
}
function evalConstant (env, path, expression) {
if (!expression) {
throw new Error('glslify-binding: invalid expression')
}
switch (expression.type) {
case 'StringLiteral':
case 'BooleanLiteral':
case 'NumericLiteral':
case 'NullLiteral':
return expression.value
case 'TemplateLiteral':
var quasis = expression.quasis
return expression.expressions.reduce(function (prev, expr, i) {
prev.push(
evalConstant(env, path, expr),
quasis[i].value.cooked)
return prev
}, [quasis[0].value.cooked]).join('')
case 'Identifier':
// TODO handle __dirname and other constants here
var binding = path.scope.getBinding(expression.name)
if (!binding) {
if (expression.name === '__dirname') {
return env.cwd
}
}
throw new Error('glslify-babel: cannot resolve glslify(), unknown id')
case 'UnaryExpression':
var value = evalConstant(env, path, expression.argument)
switch (expression.operator) {
case '+': return +value
case '-': return -value
case '!': return !value
case '~': return ~value
case 'typeof': return typeof value
case 'void': return void value
}
throw new Error('glslify-babel: unsupported unary operator')
case 'BinaryExpression':
var left = evalConstant(env, path, expression.left)
var right = evalConstant(env, path, expression.right)
switch (expression.operator) {
case '+': return left + right
case '-': return left - right
case '*': return left * right
case '&': return left & right
case '/': return left / right
case '%': return left % right
case '|': return left | right
case '^': return left ^ right
case '||': return left || right
case '&&': return left && right
case '<<': return left << right
case '>>': return left >> right
case '>>>': return left >>> right
case '===': return left === right
case '!==': return left !== right
case '<': return left < right
case '>': return right < left
case '<=': return left <= right
case '>=': return left >= right
}
throw new Error('glslify-babel: unsupported binary expression')
case 'ObjectExpression':
return expression.properties.reduce(function (result, property) {
if (property.type !== 'ObjectProperty') {
throw new Error('glslify-babel: expected object property')
}
var value = evalConstant(env, path, property.value)
var key = property.key
if (key.type === 'Identifier') {
result[key.name] = value
} else if (key.type === 'StringLiteral') {
result[key.value] = value
} else {
throw new Error('glslify-babel: invalid property type')
}
return result
}, {})
case 'ArrayExpression':
return expression.elements.map(function (prop) {
if (!prop) {
return void 0
}
return evalConstant(env, path, prop)
})
default:
throw new Error('glslify-babel: cannot resolve glslify() call')
}
}
return {
visitor: {
TaggedTemplateExpression: function (path, state) {
var node = path.node
var tag = node.tag
if (tag.type !== 'Identifier' ||
resolveModule(path, tag.name) !== 'glslify') {
return
}
var filename = state.file.log.filename
var cwd = dirname(filename)
var env = {
cwd: cwd
}
var stringInput = evalConstant(env, path, node.quasi)
if (typeof stringInput !== 'string') {
throw new Error('glslify-babel: invalid string template')
}
var result = glslifyHack(cwd, stringInput, { inline: true })
path.replaceWith(babel.types.stringLiteral(result))
},
CallExpression: function (path, state) {
var node = path.node
var callee = node.callee
if (callee.type !== 'Identifier' ||
resolveModule(path, callee.name) !== 'glslify' ||
node.arguments.length < 1) {
return
}
var filename = state.file.log.filename
var cwd = dirname(filename)
var env = {
cwd: cwd
}
var stringInput = evalConstant(env, path, node.arguments[0])
if (typeof stringInput !== 'string') {
throw new Error('glslify-babel: first argument must be a string')
}
var optionInput = {}
if (node.arguments.length >= 2) {
optionInput = evalConstant(env, path, node.arguments[1])
}
if (typeof optionInput !== 'object' || !optionInput) {
throw new Error('glslify-babel: invalid option input')
}
var result = glslifyHack(cwd, stringInput, optionInput)
path.replaceWith(babel.types.stringLiteral(result))
}
}
}
}