Skip to content

Commit 5d52fde

Browse files
committed
add support for recent glslify
fixes #6 The most recent version of glslify (6.1.0) requires that transforms export a sync interface. I have left the async interface as is, for compatiblity with older versions. I have upgrade the tests to run with the newest version of glslify, but before doing that, I asserted that the old tests still pass with an old glslify version. I have also bumped the version in package.json to 3.1.0, increasing the minor version because this is not a breaking change.
1 parent 3302ed4 commit 5d52fde

File tree

3 files changed

+43
-43
lines changed

3 files changed

+43
-43
lines changed

index.js

+21-14
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const path = require('path')
55
const fs = require('fs')
66

77
module.exports = glslifyImport
8+
module.exports.sync = glslifyImport;
89

910
function glslifyImport (file, src, opts, done) {
1011
const tokens = tokenize(src)
@@ -28,29 +29,35 @@ function glslifyImport (file, src, opts, done) {
2829

2930
var basedir = path.dirname(file)
3031

31-
resolve(target, {
32-
basedir: basedir
33-
}, function (err, resolved) {
34-
if (err) return done(err)
35-
36-
fs.readFile(resolved, 'utf8', function (err, contents) {
32+
if (typeof done === 'function') {
33+
resolve(target, { basedir: basedir }, function (err, resolved) {
3734
if (err) return done(err)
3835

39-
contents = modifyRequirePaths(contents, basedir, target)
40-
41-
glslifyImport(resolved, contents, opts, function (err, contents) {
36+
fs.readFile(resolved, 'utf8', function (err, contents) {
4237
if (err) return done(err)
4338

44-
token.data = contents
45-
if (--total) return
39+
contents = modifyRequirePaths(contents, basedir, target)
40+
41+
glslifyImport(resolved, contents, opts, function (err, contents) {
42+
if (err) return done(err)
4643

47-
done(null, string(tokens))
44+
token.data = contents
45+
if (--total) return
46+
47+
done(null, string(tokens))
48+
})
4849
})
4950
})
50-
})
51+
} else {
52+
var resolved = resolve.sync(target, { basedir: basedir });
53+
var contents = fs.readFileSync(resolved, 'utf8');
54+
contents = modifyRequirePaths(contents, basedir, target);
55+
token.data = glslifyImport(resolved, contents, opts);
56+
total--;
57+
}
5158
})(i)
5259

53-
if (!total) return done(null, src)
60+
if (!total) return typeof done === 'function' ? done(null, src) : string(tokens);
5461
}
5562

5663
function modifyRequirePaths (src, basedir, baseTarget) {

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "glslify-import",
3-
"version": "3.0.0",
3+
"version": "3.1.0",
44
"description": "A transform stream for glslify that adds an `import` directive to your shaders.",
55
"main": "index.js",
66
"dependencies": {
@@ -9,7 +9,7 @@
99
"glsl-tokenizer": "^2.0.2"
1010
},
1111
"devDependencies": {
12-
"glslify": "^2.1.2",
12+
"glslify": "^6.1.0",
1313
"tap-spec": "^3.0.0",
1414
"tape": "^4.0.0"
1515
},

test/index.js

+20-27
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,35 @@ const basic = path.join(__dirname, 'fixtures', 'basic-origin.glsl')
77
const importRequire = path.join(__dirname, 'fixtures', 'import-with-require.glsl')
88

99
test('glslify-import: basic', function (t) {
10-
glslify.bundle(basic, {
10+
var src = glslify(basic, {
1111
transform: [ require.resolve('../index.js') ]
12-
}, function (err, src) {
13-
if (err) throw err
14-
15-
t.ok(/\sunaltered\s/.exec(src), 'unaltered variable is unaltered')
16-
t.ok(/basicRequire/.exec(src), 'basic-require.glsl imported dependency is still included')
17-
t.end()
18-
})
12+
});
13+
t.ok(/\sunaltered\s/.exec(src), 'unaltered variable is unaltered')
14+
t.ok(/basicRequire/.exec(src), 'basic-require.glsl imported dependency is still included')
15+
t.end()
1916
})
2017

2118
test('glslify-import: recursive', function (t) {
22-
glslify.bundle(recursive, {
19+
var src = glslify(recursive, {
2320
transform: [ require.resolve('../index.js') ]
24-
}, function (err, src) {
25-
if (err) throw err
21+
});
2622

27-
t.ok(/\sunaltered\s/.exec(src), 'unaltered variable is unaltered')
28-
t.ok(/basicRequire/.exec(src), 'basic-require.glsl imported dependency is still included')
29-
t.ok(!/\#pragma glslify/.exec(src), 'no pragmas remaining')
30-
t.equal(src.match(/main\(\)/g).length, 2, '2 copies imported')
31-
t.equal(src.match(/\#define GLSLIFY/).length, 1, 'only 1 glslify definition')
32-
t.end()
33-
})
23+
t.ok(/\sunaltered\s/.exec(src), 'unaltered variable is unaltered')
24+
t.ok(/basicRequire/.exec(src), 'basic-require.glsl imported dependency is still included')
25+
t.ok(!/\#pragma glslify/.exec(src), 'no pragmas remaining')
26+
t.equal(src.match(/main\(\)/g).length, 2, '2 copies imported')
27+
t.equal(src.match(/\#define GLSLIFY/).length, 1, 'only 1 glslify definition')
28+
t.end()
3429
})
3530

3631
test('glslify-import: require paths modified', function (t) {
37-
glslify.bundle(importRequire, {
32+
var src = glslify(importRequire, {
3833
transform: [ require.resolve('../index.js') ]
39-
}, function (err, src) {
40-
if (err) throw err
34+
});
4135

42-
t.ok(/basicRequire/.exec(src), 'basic-require.glsl imported dependency is still included')
43-
t.ok(/subRequire1/.exec(src), 'subfolder-1/basic-require.glsl imported dependency is still included')
44-
t.ok(/subRequire2/.exec(src), 'subfolder-2/basic-require.glsl imported dependency is still included')
45-
t.ok(!/\#pragma glslify/.exec(src), 'no pragmas remaining')
46-
t.end()
47-
})
36+
t.ok(/basicRequire/.exec(src), 'basic-require.glsl imported dependency is still included')
37+
t.ok(/subRequire1/.exec(src), 'subfolder-1/basic-require.glsl imported dependency is still included')
38+
t.ok(/subRequire2/.exec(src), 'subfolder-2/basic-require.glsl imported dependency is still included')
39+
t.ok(!/\#pragma glslify/.exec(src), 'no pragmas remaining')
40+
t.end()
4841
})

0 commit comments

Comments
 (0)