Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

glslify-bundle

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

glslify-bundle - npm Package Compare versions

Comparing version 1.0.2 to 2.0.0

fixtures/another.glsl

212

index.js

@@ -1,89 +0,161 @@

var glslify = require('glslify-stream')
var deparser = require('glsl-deparser')
var concat = require('concat-stream')
var combine = require('multipipe')
var from = require('new-from')
var resolve = require('resolve')
var path = require('path')
var once = require('once')
var fs = require('fs')
var tokenize = require('glsl-tokenizer/string')
var descope = require('glsl-token-descope')
var string = require('glsl-token-string')
var scope = require('glsl-token-scope')
var depth = require('glsl-token-depth')
module.exports = bundle
module.exports = function(deps) {
return '#define GLSLIFY 1\n\n' + Bundle(deps).src
}
function bundle(cwd, options, done) {
var frag = options.frag || options.fragment
var vert = options.vert || options.vertex
var transform = options.transform || []
var inline = options.inline
var filename = cwd && path.resolve(cwd, '__entry.glsl')
function Bundle(deps) {
if (!(this instanceof Bundle)) return new Bundle(deps)
var opts = { transform: transform , input: true }
var result = {}
var files = result.files = []
this.depList = deps
this.depIndex = indexBy(deps, 'id')
this.exported = {}
this.cache = {}
this.varCounter = 0
done = once(done)
this.src = []
// Some messy normalisation going on here.
//
// * If a stream, use a dummy file name.
// * If an inline shader string, use a dummy name and convert to a stream
// * If a file path, use fs.createReadStream
// * If not supplied, ignore it
var vertname = filename
var fragname = filename
if (vert && typeof vert === 'string') {
if (!inline) {
vertname = vert
vert = resolve.sync(vert, { basedir: cwd })
vert = fs.createReadStream(vert, 'utf8')
} else {
vert = from([vert])
for (var i = 0; i < deps.length; i++) {
var dep = deps[i]
if (dep.entry) {
this.src = this.src.concat(this.bundle(dep).tokens)
}
}
if (frag && typeof frag === 'string') {
if (!inline) {
fragname = frag
frag = resolve.sync(frag, { basedir: cwd })
frag = fs.createReadStream(frag, 'utf8')
} else {
frag = from([frag])
}
}
this.src = string(this.src)
}
vertname = path.resolve(vertname)
fragname = path.resolve(fragname)
handle(vert, vert && glslify(vertname, opts), 'vert')
handle(frag, frag && glslify(fragname, opts), 'frag')
Bundle.prototype.bundle = function(dep) {
var tokens = tokenize(dep.source)
var self = this
var imports = []
var exports = null
function handle(inputStream, outputStream, key) {
if (!outputStream) {
return check(result[key] = ' ')
}
depth(tokens)
scope(tokens)
combine(
outputStream.on('file', pushFile)
, deparser()
, concat(function(data) {
check(result[key] = data)
for (var i = 0; i < tokens.length; i++) {
var token = tokens[i]
if (token.type !== 'preprocessor') continue
if (!glslifyPreprocessor(token.data)) continue
var exported = glslifyExport(token.data)
var imported = glslifyImport(token.data)
if (exported) {
exports = exported[1]
tokens.splice(i--, 1)
} else
if (imported) {
var name = imported[1]
var maps = imported[2].split(/\s?,\s?/g)
var path = maps.shift()
var target = this.depIndex[dep.deps[path]]
maps = toMapping(maps)
if (!target) throw new Error('Could not find module: "' + path + '"')
imports.push({
name: name,
target: target
})
).on('error', done)
if (inputStream) {
inputStream.pipe(outputStream)
if (self.cache[target.id]) {
tokens.splice(i--, 1)
continue
}
var targetBundle = this.bundle(target)
var targetTokens = targetBundle.tokens
var targetExport = targetBundle.exports
var targetIndex = tokens.indexOf(token)
descope(targetTokens, function(local, token) {
if ('module' in token) return local
if (maps && maps[local]) return maps[local]
// Give each variable in the required GLSL module
// a new name unique to the module. This prevents
// variable name conflicts between modules, in the
// same way you get with node/browserify.
var name = [local, target.id, self.varCounter++].join('_')
// Keep a cache of exported variable names, such
// that we can share them across files and refer
// to the same function/struct/value.
if (targetExport === local) {
return self.cache[target.id] = self.cache[target.id] || name
}
return name
})
for (var j = 0; j < targetTokens.length; j++) {
if ('module' in token) continue
targetTokens[j].module = target.id
}
tokens = []
.concat(tokens.slice(0, targetIndex))
.concat(targetTokens)
.concat(tokens.slice(targetIndex + 1))
i += targetTokens.length
}
}
function pushFile(file) {
if (file === filename) return
files.push(file)
}
tokens.forEach(function(token) {
if (token.type !== 'ident') return
if ('module' in token) return
function check() {
if (!result.vert) return
if (!result.frag) return
done(null, result)
imports.forEach(function(imported) {
if (imported.name !== token.data) return
token.data = self.cache[imported.target.id]
})
})
return {
tokens: tokens,
exports: exports
}
}
function glslifyPreprocessor(data) {
return /#pragma glslify:/.test(data)
}
function glslifyExport(data) {
return /#pragma glslify:\s*export\(([^\)]+)\)/.exec(data)
}
function glslifyImport(data) {
return /#pragma glslify:\s*([^=\s]+)\s*=\s*require\(([^\)]+)\)/.exec(data)
}
function indexBy(deps, key) {
return deps.reduce(function(deps, entry) {
deps[entry[key]] = entry
return deps
}, {})
}
function toMapping(maps) {
if (!maps) return false
return maps.reduce(function(mapping, defn) {
defn = defn.split(/\s?=\s?/g)
var expr = defn.pop()
defn.forEach(function(key) {
mapping[key] = expr
})
return mapping
}, {})
}

@@ -1,18 +0,12 @@

This software is released under the MIT license:
The MIT License (MIT)
=====================
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:
Copyright (c) 2014 [stackgl](http://github.com/stackgl/) contributors
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*stackgl contributors listed at <https://github.com/stackgl/contributing/blob/master/CONTRIBUTING.md#contributors>*
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.
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:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
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.
{
"name": "glslify-bundle",
"version": "1.0.2",
"description": "A wrapper module for bundling glslify scripts using common functionality within the glslify module ecosystem",
"version": "2.0.0",
"description": "Bundle a glslify-deps dependency tree into a GLSL source string",
"main": "index.js",
"license": "MIT",
"scripts": {
"test": "node test"
"test": "node test | tap-spec"
},

@@ -16,31 +16,31 @@ "author": {

"dependencies": {
"concat-stream": "^1.4.6",
"glsl-deparser": "0.0.3",
"glslify-stream": "^0.4.1",
"multipipe": "^0.1.1",
"new-from": "0.0.3",
"once": "^1.3.0",
"resolve": "^0.7.1"
"glsl-token-depth": "^1.1.1",
"glsl-token-descope": "^1.0.2",
"glsl-token-scope": "^1.1.1",
"glsl-token-string": "^1.0.1",
"glsl-tokenizer": "^2.0.0"
},
"devDependencies": {
"glslify-hex": "0.0.0",
"tape": "^2.13.3"
"glslify-deps": "^1.0.3",
"tape": "^3.5.0"
},
"repository": {
"type": "git",
"url": "git://github.com/hughsk/glslify-bundle.git"
"url": "git://github.com/stackgl/glslify-bundle.git"
},
"keywords": [
"ecosystem:stackgl",
"glslify",
"glsl",
"webgl",
"shader",
"bundle",
"dependency",
"tool"
"module",
"system",
"tool",
"webgl"
],
"homepage": "https://github.com/hughsk/glslify-bundle",
"homepage": "https://github.com/stackgl/glslify-bundle",
"bugs": {
"url": "https://github.com/hughsk/glslify-bundle/issues"
"url": "https://github.com/stackgl/glslify-bundle/issues"
}
}

@@ -1,11 +0,11 @@

# glslify-bundle [![experimental](http://badges.github.io/stability-badges/dist/experimental.svg)](http://github.com/badges/stability-badges)
# glslify-bundle
A wrapper module for bundling glslify scripts using common functionality within
the glslify module ecosystem.
[![experimental](http://badges.github.io/stability-badges/dist/experimental.svg)](http://github.com/badges/stability-badges)
Normalizes the API to closely match what's seen when using
[glslify as a browserify transform](https://github.com/chrisdickinson/glslify#as-a-browserify-transform),
such that when dealing with the simple case it can be used more quickly in other
modules.
Bundle a [glslify-deps](http://github.com/stackgl/glslify-deps) dependency tree into
a GLSL source string.
This has been separated from *glslify-deps* such that you can prebundle a dependency
tree server-side, but then still modify shader file contents in a browser.
## Usage

@@ -15,21 +15,29 @@

### bundle(cwd, options, done)
### `source = bundle(deps)`
* `cwd` is the working directory of any inline shaders.
* `options` is an object literal, and should support any of the parameters
you can use when using the browserify transform. Notably, `frag` and `vert`
are optional.
* `done(err, result)` is called when ready.
Takes the output object from [glslify-deps](http://github.com/stackgl/glslify-deps)
and returns a bundled GLSL string.
### result.frag
The bundled fragment shader.
``` javascript
var bundle = require('glslify-bundle')
var deps = require('glslify-deps')
var path = require('path')
### result.vert
The bundled vertex shader.
var file = path.join(__dirname, 'index.glsl')
### result.files
An array of files that were used within the bundling process.
deps().add(file, function(err, tree) {
if (err) throw err
var glsl = bundle(tree)
console.log(glsl)
})
```
## Contributing
See [stackgl/contributing](https://github.com/stackgl/contributing) for details.
## License
MIT. See [LICENSE.md](http://github.com/hughsk/glslify-bundle/blob/master/LICENSE.md) for details.
MIT. See [LICENSE.md](http://github.com/stackgl/glslify-bundle/blob/master/LICENSE.md) for details.
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc