-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
76fa1ed
commit 01e7c44
Showing
12 changed files
with
335 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
const Asset = require('../Asset'); | ||
const localRequire = require('../utils/localRequire'); | ||
const path = require('path'); | ||
const promisify = require('../utils/promisify'); | ||
const Resolver = require('../Resolver'); | ||
|
||
class GLSLAsset extends Asset { | ||
constructor(name, pkg, options) { | ||
super(name, pkg, options); | ||
this.type = 'js'; | ||
} | ||
|
||
async parse() { | ||
const glslifyDeps = await localRequire('glslify-deps', this.name); | ||
|
||
// Use the Parcel resolver rather than the default glslify one. | ||
// This adds support for parcel features like alises, and tilde paths. | ||
const resolver = new Resolver({ | ||
extensions: ['.glsl', '.vert', '.frag'], | ||
rootDir: this.options.rootDir | ||
}); | ||
|
||
// Parse and collect dependencies with glslify-deps | ||
let cwd = path.dirname(this.name); | ||
let depper = glslifyDeps({ | ||
cwd, | ||
resolve: async (target, opts, next) => { | ||
try { | ||
let res = await resolver.resolve( | ||
target, | ||
path.join(opts.basedir, 'index') | ||
); | ||
next(null, res.path); | ||
} catch (err) { | ||
next(err); | ||
} | ||
} | ||
}); | ||
|
||
return await promisify(depper.inline.bind(depper))(this.contents, cwd); | ||
} | ||
|
||
collectDependencies() { | ||
for (let dep of this.ast) { | ||
if (!dep.entry) { | ||
this.addDependency(dep.file, {includedInParent: true}); | ||
} | ||
} | ||
} | ||
|
||
async generate() { | ||
// Generate the bundled glsl file | ||
const glslifyBundle = await localRequire('glslify-bundle', this.name); | ||
let glsl = glslifyBundle(this.ast); | ||
|
||
return { | ||
js: `module.exports=${JSON.stringify(glsl)};` | ||
}; | ||
} | ||
} | ||
|
||
module.exports = GLSLAsset; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const assert = require('assert'); | ||
const fs = require('fs'); | ||
const {bundle, run, assertBundleTree} = require('./utils'); | ||
|
||
describe('glsl', function() { | ||
it('should support requiring GLSL files via glslify', async function() { | ||
let b = await bundle(__dirname + '/integration/glsl/index.js'); | ||
|
||
assertBundleTree(b, { | ||
name: 'index.js', | ||
assets: ['index.js', 'local.glsl', 'local.vert', 'local.frag'], | ||
childBundles: [ | ||
{ | ||
type: 'map' | ||
} | ||
] | ||
}); | ||
|
||
let shader = fs.readFileSync( | ||
__dirname + '/integration/glsl/compiled.glsl', | ||
'utf8' | ||
); | ||
|
||
let output = run(b); | ||
assert.equal(typeof output, 'function'); | ||
assert.ok( | ||
output().reduce((acc, requiredShader) => { | ||
return acc && shader === requiredShader; | ||
}, true) | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#version 300 es | ||
|
||
void someUniqFunction() { | ||
} | ||
|
||
precision mediump float; | ||
#define GLSLIFY 1 | ||
|
||
void main() { | ||
someUniqFunction(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const shaders = [ | ||
require('./local.glsl'), | ||
require('./local.vert'), | ||
require('./local.frag'), | ||
]; | ||
|
||
module.exports = function () { | ||
return shaders; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
void someUniqFunction() { | ||
} | ||
|
||
#pragma glslify: export(someUniqFunction) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#version 300 es | ||
|
||
#pragma glslify: test = require('./lib') | ||
|
||
precision mediump float; | ||
|
||
void main() { | ||
test(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#version 300 es | ||
|
||
#pragma glslify: test = require('./lib.glsl') | ||
|
||
precision mediump float; | ||
|
||
void main() { | ||
test(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#version 300 es | ||
|
||
#pragma glslify: test = require('~/lib') | ||
|
||
precision mediump float; | ||
|
||
void main() { | ||
test(); | ||
} |
Oops, something went wrong.