diff --git a/fixtures/glsl/index.js b/fixtures/glsl/index.js new file mode 100644 index 0000000..fc929d7 --- /dev/null +++ b/fixtures/glsl/index.js @@ -0,0 +1,9 @@ +const glsl = require('glslify') + +console.log(glsl` +precision mediump float; +#pragma glslify: ones = require(./ones) +void main () { + gl_FragColor = ones(); +} +`) diff --git a/fixtures/glsl/ones.glsl b/fixtures/glsl/ones.glsl new file mode 100644 index 0000000..4cfd2bc --- /dev/null +++ b/fixtures/glsl/ones.glsl @@ -0,0 +1,4 @@ +vec4 ones () { + return vec4(1); +} +#pragma glslify: export(ones) diff --git a/fixtures/glsl/output.txt b/fixtures/glsl/output.txt new file mode 100644 index 0000000..0dcae7a --- /dev/null +++ b/fixtures/glsl/output.txt @@ -0,0 +1,12 @@ + +precision mediump float; +#define GLSLIFY 1 +#define GLSLIFY 1 +vec4 ones () { + return vec4(1); +} + +void main () { + gl_FragColor = ones(); +} +, diff --git a/fixtures/glsl/package.json b/fixtures/glsl/package.json new file mode 100644 index 0000000..b9b2970 --- /dev/null +++ b/fixtures/glsl/package.json @@ -0,0 +1,12 @@ +{ + "name": "fixture", + "version": "1.0.0", + "scripts": { + "build": "../../node_modules/.bin/webpack webpack.config.js" + }, + "browserify": { + "transform": [ + "glslify" + ] + } +} diff --git a/fixtures/glsl/webpack.config.js b/fixtures/glsl/webpack.config.js new file mode 100644 index 0000000..475abc7 --- /dev/null +++ b/fixtures/glsl/webpack.config.js @@ -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, '..', '..') + }] + } +} diff --git a/index.js b/index.js index af0f490..e88f84a 100644 --- a/index.js +++ b/index.js @@ -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)) diff --git a/package.json b/package.json index 9f410f4..2f08538 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test.js b/test.js index 4c4ed3b..8afccfa 100644 --- a/test.js +++ b/test.js @@ -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') + } + } + }) + }) +}) +