|
1 | 1 | var path = require('path') |
2 | 2 | var fs = require('fs') |
3 | 3 | var loaderUtils = require('loader-utils') |
| 4 | +var Readable = require('stream').Readable |
| 5 | +var Tokenizer = require('glsl-tokenizer/stream') |
| 6 | +var Parser = require('glsl-parser/stream') |
| 7 | +var Deparser = require('glsl-deparser') |
| 8 | +var Minify = require('glsl-min-stream') |
4 | 9 |
|
5 | 10 | var chunks = {} |
6 | 11 | var chunkPath = "" |
7 | 12 |
|
8 | | -module.exports = function(source) { |
9 | | - this.cacheable && this.cacheable() |
| 13 | +module.exports = function (source) { |
| 14 | + this.cacheable && this.cacheable() |
10 | 15 |
|
11 | | - var uid = new Object() |
12 | | - uid.callback = this.async() |
13 | | - uid.finalString = source |
14 | | - uid.queue = 0 |
15 | | - uid.done = false |
| 16 | + var uid = new Object() |
| 17 | + uid.callback = this.async() |
| 18 | + uid.finalString = source |
| 19 | + uid.queue = 0 |
| 20 | + uid.done = false |
16 | 21 |
|
17 | | - var options = loaderUtils.getOptions(this) || {} |
| 22 | + var options = loaderUtils.getOptions(this) || {} |
18 | 23 |
|
19 | | - if(options.glsl && options.glsl.chunkPath){ |
20 | | - chunkPath = options.glsl.chunkPath |
21 | | - } |
| 24 | + if (options.glsl && options.glsl.chunkPath) { |
| 25 | + chunkPath = options.glsl.chunkPath |
| 26 | + } |
22 | 27 |
|
23 | | - var r = /\$[\w-.]+/gi |
24 | | - var match = source.match( r ) |
| 28 | + var r = /\$[\w-.]+/gi |
| 29 | + var match = source.match(r) |
25 | 30 |
|
26 | | - if(match){ |
27 | | - for (var i = 0; i < match.length; i++) { |
28 | | - chunks[match[i]] = "" |
29 | | - } |
30 | | - } else { |
31 | | - onChunksLoaded.call(uid) |
32 | | - } |
| 31 | + if (match) { |
| 32 | + for (var i = 0; i < match.length; i++) { |
| 33 | + chunks[match[i]] = "" |
| 34 | + } |
| 35 | + } else { |
| 36 | + onChunksLoaded.call(uid) |
| 37 | + } |
33 | 38 |
|
34 | | - var keys = [] |
35 | | - for (var key in chunks){ |
36 | | - keys.push(key) |
37 | | - } |
38 | | - keys.sort() |
39 | | - keys.reverse() |
40 | | - uid.queue+=keys.length |
| 39 | + var keys = [] |
| 40 | + for (var key in chunks) { |
| 41 | + keys.push(key) |
| 42 | + } |
| 43 | + keys.sort() |
| 44 | + keys.reverse() |
| 45 | + uid.queue += keys.length |
41 | 46 |
|
42 | | - for(var i=0; i < keys.length; i++){ |
43 | | - loadChunk.call(this, keys[i], uid ) |
| 47 | + for (var i = 0; i < keys.length; i++) { |
| 48 | + loadChunk.call(this, keys[i], uid) |
44 | 49 |
|
45 | | - } |
| 50 | + } |
46 | 51 | } |
47 | 52 |
|
48 | | -function loadChunk(key, uid){ |
49 | | - var name = key.substr(1, key.length-1) |
50 | | - var headerPath = path.resolve(chunkPath+"/"+name+".glsl"); |
51 | | - this.dependency(headerPath) |
52 | | - fs.readFile(headerPath, "utf-8", function(err, content) { |
53 | | - uid.queue-- |
54 | | - chunks[key]=content |
55 | | - if(err) { |
56 | | - chunks[key]="" |
57 | | - console.error("Can't open the shader chunk "+name+":\n"+err.toString()) |
58 | | - } |
59 | | - if(uid.queue==0){ |
60 | | - onChunksLoaded.call(uid) |
61 | | - } |
62 | | - }) |
| 53 | +function loadChunk (key, uid) { |
| 54 | + var name = key.substr(1, key.length - 1) |
| 55 | + var headerPath = path.resolve(chunkPath + "/" + name + ".glsl"); |
| 56 | + this.dependency(headerPath) |
| 57 | + fs.readFile(headerPath, "utf-8", function (err, content) { |
| 58 | + uid.queue-- |
| 59 | + chunks[key] = content |
| 60 | + if (err) { |
| 61 | + chunks[key] = "" |
| 62 | + console.error("Can't open the shader chunk " + name + ":\n" + err.toString()) |
| 63 | + } |
| 64 | + if (uid.queue == 0) { |
| 65 | + onChunksLoaded.call(uid) |
| 66 | + } |
| 67 | + }) |
63 | 68 |
|
64 | 69 | } |
65 | 70 |
|
66 | | -function onChunksLoaded(){ |
67 | | - if(this.done){ return } |
68 | | - this.done = true |
69 | | - var keys = [] |
70 | | - for (var key in chunks){ |
71 | | - keys.push(key) |
72 | | - } |
73 | | - keys.sort() |
74 | | - keys.reverse() |
75 | | - for(var i=0; i < keys.length; i++){ |
76 | | - var key = keys[i] |
77 | | - var re = new RegExp("(\\"+key+")", "gi") |
78 | | - this.finalString = this.finalString.replace(re,chunks[key]) |
79 | | - } |
| 71 | +function onChunksLoaded () { |
| 72 | + if (this.done) { return } |
| 73 | + this.done = true |
| 74 | + var keys = [] |
| 75 | + for (var key in chunks) { |
| 76 | + keys.push(key) |
| 77 | + } |
| 78 | + keys.sort() |
| 79 | + keys.reverse() |
| 80 | + for (var i = 0; i < keys.length; i++) { |
| 81 | + var key = keys[i] |
| 82 | + var re = new RegExp("(\\" + key + ")", "gi") |
| 83 | + this.finalString = this.finalString.replace(re, chunks[key]) |
| 84 | + } |
80 | 85 |
|
81 | | - this.finalString = "module.exports = " + JSON.stringify(this.finalString) |
82 | | - this.callback(null, this.finalString) |
| 86 | + var s = new Readable() |
| 87 | + s._read = function noop () {} |
| 88 | + s.push(this.finalString) |
| 89 | + s.push(null) |
| 90 | + this.res = '' |
| 91 | + s.pipe(Tokenizer()).pipe(Parser()).pipe(Minify(['main', 'x', 'y', 'z', 'texCoord0', 'bakedTc'], false)).pipe(Deparser(false)).on('data', function (chunk) { this.res += chunk }).on('end', function () { |
| 92 | + this.finalString = "module.exports = " + JSON.stringify(this.res) |
| 93 | + this.callback(null, this.finalString) |
| 94 | + }); |
83 | 95 | } |
0 commit comments