diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..1937215 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,19 @@ +# EditorConfig is awesome: https://editorconfig.org/ + +# top-most EditorConfig file +root = true + +[*.md] +trim_trailing_whitespace = false + +[*.js] +trim_trailing_whitespace = true + +# Unix-style newlines with a newline ending every file +[*] +indent_style = space +indent_size = 2 +end_of_line = lf +charset = utf-8 +insert_final_newline = true +max_line_length = 100 diff --git a/cacheWrap.js b/cacheWrap.js new file mode 100644 index 0000000..87e10f8 --- /dev/null +++ b/cacheWrap.js @@ -0,0 +1,29 @@ +var path = require('path') + +function cacheWrap(read, cache, async) { + // resolve all cached files such that they match + // all of the paths glslify handles, which are otherwise + // absolute + cache = Object.keys(cache).reduce(function(newCache, file) { + newCache[path.resolve(file)] = cache[file] + return newCache + }, {}) + + return function readFromCache(filename, done) { + if (!cache[filename]) { + if (async) { + return read(filename, done) + } + cache[filename] = read(filename) + } + + if (async) { + return process.nextTick(function() { + done(null, cache[filename]) + }) + } + return cache[filename] + } +} + +module.exports = cacheWrap; diff --git a/common.js b/common.js new file mode 100644 index 0000000..0ea8901 --- /dev/null +++ b/common.js @@ -0,0 +1,22 @@ +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 genInlineName() { + return '__INLINE__' + Math.random() +} + +module.exports = { + glslifyPreprocessor, + glslifyExport, + glslifyImport, + genInlineName, +}; diff --git a/index.js b/index.js index a0ecbb4..12544f4 100644 --- a/index.js +++ b/index.js @@ -5,11 +5,17 @@ var map = require('map-limit') var inherits = require('inherits') var Emitter = require('events/') var path = require('path') - var glslResolve = require('glsl-resolve') var nodeResolve = require('resolve') - -var inlineName = '__INLINE__' + Math.random() +var cacheWrap = require('./cacheWrap'); +var { + glslifyPreprocessor, + glslifyExport, + glslifyImport, + genInlineName, +} = require('./common.js') + +var inlineName = genInlineName() var inlineSource = '' module.exports = Depper @@ -39,7 +45,7 @@ function Depper(opts) { this._globalTransforms = [] - this._readFile = cacheWrap(opts.readFile || defaultRead, this._fileCache) + this._readFile = cacheWrap(opts.readFile || defaultRead, this._fileCache, true) this.resolve = opts.resolve || glslResolve if (typeof this._cwd !== 'string') { @@ -325,38 +331,6 @@ Depper.prototype.applyTransforms = function(filename, src, transforms, done) { } } -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 defaultRead(src, done) { fs.readFile(src, 'utf8', done) } - -function cacheWrap(read, cache) { - // resolve all cached files such that they match - // all of the paths glslify handles, which are otherwise - // absolute - cache = Object.keys(cache).reduce(function(newCache, file) { - newCache[path.resolve(file)] = cache[file] - return newCache - }, {}) - - return function readFromCache(filename, done) { - if (!cache[filename]) { - return read(filename, done) - } - - process.nextTick(function() { - done(null, cache[filename]) - }) - } -} diff --git a/sync.js b/sync.js index 54fe512..4bb36e7 100644 --- a/sync.js +++ b/sync.js @@ -5,11 +5,17 @@ var map = require('map-limit') var inherits = require('inherits') var Emitter = require('events/') var path = require('path') - +var cacheWrap = require('./cacheWrap'); var glslResolve = require('glsl-resolve').sync var nodeResolve = require('resolve').sync - -var inlineName = '__INLINE__' + Math.random() +var { + glslifyPreprocessor, + glslifyExport, + glslifyImport, + genInlineName +} = require('./common.js') + +var inlineName = genInlineName() var inlineSource = '' module.exports = Depper @@ -66,7 +72,7 @@ Depper.prototype.inline = function(source, basedir) { * return src.toUpperCase() * } * ``` - * + * * This is also different from the async transform API. * * Where `filename` is the absolute file path, `src` is the shader source @@ -293,35 +299,6 @@ Depper.prototype.applyTransforms = function(filename, src, transforms) { return src } -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 defaultRead(src) { return fs.readFileSync(src, 'utf8') } - -function cacheWrap(read, cache) { - // resolve all cached files such that they match - // all of the paths glslify handles, which are otherwise - // absolute - cache = Object.keys(cache).reduce(function(newCache, file) { - newCache[path.resolve(file)] = cache[file] - return newCache - }, {}) - - return function readFromCache(filename) { - if (!cache[filename]) { - cache[filename] = read(filename) - } - return cache[filename] - } -}