From c34a49b5dad66bcc79beb88849729df7a48f837a Mon Sep 17 00:00:00 2001 From: rubeniskov Date: Sat, 14 Nov 2020 11:38:20 +0100 Subject: [PATCH 01/21] add editorconfig to keep js file format --- .editorconfig | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 .editorconfig 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 From 359efb6f260f220194b2c6eddf5ff6813d5f4b1d Mon Sep 17 00:00:00 2001 From: rubeniskov Date: Sat, 14 Nov 2020 11:38:55 +0100 Subject: [PATCH 02/21] refactor common utils into separated files --- cacheWrap.js | 29 +++++++++++++++++++++++++++++ common.js | 22 ++++++++++++++++++++++ index.js | 46 ++++++++++------------------------------------ sync.js | 43 ++++++++++--------------------------------- 4 files changed, 71 insertions(+), 69 deletions(-) create mode 100644 cacheWrap.js create mode 100644 common.js 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] - } -} From ae2a5b1d02a5b51207dde41303530510917a0d65 Mon Sep 17 00:00:00 2001 From: rubeniskov Date: Sat, 14 Nov 2020 12:08:07 +0100 Subject: [PATCH 03/21] refactor: create mixed depper class for both asyn and sync --- depper.js | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ index.js | 55 +++++++++++++++---------------------------------------- sync.js | 55 ++++++++++++++----------------------------------------- 3 files changed, 81 insertions(+), 81 deletions(-) create mode 100644 depper.js diff --git a/depper.js b/depper.js new file mode 100644 index 0000000..ed66a2e --- /dev/null +++ b/depper.js @@ -0,0 +1,52 @@ +var fs = require('graceful-fs') +var Emitter = require('events/') +var inherits = require('inherits') +var cacheWrap = require('./cacheWrap') +var glslResolve = require('glsl-resolve') + +module.exports = Depper + +/** + * Creates a new instance of glslify-deps. Generally, you'll + * want to use one instance per bundle. + * + * @class + * @param {String} cwd The root directory of your shader. Defaults to process.cwd() + */ +inherits(Depper, Emitter) +function Depper(opts, async) { + if (!(this instanceof Depper)) return new Depper(opts) + Emitter.call(this) + + opts = typeof opts === 'string' ? { cwd: opts } : opts + opts = opts || {} + + this._async = opts.async || async + this._deps = [] + this._cwd = opts.cwd || process.cwd() + this._cache = {} + this._i = 0 + this._transforms = [] + this._trCache = {} + this._fileCache = opts.files || {} + + this._globalTransforms = [] + + this._readFile = cacheWrap(opts.readFile || createDefaultRead(this._async), this._fileCache, this._async) + this.resolve = opts.resolve || (this._async ? glslResolve : glslResolve.sync) + + if (typeof this._cwd !== 'string') { + throw new Error('glslify-deps: cwd must be a string path') + } +} + +function createDefaultRead(async) { + if (async) { + return function defaultRead(src, done) { + fs.readFile(src, 'utf8', done) + } + } + return function defaultRead(src) { + return fs.readFileSync(src, 'utf8') + } +} diff --git a/index.js b/index.js index 12544f4..23a53c3 100644 --- a/index.js +++ b/index.js @@ -3,11 +3,10 @@ var findup = require('@choojs/findup') var fs = require('graceful-fs') 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 cacheWrap = require('./cacheWrap'); +var Depper = require('./depper') + var { glslifyPreprocessor, glslifyExport, @@ -18,7 +17,7 @@ var { var inlineName = genInlineName() var inlineSource = '' -module.exports = Depper +module.exports = DepperAsync /** * Creates a new instance of glslify-deps. Generally, you'll @@ -27,33 +26,13 @@ module.exports = Depper * @class * @param {String} cwd The root directory of your shader. Defaults to process.cwd() */ -inherits(Depper, Emitter) -function Depper(opts) { - if (!(this instanceof Depper)) return new Depper(opts) - Emitter.call(this) - - opts = typeof opts === 'string' ? { cwd: opts } : opts - opts = opts || {} - - this._deps = [] - this._cwd = opts.cwd || process.cwd() - this._cache = {} - this._i = 0 - this._transforms = [] - this._trCache = {} - this._fileCache = opts.files || {} - - this._globalTransforms = [] - - this._readFile = cacheWrap(opts.readFile || defaultRead, this._fileCache, true) - this.resolve = opts.resolve || glslResolve - - if (typeof this._cwd !== 'string') { - throw new Error('glslify-deps: cwd must be a string path') - } +inherits(DepperAsync, Depper) +function DepperAsync(opts) { + if (!(this instanceof DepperAsync)) return new DepperAsync(opts) + Depper.call(this, opts, true); } -Depper.prototype.inline = function(source, basedir, done) { +DepperAsync.prototype.inline = function(source, basedir, done) { var inlineFile = path.resolve(basedir || process.cwd(), inlineName) inlineSource = source @@ -82,7 +61,7 @@ Depper.prototype.inline = function(source, basedir, done) { * @param {String|Function} transform * @param {Object} opts */ -Depper.prototype.transform = function(transform, opts) { +DepperAsync.prototype.transform = function(transform, opts) { var name = typeof transform === 'string' ? transform : null var list = opts && opts.global ? this._globalTransforms @@ -112,7 +91,7 @@ Depper.prototype.transform = function(transform, opts) { * resolved, and will include an array of dependencies discovered * so far as its second argument. */ -Depper.prototype.add = function(filename, done) { +DepperAsync.prototype.add = function(filename, done) { var basedir = path.dirname(filename = path.resolve(filename)) var cache = this._cache var self = this @@ -194,7 +173,7 @@ Depper.prototype.add = function(filename, done) { } } -Depper.prototype.readFile = function(filename, done) { +DepperAsync.prototype.readFile = function(filename, done) { if (path.basename(filename) !== inlineName) return this._readFile(filename, done) @@ -209,11 +188,11 @@ Depper.prototype.readFile = function(filename, done) { * - shader files in node_modules do not get local transforms * - all files will apply transforms specified in `glslify.transform` in your * `package.json` file, albeit after any transforms you specified using - * `depper.transform`. + * `depperAsync.transform`. * * @param {String} filename The absolute path of the file in question. */ -Depper.prototype.getTransformsForFile = function(filename, done) { +DepperAsync.prototype.getTransformsForFile = function(filename, done) { var self = this var entry = this._deps[0] @@ -291,7 +270,7 @@ Depper.prototype.getTransformsForFile = function(filename, done) { * * @param {String|Function} transform */ -Depper.prototype.resolveTransform = function(transform) { +DepperAsync.prototype.resolveTransform = function(transform) { if (typeof transform === 'string') { transform = nodeResolve.sync(transform, { basedir: this._cwd @@ -315,7 +294,7 @@ Depper.prototype.resolveTransform = function(transform) { * @param {Array} transforms The transforms you'd like to apply. * @param {Function} done(err, transformed) */ -Depper.prototype.applyTransforms = function(filename, src, transforms, done) { +DepperAsync.prototype.applyTransforms = function(filename, src, transforms, done) { var i = 0 next(null, src) @@ -330,7 +309,3 @@ Depper.prototype.applyTransforms = function(filename, src, transforms, done) { tr.tr(filename, updated+'', tr.opts, next) } } - -function defaultRead(src, done) { - fs.readFile(src, 'utf8', done) -} diff --git a/sync.js b/sync.js index 4bb36e7..ba22ef8 100644 --- a/sync.js +++ b/sync.js @@ -1,13 +1,10 @@ var tokenize = require('glsl-tokenizer/string') var findup = require('@choojs/findup').sync var fs = require('graceful-fs') -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 Depper = require('./depper') var { glslifyPreprocessor, glslifyExport, @@ -18,7 +15,7 @@ var { var inlineName = genInlineName() var inlineSource = '' -module.exports = Depper +module.exports = DepperSync /** * Creates a new instance of glslify-deps. Generally, you'll @@ -27,33 +24,13 @@ module.exports = Depper * @class * @param {String} cwd The root directory of your shader. Defaults to process.cwd() */ -inherits(Depper, Emitter) -function Depper(opts) { - if (!(this instanceof Depper)) return new Depper(opts) - Emitter.call(this) - - opts = typeof opts === 'string' ? { cwd: opts } : opts - opts = opts || {} - - this._deps = [] - this._cwd = opts.cwd || process.cwd() - this._cache = {} - this._i = 0 - this._transforms = [] - this._trCache = {} - this._fileCache = opts.files || {} - - this._globalTransforms = [] - - this._readFile = cacheWrap(opts.readFileSync || defaultRead, this._fileCache) - this.resolve = opts.resolve || glslResolve - - if (typeof this._cwd !== 'string') { - throw new Error('glslify-deps: cwd must be a string path') - } +inherits(DepperSync, Depper) +function DepperSync(opts) { + if (!(this instanceof DepperSync)) return new DepperSync(opts) + Depper.call(this, opts) } -Depper.prototype.inline = function(source, basedir) { +DepperSync.prototype.inline = function(source, basedir) { var inlineFile = path.resolve(basedir || process.cwd(), inlineName) inlineSource = source @@ -81,7 +58,7 @@ Depper.prototype.inline = function(source, basedir) { * @param {String|Function} transform * @param {Object} opts */ -Depper.prototype.transform = function(transform, opts) { +DepperSync.prototype.transform = function(transform, opts) { var name = typeof transform === 'string' ? transform : null var list = opts && opts.global ? this._globalTransforms @@ -108,7 +85,7 @@ Depper.prototype.transform = function(transform, opts) { * * Returns an array of dependencies discovered so far as its second argument. */ -Depper.prototype.add = function(filename) { +DepperSync.prototype.add = function(filename) { var basedir = path.dirname(filename = path.resolve(filename)) var cache = this._cache var self = this @@ -170,7 +147,7 @@ Depper.prototype.add = function(filename) { } } -Depper.prototype.readFile = function(filename) { +DepperSync.prototype.readFile = function(filename) { if (path.basename(filename) !== inlineName) return this._readFile(filename) @@ -185,11 +162,11 @@ Depper.prototype.readFile = function(filename) { * - shader files in node_modules do not get local transforms * - all files will apply transforms specified in `glslify.transform` in your * `package.json` file, albeit after any transforms you specified using - * `depper.transform`. + * `depperSync.transform`. * * @param {String} filename The absolute path of the file in question. */ -Depper.prototype.getTransformsForFile = function(filename) { +DepperSync.prototype.getTransformsForFile = function(filename) { var self = this var entry = this._deps[0] @@ -261,7 +238,7 @@ Depper.prototype.getTransformsForFile = function(filename) { * * @param {String|Function} transform */ -Depper.prototype.resolveTransform = function(transform) { +DepperSync.prototype.resolveTransform = function(transform) { if (typeof transform === 'string') { transform = nodeResolve(transform, { basedir: this._cwd @@ -290,7 +267,7 @@ Depper.prototype.resolveTransform = function(transform) { * * Returns the transformed source string. */ -Depper.prototype.applyTransforms = function(filename, src, transforms) { +DepperSync.prototype.applyTransforms = function(filename, src, transforms) { transforms.forEach(function (tr) { var opts = tr.opts if (!opts || typeof opts !== 'object') opts = {} @@ -298,7 +275,3 @@ Depper.prototype.applyTransforms = function(filename, src, transforms) { }) return src } - -function defaultRead(src) { - return fs.readFileSync(src, 'utf8') -} From ed9cf929d479d969c488e45ed32b9c4f51b26254 Mon Sep 17 00:00:00 2001 From: rubeniskov Date: Sat, 14 Nov 2020 12:15:58 +0100 Subject: [PATCH 04/21] fix inline cwd use internal instead of process.cwd --- index.js | 2 +- sync.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 23a53c3..106f9c1 100644 --- a/index.js +++ b/index.js @@ -33,7 +33,7 @@ function DepperAsync(opts) { } DepperAsync.prototype.inline = function(source, basedir, done) { - var inlineFile = path.resolve(basedir || process.cwd(), inlineName) + var inlineFile = path.resolve(basedir || this._cwd, inlineName) inlineSource = source diff --git a/sync.js b/sync.js index ba22ef8..4ee53f6 100644 --- a/sync.js +++ b/sync.js @@ -31,7 +31,7 @@ function DepperSync(opts) { } DepperSync.prototype.inline = function(source, basedir) { - var inlineFile = path.resolve(basedir || process.cwd(), inlineName) + var inlineFile = path.resolve(basedir || this._cwd, inlineName) inlineSource = source From 47c49f46257face79ee5a6a86e946161c46b6b94 Mon Sep 17 00:00:00 2001 From: rubeniskov Date: Sat, 14 Nov 2020 12:28:22 +0100 Subject: [PATCH 05/21] refactor migrate inlineSource and inlineName to mixed depper class --- depper.js | 9 +++++++++ index.js | 12 ++++-------- sync.js | 12 ++++-------- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/depper.js b/depper.js index ed66a2e..495f6b1 100644 --- a/depper.js +++ b/depper.js @@ -1,9 +1,15 @@ +var path = require('path') var fs = require('graceful-fs') var Emitter = require('events/') var inherits = require('inherits') var cacheWrap = require('./cacheWrap') var glslResolve = require('glsl-resolve') +var { + genInlineName, +} = require('./common.js') + + module.exports = Depper /** @@ -35,6 +41,9 @@ function Depper(opts, async) { this._readFile = cacheWrap(opts.readFile || createDefaultRead(this._async), this._fileCache, this._async) this.resolve = opts.resolve || (this._async ? glslResolve : glslResolve.sync) + this._inlineSource = '' + this._inlineName = genInlineName() + if (typeof this._cwd !== 'string') { throw new Error('glslify-deps: cwd must be a string path') } diff --git a/index.js b/index.js index 106f9c1..5a33e71 100644 --- a/index.js +++ b/index.js @@ -11,12 +11,8 @@ var { glslifyPreprocessor, glslifyExport, glslifyImport, - genInlineName, } = require('./common.js') -var inlineName = genInlineName() -var inlineSource = '' - module.exports = DepperAsync /** @@ -33,9 +29,9 @@ function DepperAsync(opts) { } DepperAsync.prototype.inline = function(source, basedir, done) { - var inlineFile = path.resolve(basedir || this._cwd, inlineName) + var inlineFile = path.resolve(basedir || this._cwd, this._inlineName) - inlineSource = source + this._inlineSource = source this.add(inlineFile, function(err, tree) { done && done(err, !err && tree) @@ -174,10 +170,10 @@ DepperAsync.prototype.add = function(filename, done) { } DepperAsync.prototype.readFile = function(filename, done) { - if (path.basename(filename) !== inlineName) + if (path.basename(filename) !== this._inlineName) return this._readFile(filename, done) - return done(null, inlineSource) + return done(null, this._inlineSource) } /** diff --git a/sync.js b/sync.js index 4ee53f6..a3474a0 100644 --- a/sync.js +++ b/sync.js @@ -9,12 +9,8 @@ var { glslifyPreprocessor, glslifyExport, glslifyImport, - genInlineName } = require('./common.js') -var inlineName = genInlineName() -var inlineSource = '' - module.exports = DepperSync /** @@ -31,9 +27,9 @@ function DepperSync(opts) { } DepperSync.prototype.inline = function(source, basedir) { - var inlineFile = path.resolve(basedir || this._cwd, inlineName) + var inlineFile = path.resolve(basedir || this._cwd, this._inlineName) - inlineSource = source + this._inlineSource = source return this.add(inlineFile) } @@ -148,10 +144,10 @@ DepperSync.prototype.add = function(filename) { } DepperSync.prototype.readFile = function(filename) { - if (path.basename(filename) !== inlineName) + if (path.basename(filename) !== this._inlineName) return this._readFile(filename) - return inlineSource + return this._inlineSource } /** From 8b5f7a9a669daed7a38f245f1fdd46369044b03d Mon Sep 17 00:00:00 2001 From: rubeniskov Date: Sat, 14 Nov 2020 12:29:37 +0100 Subject: [PATCH 06/21] refactor migrate inline logic to mixed depper class --- depper.js | 14 ++++++++++++++ index.js | 10 ---------- sync.js | 8 -------- 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/depper.js b/depper.js index 495f6b1..3437b8c 100644 --- a/depper.js +++ b/depper.js @@ -49,6 +49,20 @@ function Depper(opts, async) { } } +Depper.prototype.inline = function(source, basedir, done) { + var inlineFile = path.resolve(basedir || this._cwd, this._inlineName) + + this._inlineSource = source + + if (this._async) { + this.add(inlineFile, function(err, tree) { + done && done(err, !err && tree) + }) + } else { + return this.add(inlineFile) + } +} + function createDefaultRead(async) { if (async) { return function defaultRead(src, done) { diff --git a/index.js b/index.js index 5a33e71..e5c56de 100644 --- a/index.js +++ b/index.js @@ -28,16 +28,6 @@ function DepperAsync(opts) { Depper.call(this, opts, true); } -DepperAsync.prototype.inline = function(source, basedir, done) { - var inlineFile = path.resolve(basedir || this._cwd, this._inlineName) - - this._inlineSource = source - - this.add(inlineFile, function(err, tree) { - done && done(err, !err && tree) - }) -} - /** * Adds a transform to use on your local dependencies. * Note that this should be used before calling `add`. diff --git a/sync.js b/sync.js index a3474a0..072a28c 100644 --- a/sync.js +++ b/sync.js @@ -26,14 +26,6 @@ function DepperSync(opts) { Depper.call(this, opts) } -DepperSync.prototype.inline = function(source, basedir) { - var inlineFile = path.resolve(basedir || this._cwd, this._inlineName) - - this._inlineSource = source - - return this.add(inlineFile) -} - /** * Adds a transform to use on your local dependencies. * Note that this should be used before calling `add`. From eff149a0c26852b5f0db4230ffb7faa71d593837 Mon Sep 17 00:00:00 2001 From: rubeniskov Date: Sat, 14 Nov 2020 12:40:24 +0100 Subject: [PATCH 07/21] refactor migrate transform logic to mixed depper class --- depper.js | 35 +++++++++++++++++++++++++++++++++++ index.js | 35 ----------------------------------- sync.js | 36 ------------------------------------ 3 files changed, 35 insertions(+), 71 deletions(-) diff --git a/depper.js b/depper.js index 3437b8c..d3e95be 100644 --- a/depper.js +++ b/depper.js @@ -63,6 +63,41 @@ Depper.prototype.inline = function(source, basedir, done) { } } +/** + * Adds a transform to use on your local dependencies. + * Note that this should be used before calling `add`. + * + * Transforms are handled using a different API to browserify, e.g.: + * + * ``` js + * module.exports = function transform(filename, src, opts, done) { + * done(null, src.toUpperCase()) + * } + * ``` + * + * Where `filename` is the absolute file path, `src` is the shader source + * as a string, `opts` is an options object for configuration, and `done` + * is a callback which takes the transformed shader source. + * + * @param {String|Function} transform + * @param {Object} opts + */ +Depper.prototype.transform = function(transform, opts) { + var name = typeof transform === 'string' ? transform : null + var list = opts && opts.global + ? this._globalTransforms + : this._transforms + + // post transforms are ignored by glslify-deps, to be handled + // by glslify after the file has been bundled. + if (opts && opts.post) return this + + transform = this.resolveTransform(transform) + list.push({ tr: transform, opts: opts, name: name }) + + return this +} + function createDefaultRead(async) { if (async) { return function defaultRead(src, done) { diff --git a/index.js b/index.js index e5c56de..0c57051 100644 --- a/index.js +++ b/index.js @@ -28,41 +28,6 @@ function DepperAsync(opts) { Depper.call(this, opts, true); } -/** - * Adds a transform to use on your local dependencies. - * Note that this should be used before calling `add`. - * - * Transforms are handled using a different API to browserify, e.g.: - * - * ``` js - * module.exports = function transform(filename, src, opts, done) { - * done(null, src.toUpperCase()) - * } - * ``` - * - * Where `filename` is the absolute file path, `src` is the shader source - * as a string, `opts` is an options object for configuration, and `done` - * is a callback which takes the transformed shader source. - * - * @param {String|Function} transform - * @param {Object} opts - */ -DepperAsync.prototype.transform = function(transform, opts) { - var name = typeof transform === 'string' ? transform : null - var list = opts && opts.global - ? this._globalTransforms - : this._transforms - - // post transforms are ignored by glslify-deps, to be handled - // by glslify after the file has been bundled. - if (opts && opts.post) return this - - transform = this.resolveTransform(transform) - list.push({ tr: transform, opts: opts, name: name }) - - return this -} - /** * Adds a shader file to the graph, including its dependencies * which are resolved in this step. Transforms are also applied diff --git a/sync.js b/sync.js index 072a28c..bb8aac7 100644 --- a/sync.js +++ b/sync.js @@ -26,42 +26,6 @@ function DepperSync(opts) { Depper.call(this, opts) } -/** - * Adds a transform to use on your local dependencies. - * Note that this should be used before calling `add`. - * - * Transforms are handled using a different API to browserify, e.g.: - * - * ``` js - * exports.sync = function transform(filename, src, opts) { - * return src.toUpperCase() - * } - * ``` - * - * This is also different from the async transform API. - * - * Where `filename` is the absolute file path, `src` is the shader source - * as a string, `opts` is an options object for configuration. - * - * @param {String|Function} transform - * @param {Object} opts - */ -DepperSync.prototype.transform = function(transform, opts) { - var name = typeof transform === 'string' ? transform : null - var list = opts && opts.global - ? this._globalTransforms - : this._transforms - - // post transforms are ignored by glslify-deps, to be handled - // by glslify after the file has been bundled. - if (opts && opts.post) return this - - transform = this.resolveTransform(transform) - list.push({ tr: transform, opts: opts, name: name }) - - return this -} - /** * Adds a shader file to the graph, including its dependencies * which are resolved in this step. Transforms are also applied From e082a4d87b220b26bb1837712a7afb1454b1742e Mon Sep 17 00:00:00 2001 From: rubeniskov Date: Sat, 14 Nov 2020 12:44:28 +0100 Subject: [PATCH 08/21] refactor migrate resolveTransform logic to mixed depper class --- depper.js | 29 +++++++++++++++++++++++++++++ index.js | 23 ----------------------- sync.js | 26 -------------------------- 3 files changed, 29 insertions(+), 49 deletions(-) diff --git a/depper.js b/depper.js index d3e95be..3f4ec12 100644 --- a/depper.js +++ b/depper.js @@ -3,6 +3,7 @@ var fs = require('graceful-fs') var Emitter = require('events/') var inherits = require('inherits') var cacheWrap = require('./cacheWrap') +var nodeResolve = require('resolve') var glslResolve = require('glsl-resolve') var { @@ -98,6 +99,34 @@ Depper.prototype.transform = function(transform, opts) { return this } +/** + * Resolves a transform. + * + * Functions are retained as-is. + * Strings are resolved using node's `require` resolution algorithm, + * and then required directly. + * + * @param {String|Function} transform + */ +Depper.prototype.resolveTransform = function(transform) { + if (typeof transform === 'string') { + transform = nodeResolve.sync(transform, { + basedir: this._cwd + }) + if (this._async) { + transform = require(transform) + } else { + var m = require(transform) + if (!m || typeof m.sync !== 'function') { + throw new Error('transform ' + transform + ' does not provide a' + + ' synchronous interface') + } + transform = m.sync + } + } + return transform +} + function createDefaultRead(async) { if (async) { return function defaultRead(src, done) { diff --git a/index.js b/index.js index 0c57051..1bf6326 100644 --- a/index.js +++ b/index.js @@ -4,9 +4,7 @@ var fs = require('graceful-fs') var map = require('map-limit') var inherits = require('inherits') var path = require('path') -var nodeResolve = require('resolve') var Depper = require('./depper') - var { glslifyPreprocessor, glslifyExport, @@ -212,27 +210,6 @@ DepperAsync.prototype.getTransformsForFile = function(filename, done) { } } -/** - * Resolves a transform. - * - * Functions are retained as-is. - * Strings are resolved using node's `require` resolution algorithm, - * and then required directly. - * - * @param {String|Function} transform - */ -DepperAsync.prototype.resolveTransform = function(transform) { - if (typeof transform === 'string') { - transform = nodeResolve.sync(transform, { - basedir: this._cwd - }) - - transform = require(transform) - } - - return transform -} - /** * Applies a transform to a string. * diff --git a/sync.js b/sync.js index bb8aac7..572d2f9 100644 --- a/sync.js +++ b/sync.js @@ -3,7 +3,6 @@ var findup = require('@choojs/findup').sync var fs = require('graceful-fs') var inherits = require('inherits') var path = require('path') -var nodeResolve = require('resolve').sync var Depper = require('./depper') var { glslifyPreprocessor, @@ -181,31 +180,6 @@ DepperSync.prototype.getTransformsForFile = function(filename) { } } -/** - * Resolves a transform. - * - * Functions are retained as-is. - * Strings are resolved using node's `require` resolution algorithm, - * and then required directly. - * - * @param {String|Function} transform - */ -DepperSync.prototype.resolveTransform = function(transform) { - if (typeof transform === 'string') { - transform = nodeResolve(transform, { - basedir: this._cwd - }) - - var m = require(transform) - if (!m || typeof m.sync !== 'function') { - throw new Error('transform ' + transform + ' does not provide a' - + ' synchronous interface') - } - transform = m.sync - } - return transform -} - /** * Applies a transform to a string. * From 1bd31d6f0db47352b5c44b5c02463c5c0e938f4e Mon Sep 17 00:00:00 2001 From: rubeniskov Date: Sat, 14 Nov 2020 16:42:47 +0100 Subject: [PATCH 09/21] refactor migrate applyTransforms logic to mixed depper class --- depper.js | 37 +++++++++++++++++++++++++++++++++++++ index.js | 28 ---------------------------- sync.js | 22 ---------------------- 3 files changed, 37 insertions(+), 50 deletions(-) diff --git a/depper.js b/depper.js index 3f4ec12..06055a6 100644 --- a/depper.js +++ b/depper.js @@ -127,6 +127,43 @@ Depper.prototype.resolveTransform = function(transform) { return transform } +/** + * Applies a transform to a string. + * + * Note that transforms here are passed in differently to other methods: + * - `tr.tr` should point to the transform function. + * - `tr.opts` should contain the options for the transform, if applicable. + * + * @param {String} filename The absolute path of the file you're transforming. + * @param {String} src The shader source you'd like to transform. + * @param {Array} transforms The transforms you'd like to apply. + * @param {Function} [done] applies when async true + */ +Depper.prototype.applyTransforms = function(filename, src, transforms, done) { + if (this._async) { + var i = 0 + + next(null, src) + function next(err, updated) { + if (err) return done(err) + if (i >= transforms.length) return done(null, updated) + + var tr = transforms[i++] + var opts = tr.opts + + if (!opts || typeof opts !== 'object') opts = {} + tr.tr(filename, updated+'', tr.opts, next) + } + } else { + transforms.forEach(function (tr) { + var opts = tr.opts + if (!opts || typeof opts !== 'object') opts = {} + src = tr.tr(filename, src+'', tr.opts) + }) + return src + } +} + function createDefaultRead(async) { if (async) { return function defaultRead(src, done) { diff --git a/index.js b/index.js index 1bf6326..2337a89 100644 --- a/index.js +++ b/index.js @@ -209,31 +209,3 @@ DepperAsync.prototype.getTransformsForFile = function(filename, done) { .concat(self._globalTransforms)) } } - -/** - * Applies a transform to a string. - * - * Note that transforms here are passed in differently to other methods: - * - `tr.tr` should point to the transform function. - * - `tr.opts` should contain the options for the transform, if applicable. - * - * @param {String} filename The absolute path of the file you're transforming. - * @param {String} src The shader source you'd like to transform. - * @param {Array} transforms The transforms you'd like to apply. - * @param {Function} done(err, transformed) - */ -DepperAsync.prototype.applyTransforms = function(filename, src, transforms, done) { - var i = 0 - - next(null, src) - function next(err, updated) { - if (err) return done(err) - if (i >= transforms.length) return done(null, updated) - - var tr = transforms[i++] - var opts = tr.opts - - if (!opts || typeof opts !== 'object') opts = {} - tr.tr(filename, updated+'', tr.opts, next) - } -} diff --git a/sync.js b/sync.js index 572d2f9..c366f15 100644 --- a/sync.js +++ b/sync.js @@ -179,25 +179,3 @@ DepperSync.prototype.getTransformsForFile = function(filename) { .concat(self._globalTransforms) } } - -/** - * Applies a transform to a string. - * - * Note that transforms here are passed in differently to other methods: - * - `tr.tr` should point to the transform function. - * - `tr.opts` should contain the options for the transform, if applicable. - * - * @param {String} filename The absolute path of the file you're transforming. - * @param {String} src The shader source you'd like to transform. - * @param {Array} transforms The transforms you'd like to apply. - * - * Returns the transformed source string. - */ -DepperSync.prototype.applyTransforms = function(filename, src, transforms) { - transforms.forEach(function (tr) { - var opts = tr.opts - if (!opts || typeof opts !== 'object') opts = {} - src = tr.tr(filename, src+'', tr.opts) - }) - return src -} From 877676c3017f3aa4f020eb5cf809e9a21875486c Mon Sep 17 00:00:00 2001 From: rubeniskov Date: Sat, 14 Nov 2020 16:55:16 +0100 Subject: [PATCH 10/21] refactor migrate readFile logic to mixed depper class --- depper.js | 10 ++++++++++ index.js | 7 ------- sync.js | 7 ------- 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/depper.js b/depper.js index 06055a6..fb14ab6 100644 --- a/depper.js +++ b/depper.js @@ -164,6 +164,16 @@ Depper.prototype.applyTransforms = function(filename, src, transforms, done) { } } +Depper.prototype.readFile = function(filename, done) { + if (path.basename(filename) !== this._inlineName) + return this._readFile(filename, done) + + if(this._async) { + return done(null, this._inlineSource) + } + return this._inlineSource +} + function createDefaultRead(async) { if (async) { return function defaultRead(src, done) { diff --git a/index.js b/index.js index 2337a89..4d507da 100644 --- a/index.js +++ b/index.js @@ -122,13 +122,6 @@ DepperAsync.prototype.add = function(filename, done) { } } -DepperAsync.prototype.readFile = function(filename, done) { - if (path.basename(filename) !== this._inlineName) - return this._readFile(filename, done) - - return done(null, this._inlineSource) -} - /** * Determines which transforms to use for a particular file. * The rules here are the same you see in browserify: diff --git a/sync.js b/sync.js index c366f15..d966c4d 100644 --- a/sync.js +++ b/sync.js @@ -98,13 +98,6 @@ DepperSync.prototype.add = function(filename) { } } -DepperSync.prototype.readFile = function(filename) { - if (path.basename(filename) !== this._inlineName) - return this._readFile(filename) - - return this._inlineSource -} - /** * Determines which transforms to use for a particular file. * The rules here are the same you see in browserify: From 707f3ad40d45a5bd3a75c490da99e326d673801c Mon Sep 17 00:00:00 2001 From: rubeniskov Date: Sat, 14 Nov 2020 18:11:47 +0100 Subject: [PATCH 11/21] refactor migrate getTransformsForFile logic to mixed depper class --- depper.js | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- index.js | 81 ------------------------------------------------- sync.js | 75 --------------------------------------------- utils.js | 37 ++++++++++++++++++++++ 4 files changed, 127 insertions(+), 157 deletions(-) create mode 100644 utils.js diff --git a/depper.js b/depper.js index fb14ab6..cb52b92 100644 --- a/depper.js +++ b/depper.js @@ -5,11 +5,16 @@ var inherits = require('inherits') var cacheWrap = require('./cacheWrap') var nodeResolve = require('resolve') var glslResolve = require('glsl-resolve') +var findup = require('@choojs/findup') var { genInlineName, } = require('./common.js') +var { + getTransformsFromPkg, +} = require('./utils.js') + module.exports = Depper @@ -137,7 +142,7 @@ Depper.prototype.resolveTransform = function(transform) { * @param {String} filename The absolute path of the file you're transforming. * @param {String} src The shader source you'd like to transform. * @param {Array} transforms The transforms you'd like to apply. - * @param {Function} [done] applies when async true + * @param {(err: Error, result: string) => any} [done] Applies when async true */ Depper.prototype.applyTransforms = function(filename, src, transforms, done) { if (this._async) { @@ -164,6 +169,90 @@ Depper.prototype.applyTransforms = function(filename, src, transforms, done) { } } +/** + * Determines which transforms to use for a particular file. + * The rules here are the same you see in browserify: + * + * - your shader files will have your specified transforms applied to them + * - shader files in node_modules do not get local transforms + * - all files will apply transforms specified in `glslify.transform` in your + * `package.json` file, albeit after any transforms you specified using + * `depper.transform`. + * + * @param {String} filename The absolute path of the file in question. + * @param {(err: Error, transforms: any) => any} [done] Applies when async true + */ +Depper.prototype.getTransformsForFile = function(filename, done) { + var self = this + var entry = this._deps[0] + + if (!entry) return done(new Error( + 'getTransformsForFile may only be called after adding your entry file' + )) + + var entryDir = path.dirname(path.resolve(entry.file)) + var fileDir = path.dirname(path.resolve(filename)) + var relative = path.relative(entryDir, fileDir).split(path.sep) + var node_modules = relative.indexOf('node_modules') !== -1 + var trLocal = node_modules ? [] : this._transforms + var trCache = this._trCache + var pkgName = 'package.json' + + if (trCache[fileDir]) { + if (this._async) { + return done(null, trCache[fileDir]) + } else { + return trCache[fileDir] + } + } + + function register(transforms) { + trCache[fileDir] = trLocal + .concat(transforms.map(function(tr) { + tr.tr = self.resolveTransform(tr.tr) + return tr + })) + .concat(self._globalTransforms); + var result = trCache[fileDir] + if (self._async) { + done(null, result) + } else { + return result + } + } + + if (this._async) { + findup(fileDir, pkgName, function(err, found) { + var notFound = err && err.message === 'not found' + if (notFound) return register([]) + if (err) return done(err) + + var pkg = path.join(found, pkgName) + + self.readFile(pkg, function(err, pkgJson) { + if (err) return done(err) + var transforms; + try { + transforms = getTransformsFromPkg(pkgJson) + } catch(e) { return done(e) } + + register(transforms) + }) + }) + } else { + try { var found = findup.sync(fileDir, pkgName) } + catch (err) { + var notFound = err.message === 'not found' + if (notFound) return register([]) + else throw err + } + + var pkg = path.join(found, pkgName) + + return register(getTransformsFromPkg(self.readFile(pkg))) + } +} + Depper.prototype.readFile = function(filename, done) { if (path.basename(filename) !== this._inlineName) return this._readFile(filename, done) diff --git a/index.js b/index.js index 4d507da..8d0c777 100644 --- a/index.js +++ b/index.js @@ -121,84 +121,3 @@ DepperAsync.prototype.add = function(filename, done) { }, resolved) } } - -/** - * Determines which transforms to use for a particular file. - * The rules here are the same you see in browserify: - * - * - your shader files will have your specified transforms applied to them - * - shader files in node_modules do not get local transforms - * - all files will apply transforms specified in `glslify.transform` in your - * `package.json` file, albeit after any transforms you specified using - * `depperAsync.transform`. - * - * @param {String} filename The absolute path of the file in question. - */ -DepperAsync.prototype.getTransformsForFile = function(filename, done) { - var self = this - var entry = this._deps[0] - - if (!entry) return done(new Error( - 'getTransformsForFile may only be called after adding your entry file' - )) - - var entryDir = path.dirname(path.resolve(entry.file)) - var fileDir = path.dirname(path.resolve(filename)) - var relative = path.relative(entryDir, fileDir).split(path.sep) - var node_modules = relative.indexOf('node_modules') !== -1 - var trLocal = node_modules ? [] : this._transforms - var trCache = this._trCache - - if (trCache[fileDir]) { - return done(null, trCache[fileDir]) - } - - findup(fileDir, 'package.json', function(err, found) { - var notFound = err && err.message === 'not found' - if (notFound) return register([]) - if (err) return done(err) - - var pkg = path.join(found, 'package.json') - - self.readFile(pkg, function(err, pkgjson) { - if (err) return done(err) - - try { - pkgjson = JSON.parse(pkgjson) - } catch(e) { return done(e) } - - var transforms = ( - pkgjson['glslify'] - && pkgjson['glslify']['transform'] - || [] - ) - - transforms = transforms.map(function(key) { - var transform = Array.isArray(key) - ? key - : [key, {}] - - var key = transform[0] - var opt = transform[1] - - if (opt) { - delete opt.global - delete opt.post - } - - return { tr: key, opts: opt, name: key } - }).map(function(tr) { - tr.tr = self.resolveTransform(tr.tr) - return tr - }) - - register(transforms) - }) - }) - - function register(transforms) { - done(null, trCache[fileDir] = trLocal - .concat(transforms) - .concat(self._globalTransforms)) - } -} diff --git a/sync.js b/sync.js index d966c4d..981031e 100644 --- a/sync.js +++ b/sync.js @@ -97,78 +97,3 @@ DepperSync.prototype.add = function(filename) { }) } } - -/** - * Determines which transforms to use for a particular file. - * The rules here are the same you see in browserify: - * - * - your shader files will have your specified transforms applied to them - * - shader files in node_modules do not get local transforms - * - all files will apply transforms specified in `glslify.transform` in your - * `package.json` file, albeit after any transforms you specified using - * `depperSync.transform`. - * - * @param {String} filename The absolute path of the file in question. - */ -DepperSync.prototype.getTransformsForFile = function(filename) { - var self = this - var entry = this._deps[0] - - if (!entry) throw new Error( - 'getTransformsForFile may only be called after adding your entry file' - ) - - var entryDir = path.dirname(path.resolve(entry.file)) - var fileDir = path.dirname(path.resolve(filename)) - var relative = path.relative(entryDir, fileDir).split(path.sep) - var node_modules = relative.indexOf('node_modules') !== -1 - var trLocal = node_modules ? [] : this._transforms - var trCache = this._trCache - - if (trCache[fileDir]) { - return trCache[fileDir] - } - - try { var found = findup(fileDir, 'package.json') } - catch (err) { - var notFound = err.message === 'not found' - if (notFound) return register([]) - else throw err - } - - var pkg = path.join(found, 'package.json') - var pkgjson = JSON.parse(self.readFile(pkg)) - - var transforms = ( - pkgjson['glslify'] - && pkgjson['glslify']['transform'] - || [] - ) - - transforms = transforms.map(function(key) { - var transform = Array.isArray(key) - ? key - : [key, {}] - - var key = transform[0] - var opt = transform[1] - - if (opt) { - delete opt.global - delete opt.post - } - - return { tr: key, opts: opt, name: key } - }).map(function(tr) { - tr.tr = self.resolveTransform(tr.tr) - return tr - }) - - return register(transforms) - - function register(transforms) { - return trCache[fileDir] = trLocal - .concat(transforms) - .concat(self._globalTransforms) - } -} diff --git a/utils.js b/utils.js new file mode 100644 index 0000000..4b3329a --- /dev/null +++ b/utils.js @@ -0,0 +1,37 @@ +/** + * Gets glslify transform from given package.json + * + * @param {object|string} pkgJson package.json filename path or json + * @returns {({tr: string, name: string, opts: object})[]} + */ +function getTransformsFromPkg(pkgJson) { + if (typeof pkgJson === 'string') { + pkgJson = JSON.parse(pkgJson); + } + + var transforms = ( + pkgJson['glslify'] + && pkgJson['glslify']['transform'] + || [] + ) + + return transforms.map(function(key) { + var transform = Array.isArray(key) + ? key + : [key, {}] + + var key = transform[0] + var opt = transform[1] + + if (opt) { + delete opt.global + delete opt.post + } + + return { tr: key, opts: opt, name: key } + }); +} + +module.exports = { + getTransformsFromPkg +} From 27dc370e90ab564c38a5196e4ee5273d949ed60e Mon Sep 17 00:00:00 2001 From: rubeniskov Date: Sat, 14 Nov 2020 18:52:42 +0100 Subject: [PATCH 12/21] remove unused variable --- sync.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sync.js b/sync.js index 981031e..6a2f759 100644 --- a/sync.js +++ b/sync.js @@ -79,7 +79,7 @@ DepperSync.prototype.add = function(filename) { } } - function resolveImports(resolved) { + function resolveImports() { imports.forEach(function (imp) { var importName = imp.split(/\s*,\s*/).shift() From d39fdc1803b24a09f50767e09ab769d6346ff922 Mon Sep 17 00:00:00 2001 From: rubeniskov Date: Sat, 14 Nov 2020 18:53:22 +0100 Subject: [PATCH 13/21] prevent variable shadowing in resolveImports --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 8d0c777..17099b5 100644 --- a/index.js +++ b/index.js @@ -96,7 +96,7 @@ DepperAsync.prototype.add = function(filename, done) { } } - function resolveImports(resolved) { + function resolveImports(done) { map(imports, 10, function(imp, next) { var importName = imp.split(/\s*,\s*/).shift() @@ -118,6 +118,6 @@ DepperAsync.prototype.add = function(filename, done) { next() }) }) - }, resolved) + }, done) } } From 6e4464dcd7c46d39b672863e223dc97c6165e8ca Mon Sep 17 00:00:00 2001 From: rubeniskov Date: Sat, 14 Nov 2020 18:58:51 +0100 Subject: [PATCH 14/21] refactor migrate logic getImportName from resolveImports --- index.js | 10 +++++----- sync.js | 10 +++++----- utils.js | 12 +++++++++++- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index 17099b5..16b1586 100644 --- a/index.js +++ b/index.js @@ -11,6 +11,10 @@ var { glslifyImport, } = require('./common.js') +var { + getImportName +} = require('./utils'); + module.exports = DepperAsync /** @@ -98,11 +102,7 @@ DepperAsync.prototype.add = function(filename, done) { function resolveImports(done) { map(imports, 10, function(imp, next) { - var importName = imp.split(/\s*,\s*/).shift() - - importName = importName.trim() - importName = importName.replace(/^'|'$/g, '') - importName = importName.replace(/^"|"$/g, '') + var importName = getImportName(imp) self.resolve(importName, { basedir: basedir }, function(err, resolved) { if (err) return next(err) diff --git a/sync.js b/sync.js index 6a2f759..3372b67 100644 --- a/sync.js +++ b/sync.js @@ -10,6 +10,10 @@ var { glslifyImport, } = require('./common.js') +var { + getImportName +} = require('./utils'); + module.exports = DepperSync /** @@ -81,11 +85,7 @@ DepperSync.prototype.add = function(filename) { function resolveImports() { imports.forEach(function (imp) { - var importName = imp.split(/\s*,\s*/).shift() - - importName = importName.trim() - importName = importName.replace(/^'|'$/g, '') - importName = importName.replace(/^"|"$/g, '') + var importName = getImportName(imp) var resolved = self.resolve(importName, { basedir: basedir }) if (cache[resolved]) { diff --git a/utils.js b/utils.js index 4b3329a..bce98d3 100644 --- a/utils.js +++ b/utils.js @@ -32,6 +32,16 @@ function getTransformsFromPkg(pkgJson) { }); } +function getImportName(imp) { + return imp + .split(/\s*,\s*/) + .shift() + .trim() + .replace(/^'|'$/g, '') + .replace(/^"|"$/g, '') +} + module.exports = { - getTransformsFromPkg + getTransformsFromPkg, + getImportName } From 56ed0d71b4a6312ec7b54e54404bf14603fe28bb Mon Sep 17 00:00:00 2001 From: rubeniskov Date: Sat, 14 Nov 2020 19:22:24 +0100 Subject: [PATCH 15/21] remove unused requires --- index.js | 2 -- sync.js | 2 -- 2 files changed, 4 deletions(-) diff --git a/index.js b/index.js index 16b1586..232adec 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,4 @@ var tokenize = require('glsl-tokenizer/string') -var findup = require('@choojs/findup') -var fs = require('graceful-fs') var map = require('map-limit') var inherits = require('inherits') var path = require('path') diff --git a/sync.js b/sync.js index 3372b67..df6c807 100644 --- a/sync.js +++ b/sync.js @@ -1,6 +1,4 @@ var tokenize = require('glsl-tokenizer/string') -var findup = require('@choojs/findup').sync -var fs = require('graceful-fs') var inherits = require('inherits') var path = require('path') var Depper = require('./depper') From bfadac0eb578e44c8b99e0f34ad641636cb169cb Mon Sep 17 00:00:00 2001 From: rubeniskov Date: Sat, 14 Nov 2020 19:27:05 +0100 Subject: [PATCH 16/21] refactor migrate extractPreprocessors from add method --- common.js | 22 ---------------------- depper.js | 3 --- index.js | 27 +++------------------------ sync.js | 27 +++------------------------ utils.js | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 52 insertions(+), 74 deletions(-) delete mode 100644 common.js diff --git a/common.js b/common.js deleted file mode 100644 index 0ea8901..0000000 --- a/common.js +++ /dev/null @@ -1,22 +0,0 @@ -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/depper.js b/depper.js index cb52b92..e9bbc33 100644 --- a/depper.js +++ b/depper.js @@ -9,9 +9,6 @@ var findup = require('@choojs/findup') var { genInlineName, -} = require('./common.js') - -var { getTransformsFromPkg, } = require('./utils.js') diff --git a/index.js b/index.js index 232adec..74c3fcd 100644 --- a/index.js +++ b/index.js @@ -3,14 +3,10 @@ var map = require('map-limit') var inherits = require('inherits') var path = require('path') var Depper = require('./depper') -var { - glslifyPreprocessor, - glslifyExport, - glslifyImport, -} = require('./common.js') var { - getImportName + getImportName, + extractPreprocessors } = require('./utils'); module.exports = DepperAsync @@ -69,7 +65,7 @@ DepperAsync.prototype.add = function(filename, done) { if (err) return done(err) dep.source = src - extractPreprocessors() + extractPreprocessors(dep.source, imports, exports) resolveImports(function(err) { setTimeout(function() { done && done(err, !err && self._deps) @@ -81,23 +77,6 @@ DepperAsync.prototype.add = function(filename, done) { return dep - function extractPreprocessors() { - var tokens = tokenize(dep.source) - - for (var i = 0; i < tokens.length; i++) { - var token = tokens[i] - if (token.type !== 'preprocessor') continue - - var data = token.data - if (!glslifyPreprocessor(data)) continue - - var exp = glslifyExport(data) - var imp = glslifyImport(data) - if (exp) exports.push(exp[1]) - if (imp) imports.push(imp[2]) - } - } - function resolveImports(done) { map(imports, 10, function(imp, next) { var importName = getImportName(imp) diff --git a/sync.js b/sync.js index df6c807..b183208 100644 --- a/sync.js +++ b/sync.js @@ -2,14 +2,10 @@ var tokenize = require('glsl-tokenizer/string') var inherits = require('inherits') var path = require('path') var Depper = require('./depper') -var { - glslifyPreprocessor, - glslifyExport, - glslifyImport, -} = require('./common.js') var { - getImportName + getImportName, + extractPreprocessors } = require('./utils'); module.exports = DepperSync @@ -59,28 +55,11 @@ DepperSync.prototype.add = function(filename) { self.emit('file', filename) src = self.applyTransforms(filename, src, trs) dep.source = src - extractPreprocessors() + extractPreprocessors(dep.source, imports, exports) resolveImports() return self._deps - function extractPreprocessors() { - var tokens = tokenize(dep.source) - - for (var i = 0; i < tokens.length; i++) { - var token = tokens[i] - if (token.type !== 'preprocessor') continue - - var data = token.data - if (!glslifyPreprocessor(data)) continue - - var exp = glslifyExport(data) - var imp = glslifyImport(data) - if (exp) exports.push(exp[1]) - if (imp) imports.push(imp[2]) - } - } - function resolveImports() { imports.forEach(function (imp) { var importName = getImportName(imp) diff --git a/utils.js b/utils.js index bce98d3..a0ac973 100644 --- a/utils.js +++ b/utils.js @@ -1,3 +1,22 @@ +var tokenize = require('glsl-tokenizer/string') + +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() +} + + /** * Gets glslify transform from given package.json * @@ -32,6 +51,30 @@ function getTransformsFromPkg(pkgJson) { }); } +/** + * Extracts preprocessors copying the imports and exports + * into respective parameters + * @param {string} source + * @param {string[]} imports + * @param {string[]} exports + */ +function extractPreprocessors(source, imports, exports) { + var tokens = tokenize(source) + + for (var i = 0; i < tokens.length; i++) { + var token = tokens[i] + if (token.type !== 'preprocessor') continue + + var data = token.data + if (!glslifyPreprocessor(data)) continue + + var exp = glslifyExport(data) + var imp = glslifyImport(data) + if (exp) exports.push(exp[1]) + if (imp) imports.push(imp[2]) + } +} + function getImportName(imp) { return imp .split(/\s*,\s*/) @@ -43,5 +86,7 @@ function getImportName(imp) { module.exports = { getTransformsFromPkg, - getImportName + getImportName, + extractPreprocessors, + genInlineName } From d71c4ba1b9ffacfc49c6859782b2207b940b8388 Mon Sep 17 00:00:00 2001 From: rubeniskov Date: Sat, 14 Nov 2020 20:05:41 +0100 Subject: [PATCH 17/21] split add logic migrating _resolveImports into internal method --- index.js | 53 ++++++++++++++++++++++++++++++++++------------------- sync.js | 46 +++++++++++++++++++++++++++++++--------------- 2 files changed, 65 insertions(+), 34 deletions(-) diff --git a/index.js b/index.js index 74c3fcd..b0cb2f3 100644 --- a/index.js +++ b/index.js @@ -31,7 +31,6 @@ function DepperAsync(opts) { * modules. * * @param {String} filename The absolute path of this file. - * @param {String} src The shader source for this file. * @param {Function} done(err, deps) * * The `done` callback will be called when the entire graph has been @@ -40,7 +39,6 @@ function DepperAsync(opts) { */ DepperAsync.prototype.add = function(filename, done) { var basedir = path.dirname(filename = path.resolve(filename)) - var cache = this._cache var self = this var exports = [] var imports = [] @@ -66,7 +64,10 @@ DepperAsync.prototype.add = function(filename, done) { dep.source = src extractPreprocessors(dep.source, imports, exports) - resolveImports(function(err) { + self._resolveImports(imports, { + deps: dep.deps, + basedir: basedir + }, function(err) { setTimeout(function() { done && done(err, !err && self._deps) }) @@ -76,25 +77,39 @@ DepperAsync.prototype.add = function(filename, done) { }) return dep +} - function resolveImports(done) { - map(imports, 10, function(imp, next) { - var importName = getImportName(imp) +/** + * Internal async method to retrieve dependencies + * resolving imports using the internal cache + * + * @param {string[]} imports + * @param {object} opts extends options for https://www.npmjs.com/package/resolve + * @param {object} opts.deps existing dependencies + * @param {(err: Error)} done + * @return {object} resolved dependencies + */ +DepperAsync.prototype._resolveImports = function(imports, opts, done) { + var self = this + var deps = opts && opts.deps || {} + map(imports, 10, function(imp, next) { + var importName = getImportName(imp) - self.resolve(importName, { basedir: basedir }, function(err, resolved) { - if (err) return next(err) + self.resolve(importName, opts, function(err, resolved) { + if (err) return next(err) - if (cache[resolved]) { - dep.deps[importName] = cache[resolved].id - return next() - } + if (self._cache[resolved]) { + deps[importName] = self._cache[resolved].id + return next() + } - cache[resolved] = self.add(resolved, function(err) { - if (err) return next(err) - dep.deps[importName] = cache[resolved].id - next() - }) + self._cache[resolved] = self.add(resolved, function(err) { + if (err) return next(err) + deps[importName] = self._cache[resolved].id + next() }) - }, done) - } + }) + }, done) + + return deps } diff --git a/sync.js b/sync.js index b183208..eca7193 100644 --- a/sync.js +++ b/sync.js @@ -30,13 +30,11 @@ function DepperSync(opts) { * modules. * * @param {String} filename The absolute path of this file. - * @param {String} src The shader source for this file. * * Returns an array of dependencies discovered so far as its second argument. */ DepperSync.prototype.add = function(filename) { var basedir = path.dirname(filename = path.resolve(filename)) - var cache = this._cache var self = this var exports = [] var imports = [] @@ -57,20 +55,38 @@ DepperSync.prototype.add = function(filename) { dep.source = src extractPreprocessors(dep.source, imports, exports) - resolveImports() + self._resolveImports(imports, { + basedir: basedir, + deps: dep.deps + }) + return self._deps +} + +/** + * Internal sync method to retrieve dependencies + * resolving imports using the internal cache + * + * @param {string[]} imports + * @param {object} opts extends options for https://www.npmjs.com/package/resolve + * @param {object} opts.deps existing dependencies + * @return {object} resolved dependencies + */ +DepperSync.prototype._resolveImports = function(imports, opts) { + var self = this + var deps = opts && opts.deps || {} - function resolveImports() { - imports.forEach(function (imp) { - var importName = getImportName(imp) + imports.forEach(function (imp) { + var importName = getImportName(imp) - var resolved = self.resolve(importName, { basedir: basedir }) - if (cache[resolved]) { - dep.deps[importName] = cache[resolved].id - } - var i = self._i - cache[resolved] = self.add(resolved)[i] - dep.deps[importName] = cache[resolved].id - }) - } + var resolved = self.resolve(importName, opts) + if (self._cache[resolved]) { + deps[importName] = self._cache[resolved].id + } + var i = self._i + self._cache[resolved] = self.add(resolved)[i] + deps[importName] = self._cache[resolved].id + }) + + return deps } From 77963ae3d8e694671b8857b2987bd0f89a7ff786 Mon Sep 17 00:00:00 2001 From: rubeniskov Date: Sat, 14 Nov 2020 20:06:11 +0100 Subject: [PATCH 18/21] remove useless self reference add method --- sync.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/sync.js b/sync.js index eca7193..e7c421f 100644 --- a/sync.js +++ b/sync.js @@ -35,7 +35,6 @@ function DepperSync(opts) { */ DepperSync.prototype.add = function(filename) { var basedir = path.dirname(filename = path.resolve(filename)) - var self = this var exports = [] var imports = [] @@ -49,18 +48,18 @@ DepperSync.prototype.add = function(filename) { this._deps.push(dep) var src = this.readFile(filename) - var trs = self.getTransformsForFile(filename) - self.emit('file', filename) - src = self.applyTransforms(filename, src, trs) + var trs = this.getTransformsForFile(filename) + this.emit('file', filename) + src = this.applyTransforms(filename, src, trs) dep.source = src extractPreprocessors(dep.source, imports, exports) - self._resolveImports(imports, { + this._resolveImports(imports, { basedir: basedir, deps: dep.deps }) - return self._deps + return this._deps } /** From cd1256c888f4afe426c6300bff684a73fba019af Mon Sep 17 00:00:00 2001 From: rubeniskov Date: Sat, 14 Nov 2020 20:23:27 +0100 Subject: [PATCH 19/21] create internal method _addDep --- depper.js | 19 +++++++++++++++++++ index.js | 10 +--------- sync.js | 10 +--------- 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/depper.js b/depper.js index e9bbc33..be91615 100644 --- a/depper.js +++ b/depper.js @@ -66,6 +66,25 @@ Depper.prototype.inline = function(source, basedir, done) { } } +/** + * Internal method to add dependencies + * @param {string} filename + */ +Depper.prototype._addDep = function(filename) { + var dep = { + id: this._i++ + , deps: {} + , file: filename + , source: null + , entry: this._i === 1 + } + + this._deps.push(dep) + + return dep; +} + + /** * Adds a transform to use on your local dependencies. * Note that this should be used before calling `add`. diff --git a/index.js b/index.js index b0cb2f3..6a849a6 100644 --- a/index.js +++ b/index.js @@ -43,15 +43,7 @@ DepperAsync.prototype.add = function(filename, done) { var exports = [] var imports = [] - var dep = { - id: this._i++ - , deps: {} - , file: filename - , source: null - , entry: this._i === 1 - } - - this._deps.push(dep) + var dep = this._addDep(filename) this.readFile(filename, function(err, src) { if (err) return done(err) diff --git a/sync.js b/sync.js index e7c421f..46e9ff9 100644 --- a/sync.js +++ b/sync.js @@ -38,15 +38,7 @@ DepperSync.prototype.add = function(filename) { var exports = [] var imports = [] - var dep = { - id: this._i++ - , deps: {} - , file: filename - , source: null - , entry: this._i === 1 - } - - this._deps.push(dep) + var dep = this._addDep(filename) var src = this.readFile(filename) var trs = this.getTransformsForFile(filename) this.emit('file', filename) From cff17c01b24bf9e2d13dd040e1e312da071add91 Mon Sep 17 00:00:00 2001 From: rubeniskov Date: Sat, 14 Nov 2020 20:25:24 +0100 Subject: [PATCH 20/21] remove unused require tokenize --- index.js | 1 - sync.js | 1 - 2 files changed, 2 deletions(-) diff --git a/index.js b/index.js index 6a849a6..228c935 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,3 @@ -var tokenize = require('glsl-tokenizer/string') var map = require('map-limit') var inherits = require('inherits') var path = require('path') diff --git a/sync.js b/sync.js index 46e9ff9..241a6c7 100644 --- a/sync.js +++ b/sync.js @@ -1,4 +1,3 @@ -var tokenize = require('glsl-tokenizer/string') var inherits = require('inherits') var path = require('path') var Depper = require('./depper') From acafa5100555c7e96555d5900fab78aef784ab9f Mon Sep 17 00:00:00 2001 From: rubeniskov Date: Sat, 14 Nov 2020 20:27:46 +0100 Subject: [PATCH 21/21] update depper interface docs --- depper.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/depper.js b/depper.js index be91615..9a9c8a1 100644 --- a/depper.js +++ b/depper.js @@ -19,6 +19,8 @@ module.exports = Depper * Creates a new instance of glslify-deps. Generally, you'll * want to use one instance per bundle. * + * note: this is an interface to be extended with a top class + * * @class * @param {String} cwd The root directory of your shader. Defaults to process.cwd() */