-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathglslify-loader.js
60 lines (52 loc) · 1.8 KB
/
glslify-loader.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
const path = require('path')
const loaderUtils = require('loader-utils')
const resolve = require('resolve')
const deps = require('glslify-deps')
const bundle = require('glslify-bundle')
module.exports = function glslifyLoader (content) {
this.cacheable && this.cacheable()
const depper = deps()
const callback = this.async()
// Setup options
const options = Object.assign({
basedir: path.dirname(this.resourcePath),
transform: []
}, loaderUtils.getOptions(this))
// Handle transforms from options
const transforms = Array.isArray(options.transform) ? options.transform : []
const postTransforms = []
transforms.forEach(transform => {
if (!Array.isArray(transform)) transform = [String(transform)]
const name = transform[0]
const opts = transform[1] || {}
// Keep post-transforms for later
if (opts.post) postTransforms.push({ name, opts })
else depper.transform(name, opts)
})
// Build the dependency graph
depper.inline(content, options.basedir, (err, tree) => {
if (err) return error(err)
// Make webpack watch each subdependencies
tree && tree.forEach(file => !file.entry && this.addDependency(file.file))
// Bundle the glsl output
const output = String(bundle(tree))
// Start applying post transforms
nextPostTransform(null, output)
})
// Iterate over each post transforms
function nextPostTransform (err, output) {
if (err) return error(err)
const transform = postTransforms.shift()
if (!transform) return done(output)
resolve(transform.name, { basedir: options.basedir }, (err, target) => {
if (err) return error(err)
require(target)(null, output, transform.opts, nextPostTransform)
})
}
function error (err) {
callback(err, null)
}
function done (output) {
callback(null, output)
}
}