Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/refactor common utils between async and sync #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions cacheWrap.js
Original file line number Diff line number Diff line change
@@ -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;
22 changes: 22 additions & 0 deletions common.js
Original file line number Diff line number Diff line change
@@ -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,
};
46 changes: 10 additions & 36 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -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])
})
}
}
43 changes: 10 additions & 33 deletions sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]
}
}