Skip to content

Commit c811ac4

Browse files
author
Matthias Wolff
committed
added initial shader minification draft
1 parent 8f29ae3 commit c811ac4

File tree

2 files changed

+77
-61
lines changed

2 files changed

+77
-61
lines changed

index.js

Lines changed: 73 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,95 @@
11
var path = require('path')
22
var fs = require('fs')
33
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')
49

510
var chunks = {}
611
var chunkPath = ""
712

8-
module.exports = function(source) {
9-
this.cacheable && this.cacheable()
13+
module.exports = function (source) {
14+
this.cacheable && this.cacheable()
1015

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
1621

17-
var options = loaderUtils.getOptions(this) || {}
22+
var options = loaderUtils.getOptions(this) || {}
1823

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+
}
2227

23-
var r = /\$[\w-.]+/gi
24-
var match = source.match( r )
28+
var r = /\$[\w-.]+/gi
29+
var match = source.match(r)
2530

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+
}
3338

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
4146

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)
4449

45-
}
50+
}
4651
}
4752

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+
})
6368

6469
}
6570

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+
}
8085

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+
});
8395
}

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
},
2525
"homepage": "https://github.com/Makio64/shader-loader",
2626
"dependencies": {
27+
"glsl-deparser": "^1.0.0",
28+
"glsl-min-stream": "git+ssh://[email protected]:33669/glsl-min-stream.git#master",
29+
"glsl-parser": "^2.0.0",
30+
"glsl-tokenizer": "git+ssh://[email protected]:33669/glsl-tokenizer.git#master",
2731
"loader-utils": "^1.1.0"
2832
}
2933
}

0 commit comments

Comments
 (0)