glsl-inject-defines
Advanced tools
Comparing version 1.0.1 to 1.0.2
{ | ||
"name": "glsl-inject-defines", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "injects a #define statement into a shader source", | ||
@@ -16,2 +16,3 @@ "main": "string.js", | ||
"dependencies": { | ||
"glsl-token-inject-block": "^1.0.0", | ||
"glsl-token-string": "^1.0.1", | ||
@@ -18,0 +19,0 @@ "glsl-tokenizer": "^2.0.2" |
var tokenize = require('glsl-tokenizer') | ||
var stringify = require('glsl-token-string') | ||
var inject = require('glsl-token-inject-block') | ||
var newline = { | ||
type: 'whitespace', | ||
data: '\n' | ||
} | ||
module.exports = function (source, defines) { | ||
module.exports = function glslInjectDefine (source, defines) { | ||
if (!defines) { | ||
@@ -20,9 +16,4 @@ return source | ||
var tokens = tokenize(source) | ||
var idx = startIndex(tokens) | ||
var last = tokens[idx - 1] | ||
if (last) { | ||
insert(tokens, idx++, newline) | ||
} | ||
keys.forEach(function (key) { | ||
for (var i=keys.length-1; i>=0; i--) { | ||
var key = keys[i] | ||
var val = String(defines[key]) | ||
@@ -33,49 +24,9 @@ if (val) { // allow empty value | ||
insert(tokens, idx++, { | ||
inject(tokens, { | ||
type: 'preprocessor', | ||
data: '#define ' + key + val | ||
}) | ||
insert(tokens, idx++, newline) | ||
}) | ||
var src = stringify(tokens) | ||
return src | ||
} | ||
function startIndex (tokens) { | ||
// determine starting index for defines | ||
var start = 0 | ||
for (var i = 0; i < tokens.length; i++) { | ||
var token = tokens[i] | ||
if (token.type === 'preprocessor') { | ||
if (/^#(extension|version)/.test(token.data)) { | ||
start = Math.max(start, i + 1) | ||
} | ||
} | ||
} | ||
return start | ||
return stringify(tokens) | ||
} | ||
function insert (tokens, idx, token) { | ||
// inserts a token at the start of a line | ||
var last = tokens[idx - 1] | ||
var line = last ? last.line : 0 | ||
var position = last ? (last.position + last.data.length) : 0 | ||
token = { | ||
type: token.type, | ||
data: token.data, | ||
position: position, | ||
line: line, | ||
column: 0 | ||
} | ||
tokens.splice(idx, 0, token) | ||
var offset = token.data.length | ||
while (idx < tokens.length) { | ||
tokens[idx].position += offset | ||
tokens[idx].line++ | ||
idx++ | ||
} | ||
return token | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4586
3
25
+ Addedglsl-token-inject-block@1.1.0(transitive)