Skip to content

Commit

Permalink
refactor migrate this._cwd logic to NodeDepper
Browse files Browse the repository at this point in the history
  • Loading branch information
rubeniskov committed Nov 17, 2020
1 parent 35fb74f commit 355d42e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 14 deletions.
23 changes: 10 additions & 13 deletions depper.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -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<string, string>} [files] a filename/source object mapping of files to prepopulate the file cache with. Useful for overriding.
Expand Down Expand Up @@ -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')
}
Expand Down Expand Up @@ -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)
}

/**
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -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) {
Expand Down
47 changes: 46 additions & 1 deletion node.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -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)
Expand All @@ -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);
}

/**
Expand All @@ -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:
Expand Down

0 comments on commit 355d42e

Please sign in to comment.