From 5b67503251767e57c89ae191d4f703b3236b536f Mon Sep 17 00:00:00 2001 From: rubeniskov Date: Tue, 17 Nov 2020 12:19:25 +0100 Subject: [PATCH] refactor migrate this._cwd logic to NodeDepper --- depper.js | 25 +++++++++++-------------- node.js | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 15 deletions(-) diff --git a/depper.js b/depper.js index a2f4347..76e3518 100644 --- a/depper.js +++ b/depper.js @@ -1,6 +1,5 @@ // @ts-check /** @typedef {import('glsl-resolve')} GlslResolve */ -var path = require('path') var Emitter = require('events/') var inherits = require('inherits') var map = require('map-limit') @@ -70,7 +69,6 @@ var { /** * @typedef {Object} DepperOptions * @prop {Boolean} [async] Defines the mechanism flow resolution. - * @prop {String} [cwd] The root directory of your shader. Defaults to process.cwd(). * @prop {Function} [readFile] pass in a custom function reading files. * @prop {GlslResolve} [resolve] pass in a custom function for resolving require calls. It has the same signature as glsl-resolve. * @prop {Object} [files] a filename/source object mapping of files to prepopulate the file cache with. Useful for overriding. @@ -101,17 +99,12 @@ function Depper(opts) { this._cache = {} this._fileCache = parseFiles(Object.assign({}, opts.files) || {}) - this._cwd = opts.cwd || process.cwd() /** @type {TransformDefinition[]} */ this._transforms = [] /** @type {TransformDefinition[]} */ this._globalTransforms = [] - if (typeof this._cwd !== 'string') { - throw new Error('glslify-deps: cwd must be a string path') - } - if (!opts.readFile) { throw new Error('glslify-deps: readFile must be defined') } @@ -139,10 +132,9 @@ function Depper(opts) { } } -Depper.prototype.inline = function(source, basedir, done) { - var inlineFile = path.resolve(basedir || this._cwd, this._inlineName) +Depper.prototype.inline = function(source, filename, done) { this._inlineSource = source - return this.add(inlineFile, done) + return this.add(filename || this._inlineName, done) } /** @@ -263,11 +255,16 @@ Depper.prototype.transform = function(transform, opts) { * * * @param {String|GlslTransform} transform + * @param {Object} [opts] The options will be pased to transformRequire function. * @param {(err: Error, transform?: GlslTransform) => any} [done] Applies if is defined * @return {Function} */ -Depper.prototype.resolveTransform = function(transform, done) { - var opts = { cwd: this._cwd } +Depper.prototype.resolveTransform = function(transform, opts, done) { + if (typeof opts === 'function') { + done = opts + opts = {} + } + var self = this if (typeof transform === 'function') { @@ -358,7 +355,7 @@ Depper.prototype._addDep = function(file, extra) { } /** - * Internal method to register + * Internal method to register transforms * @param {TransformDefinition[]} transforms * @param {(err: Error, resolved?: TransformResolved[]) => any} cb * @returns {TransformResolved[]} @@ -444,7 +441,7 @@ Depper.prototype._resolveImports = function(imports, opts, done) { } Depper.prototype.readFile = function(filename, done) { - if (path.basename(filename) !== this._inlineName) + if (filename !== this._inlineName) return this._readFile(filename, done) if(this._async) { diff --git a/node.js b/node.js index 798dff3..74d116a 100644 --- a/node.js +++ b/node.js @@ -1,7 +1,6 @@ /** @typedef {import('./depper').DepperOptions} DepperOptions */ var Depper = require('./depper') var path = require('path') -var map = require('map-limit') var inherits = require('inherits') var fs = require('graceful-fs') var findup = require('@choojs/findup') @@ -32,6 +31,7 @@ function createDefaultRead() { *//** * @constructor * @param {DepperOptions} opts + * @param {String} [opts.cwd] The root directory of your shader. Defaults to process.cwd(). */ function NodeDepper(opts) { if (!(this instanceof NodeDepper)) return new NodeDepper(opts) @@ -41,7 +41,24 @@ function NodeDepper(opts) { opts.transformRequire = opts.transformRequire || transformRequire.sync opts.readFile = opts.readFile || createDefaultRead() Depper.call(this, opts) + + this._cwd = opts.cwd || process.cwd() this._trCache = {} + + if (typeof this._cwd !== 'string') { + throw new Error('glslify-deps: cwd must be a string path') + } +} + +/** + * @override + * @param {*} source + * @param {*} basedir + * @param {*} done + */ +NodeDepper.prototype.inline = function(source, basedir, done) { + var inlineFile = path.resolve(basedir || this._cwd, this._inlineName) + return Depper.prototype.inline.call(this, source, inlineFile, done); } /** @@ -56,6 +73,34 @@ NodeDepper.prototype.add = function(filename, done) { }, done) } +/** + * @override + * @param {String|GlslTransform} transform + * @param {(err: Error, transform?: GlslTransform) => any} [done] Applies if is defined + * @return {Function} + */ +NodeDepper.prototype.resolveTransform = function(transform, done) { + return Depper.prototype.resolveTransform.call(this, transform, { + cwd: this._cwd + }, done) +} + +/** + * @override + * @param {*} filename + * @param {*} done + */ +NodeDepper.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 +} + + /** * Determines which transforms to use for a particular file. * The rules here are the same you see in browserify: