Skip to content

Get glslify working with ify-loader #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Sep 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions fixtures/glsl/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const glsl = require('glslify')

console.log(glsl`
precision mediump float;
#pragma glslify: ones = require(./ones)
void main () {
gl_FragColor = ones();
}
`)
4 changes: 4 additions & 0 deletions fixtures/glsl/ones.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
vec4 ones () {
return vec4(1);
}
#pragma glslify: export(ones)
12 changes: 12 additions & 0 deletions fixtures/glsl/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

precision mediump float;
#define GLSLIFY 1
#define GLSLIFY 1
vec4 ones () {
return vec4(1);
}

void main () {
gl_FragColor = ones();
}
,
Copy link
Contributor Author

@rreusser rreusser Sep 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know why this comma exists in the output, but perhaps it's nothing problematic. A large library with lots of glsl like plotly.js compiles fine, so I'm led to believe there's not a fundamental problem here. I'm going to check it off since I'm not concerned enough to investigate too deeply, but if it looks fishy to someone else, I'm glad to dig a bit.

12 changes: 12 additions & 0 deletions fixtures/glsl/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "fixture",
"version": "1.0.0",
"scripts": {
"build": "../../node_modules/.bin/webpack webpack.config.js"
},
"browserify": {
"transform": [
"glslify"
]
}
}
15 changes: 15 additions & 0 deletions fixtures/glsl/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const path = require('path')

module.exports = {
entry: './index.js',
output: {
path: __dirname,
filename: 'bundle.js'
},
module: {
rules: [{
test: /\.js/,
use: path.join(__dirname, '..', '..')
}]
}
}
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ function loader (source) {

const name = transform[0]
const opts = transform[1] || {}
opts._flags = opts._flags || []

if (typeof name === 'function') {
return next(null, name(filename, opts))
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
},
"devDependencies": {
"brfs": "^1.4.2",
"from2-string": "^1.1.0",
"glslify": "^6.1.0",
"npm-which": "^2.0.0",
"regl": "^1.3.0",
"standard": "^5.4.1",
"tap-spec": "^4.1.1",
"tape": "^4.4.0",
"through2": "^2.0.0",
"webpack": "^2.2.1"
"webpack": "^3.4.0"
},
"scripts": {
"test": "node test.js | tspec",
Expand Down
36 changes: 36 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,39 @@ test('error handling', function (t) {
t.equal(code, 2, 'exit code was 2')
})
})

test('glsl-transform', function (t) {
const wpack = which.sync('webpack', { cwd: __dirname })
const output = path.join(__dirname, 'fixtures', 'glsl', 'bundle.js')
const config = path.join(__dirname, 'fixtures', 'glsl', 'webpack.config.js')
const fixture = path.join(__dirname, 'fixtures', 'glsl', 'output.txt')

t.plan(1)

try {
fs.unlinkSync(output)
} catch (e) {}

spawn(wpack, [
'--module-bind', 'js=' + __dirname,
'--config',
config
], {
cwd: path.join(__dirname, 'fixtures', 'glsl'),
stdio: ['pipe', 'pipe', 2]
}).once('exit', function () {
const result = fs.readFileSync(output, { encoding: 'utf8' })

fs.unlinkSync(output)

vm.runInNewContext(result, {
console: {
log: function (shader) {
const expected = fs.readFileSync(fixture, { encoding: 'utf8' })
t.equal(shader + '\n', expected, 'processed brfs from package.json')
}
}
})
})
})