glslify-bundle
Advanced tools
Comparing version 2.0.4 to 3.0.0
72
index.js
var tokenize = require('glsl-tokenizer/string') | ||
var inject = require('glsl-inject-defines') | ||
var defines = require('glsl-token-defines') | ||
var descope = require('glsl-token-descope') | ||
var string = require('glsl-token-string') | ||
var scope = require('glsl-token-scope') | ||
var depth = require('glsl-token-depth') | ||
var inject = require('glsl-inject-defines') | ||
var defines = require('glsl-token-defines') | ||
var descope = require('glsl-token-descope') | ||
var string = require('glsl-token-string') | ||
var scope = require('glsl-token-scope') | ||
var depth = require('glsl-token-depth') | ||
var topoSort = require('./lib/topo-sort') | ||
module.exports = function(deps) { | ||
module.exports = function (deps) { | ||
return inject(Bundle(deps).src, { | ||
@@ -15,9 +16,12 @@ GLSLIFY: 1 | ||
function Bundle(deps) { | ||
function Bundle (deps) { | ||
if (!(this instanceof Bundle)) return new Bundle(deps) | ||
this.depList = deps | ||
this.depIndex = indexBy(deps, 'id') | ||
this.exported = {} | ||
this.cache = {} | ||
// Reorder dependencies topologically | ||
deps = topoSort(deps) | ||
this.depList = deps | ||
this.depIndex = indexBy(deps, 'id') | ||
this.exported = {} | ||
this.cache = {} | ||
this.varCounter = 0 | ||
@@ -29,4 +33,5 @@ | ||
var dep = deps[i] | ||
dep.bundle = this.bundle(dep) | ||
if (dep.entry) { | ||
this.src = this.src.concat(this.bundle(dep).tokens) | ||
this.src = this.src.concat(dep.bundle.tokens) | ||
} | ||
@@ -38,5 +43,5 @@ } | ||
Bundle.prototype.bundle = function(dep) { | ||
var tokens = tokenize(dep.source) | ||
var self = this | ||
Bundle.prototype.bundle = function (dep) { | ||
var tokens = tokenize(dep.source) | ||
var self = this | ||
var imports = [] | ||
@@ -59,4 +64,3 @@ var exports = null | ||
tokens.splice(i--, 1) | ||
} else | ||
if (imported) { | ||
} else if (imported) { | ||
var name = imported[1] | ||
@@ -85,9 +89,9 @@ var maps = imported[2].split(/\s?,\s?/g) | ||
var targetBundle = this.bundle(target) | ||
var targetBundle = target.bundle | ||
var targetTokens = targetBundle.tokens | ||
var targetExport = targetBundle.exports | ||
var targetIndex = tokens.indexOf(token) | ||
var targetDefs = defines(targetTokens) | ||
var targetIndex = tokens.indexOf(token) | ||
var targetDefs = defines(targetTokens) | ||
descope(targetTokens, function(local, token) { | ||
descope(targetTokens, function (local, token) { | ||
if ('module' in token) return local | ||
@@ -107,3 +111,5 @@ if (targetDefs[local]) return local | ||
if (targetExport === local) { | ||
return self.cache[target.id] = self.cache[target.id] || name | ||
return ( | ||
self.cache[target.id] = self.cache[target.id] || name | ||
) | ||
} | ||
@@ -128,7 +134,7 @@ | ||
tokens.forEach(function(token) { | ||
tokens.forEach(function (token) { | ||
if (token.type !== 'ident') return | ||
if ('module' in token) return | ||
imports.forEach(function(imported) { | ||
imports.forEach(function (imported) { | ||
if (imported.name !== token.data) return | ||
@@ -145,16 +151,16 @@ token.data = self.cache[imported.target.id] | ||
function glslifyPreprocessor(data) { | ||
function glslifyPreprocessor (data) { | ||
return /#pragma glslify:/.test(data) | ||
} | ||
function glslifyExport(data) { | ||
function glslifyExport (data) { | ||
return /#pragma glslify:\s*export\(([^\)]+)\)/.exec(data) | ||
} | ||
function glslifyImport(data) { | ||
function glslifyImport (data) { | ||
return /#pragma glslify:\s*([^=\s]+)\s*=\s*require\(([^\)]+)\)/.exec(data) | ||
} | ||
function indexBy(deps, key) { | ||
return deps.reduce(function(deps, entry) { | ||
function indexBy (deps, key) { | ||
return deps.reduce(function (deps, entry) { | ||
deps[entry[key]] = entry | ||
@@ -165,6 +171,6 @@ return deps | ||
function toMapping(maps) { | ||
function toMapping (maps) { | ||
if (!maps) return false | ||
return maps.reduce(function(mapping, defn) { | ||
return maps.reduce(function (mapping, defn) { | ||
defn = defn.split(/\s?=\s?/g) | ||
@@ -174,3 +180,3 @@ | ||
defn.forEach(function(key) { | ||
defn.forEach(function (key) { | ||
mapping[key] = expr | ||
@@ -177,0 +183,0 @@ }) |
{ | ||
"name": "glslify-bundle", | ||
"version": "2.0.4", | ||
"version": "3.0.0", | ||
"description": "Bundle a glslify-deps dependency tree into a GLSL source string", | ||
@@ -8,3 +8,3 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "node test | tap-spec" | ||
"test": "standard; node test | tap-spec" | ||
}, | ||
@@ -26,3 +26,6 @@ "author": { | ||
"devDependencies": { | ||
"gl": "^2.1.5", | ||
"gl-shader": "^4.0.6", | ||
"glslify-deps": "^1.2.1", | ||
"standard": "^5.4.1", | ||
"tape": "^3.5.0" | ||
@@ -29,0 +32,0 @@ }, |
11736
15
204
5