-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform.js
191 lines (156 loc) · 4.53 KB
/
transform.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
const format = require('kindred-shader-formatter')
const staticModule = require('static-module')
const isRequire = require('is-require')()
const combine = require('stream-combiner')
const through = require('through2')
const glslify = require('glslify')
const shortid = require('shortid')
const falafel = require('falafel')
const xtend = require('xtend')
const path = require('path')
const parseOptions = {
ecmaVersion: 6,
sourceType: 'module',
allowReserved: true,
allowReturnOutsideFunction: true,
allowHashBang: true
}
module.exports = transform
function transform (filename, opts) {
return combine(
templateTransform(filename, opts),
fileTransform(filename, opts)
)
}
function templateTransform (filename, transformOpts) {
var stream = through(write, flush)
var cwd = path.dirname(filename)
var buffer = []
return stream
function write (chunk, _, next) {
buffer.push(chunk)
next()
}
function flush () {
var src = buffer.join('')
var requires = []
var queue = 0
if (src.indexOf('kindred-shader') === -1) {
this.push(src)
this.push(null)
return
}
try {
src = falafel(src, parseOptions, function (node) {
scrapeRequires(node)
scrapeTemplates(node)
})
} catch (e) {
return stream.emit('error', e)
}
checkQueue()
function scrapeRequires (node) {
if (!isRequire(node)) return
var args = node.arguments
if (args.length < 1) return
var target = args[0]
if (target.type !== 'Literal') return
var value = target.value
if (value !== 'kindred-shader') return
if (node.parent.type !== 'VariableDeclarator') return
if (node.parent.id.type !== 'Identifier') return
var varName = node.parent.id.name
requires.push(varName)
}
function scrapeTemplates (node) {
if (node.type !== 'TaggedTemplateExpression') return
if (node.tag.type !== 'Identifier') return
if (requires.indexOf(node.tag.name) === -1) return
var textBits = node.quasi.quasis
var expressions = node.quasi.expressions
var replaceMap = []
var combined = []
for (var i = 0; i + 1 < textBits.length; i++) {
var mapping = '_' + shortid.generate()
var expr = expressions[i].source().trim()
replaceMap[expr] = replaceMap[expr] || mapping
combined.push(textBits[i].source())
combined.push(mapping)
}
combined.push(textBits[textBits.length - 1].source())
combined = combined.join('')
queue++
glslify.bundle(combined, {
basedir: cwd,
inline: true
}, function (err, source) {
if (err) return stream.emit('error', err)
var jsReplacement = splitAndFormat(source, replaceMap)
node.update(jsReplacement)
checkQueue(queue--)
}).on('file', function (file) {
stream.emit('file', file)
})
}
function checkQueue () {
if (queue) return
stream.push(src.toString())
stream.push(null)
}
}
}
function fileTransform (filename, transformOpts) {
var cwd = path.dirname(filename)
var stream = staticModule({
'kindred-shader': {
file: replaceStream,
raw: function (vert, frag) {
var prefix = '(require("kindred-shader/raw")('
var args = [JSON.stringify(vert), JSON.stringify(frag)]
return prefix + args.join(',') + '))'
}
}
}, {
vars: {
__dirname: cwd,
__filename: filename
},
varModules: {
path: path
}
})
return stream
function replaceStream (glslFile, opts) {
var replacer = through()
opts = xtend({
basedir: cwd
}, transformOpts || {}, opts || {})
glslify.bundle(glslFile, opts, function (err, bundled) {
if (err) return stream.emit('error', err)
var jsReplacement = splitAndFormat(bundled)
replacer.push(jsReplacement)
replacer.push(null)
}).on('file', function (file) {
stream.emit('file', file)
})
return replacer
}
}
function splitAndFormat (source, replaceMap) {
var vert = JSON.stringify(format.vert(source))
var frag = JSON.stringify(format.frag(source))
var bundled = ''
if (replaceMap) {
Object.keys(replaceMap).forEach(function (value) {
var key = replaceMap[value]
vert = vert.replace(new RegExp(key, 'g'), '"+(' + value + ')+"')
frag = frag.replace(new RegExp(key, 'g'), '"+(' + value + ')+"')
})
}
bundled += 'require("kindred-shader/raw")('
bundled += vert
bundled += ','
bundled += frag
bundled += ')'
return bundled
}