Skip to content

Commit f2e6c2b

Browse files
committed
support for glslify@2 :)
1 parent e580216 commit f2e6c2b

11 files changed

+101
-50
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
*.log
3+
.DS_Store
4+
bundle.js

.npmignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
node_modules
2+
*.log
3+
.DS_Store
4+
bundle.js
5+
test
6+
test.js
7+
demo
8+
example
9+
.npmignore

LICENSE.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
The MIT License (MIT)
2+
=====================
3+
4+
Copyright (c) 2014 Hugh Kennedy
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
7+
8+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9+
10+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# glslify-import #
22

3-
A transform stream for [glslify](http://github.com/chrisdickinson/glslify)
3+
A transform stream for [glslify](http://github.com/stackgl/glslify)
44
that adds an `import` directive to your shaders.
55

66
## Usage ##
@@ -9,7 +9,7 @@ Given a basic shader:
99

1010
``` glsl
1111
// main.frag
12-
#pragma import: ./common.glsl
12+
#pragma glslify: import('./common.glsl')
1313
1414
void main() {
1515
gl_FragColor = vec4(color, 1.0);

index.js

+38-39
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,46 @@
1-
var combine = require('stream-combiner')
2-
var glslify = require('glslify-stream')
3-
var concat = require('concat-stream')
4-
var map = require('map-stream')
5-
var fs = require('graceful-fs')
6-
var split = require('split')
7-
var eol = require('os').EOL
1+
const string = require('glsl-token-string')
2+
const tokenize = require('glsl-tokenizer')
3+
const resolve = require('glsl-resolve')
4+
const path = require('path')
5+
const fs = require('fs')
86

9-
module.exports = include
7+
module.exports = glslifyImport
108

11-
function include(file) {
12-
var stream = combine(split(), map(write))
9+
function glslifyImport(file, src, opts, done) {
10+
const tokens = tokenize(src)
1311

14-
return stream
12+
var total = 0
1513

16-
function write(line, next) {
17-
var cache = {}
18-
var match
14+
for (var i = 0; i < tokens.length; i++) (function(i) {
15+
var token = tokens[i]
16+
if (token.type !== 'preprocessor') return
1917

20-
line = String(line)
21-
line = line.replace(/^\s*?#pragma\s+import\:(.+)$/g, function(full, name) {
22-
match = name.trim()
23-
return ''
24-
})
18+
var imported = /#pragma glslify:\s*import\(([^\)]+)\)/.exec(token.data)
19+
if (!imported) return
20+
if (!imported[1]) return
21+
22+
var target = imported[1]
23+
.trim()
24+
.replace(/^'|'$/g, '')
25+
.replace(/^"|"$/g, '')
26+
27+
total++
28+
29+
resolve(target, {
30+
basedir: path.dirname(file)
31+
}, function(err, resolved) {
32+
if (err) return done(err)
2533

26-
if (!match) return next(null, line + eol)
27-
28-
glslify.resolve(file, match, function(err, dest) {
29-
if (err) return stream.emit('error', err)
30-
if (cache[dest]) return next(null, cache[dest])
31-
32-
// Handle imports recursively too!
33-
fs.createReadStream(dest)
34-
.on('error', error)
35-
.pipe(include(dest))
36-
.on('error', error)
37-
.pipe(concat(function(data) {
38-
next(null, cache[dest] = data + eol)
39-
}))
40-
.on('error', error)
41-
42-
function error(err) {
43-
return stream.emit('error', err)
44-
}
34+
fs.readFile(resolved, 'utf8', function(err, contents) {
35+
if (err) return done(err)
36+
37+
token.data = contents
38+
if (--total) return
39+
40+
done(null, string(tokens))
41+
})
4542
})
46-
}
43+
})(i)
44+
45+
if (!total) return done(null, src)
4746
}

package.json

+9-9
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
"description": "A transform stream for glslify that adds an `import` directive to your shaders.",
55
"main": "index.js",
66
"dependencies": {
7-
"map-stream": "0.0.3",
8-
"split": "~0.2.10",
9-
"stream-combiner": "0.0.2",
10-
"concat-stream": "~1.0.1",
11-
"glsl-deparser": "0.0.2",
12-
"graceful-fs": "~2.0.1",
13-
"glslify-stream": "0.0.1"
7+
"glsl-resolve": "0.0.1",
8+
"glsl-token-string": "^1.0.1",
9+
"glsl-tokenizer": "^2.0.2"
10+
},
11+
"devDependencies": {
12+
"glslify": "^2.1.2",
13+
"tap-spec": "^3.0.0",
14+
"tape": "^4.0.0"
1415
},
15-
"devDependencies": {},
1616
"scripts": {
17-
"test": "echo \"Error: no test specified\" && exit 1"
17+
"test": "node test | tap-spec"
1818
},
1919
"author": "Hugh Kennedy <hughskennedy@gmail.com> (http://hughskennedy.com/)",
2020
"license": "MIT",

test/fixtures/basic-import-1.glsl

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const float unaltered = 1.0;

test/fixtures/basic-import-2.glsl

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#pragma glslify: imported = require('./basic-require')

test/fixtures/basic-origin.glsl

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#pragma glslify: import('./basic-import-1.glsl')
2+
#pragma glslify: import('./basic-import-2.glsl')
3+
4+
void main() {
5+
gl_FragCoord = imported();
6+
}

test/fixtures/basic-require.glsl

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
vec4 basicRequire() {
2+
return vec4(1, 0, 0, 1);
3+
}
4+
5+
#pragma glslify: export(basicRequire)

test/index.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const glslify = require('glslify')
2+
const path = require('path')
3+
const test = require('tape')
4+
const origin = path.join(__dirname, 'fixtures', 'basic-origin.glsl')
5+
6+
test('glslify-import', function(t) {
7+
glslify.bundle(origin, {
8+
transform: [ require.resolve('../index.js') ]
9+
}, function(err, src) {
10+
if (err) throw err
11+
12+
t.ok(/\sunaltered\s/.exec(src), 'unaltered variable is unaltered')
13+
t.ok(/basicRequire/.exec(src), 'basic-require.glsl imported dependency is still included')
14+
t.end()
15+
})
16+
})

0 commit comments

Comments
 (0)