Skip to content

Commit 5b67503

Browse files
author
rubeniskov
committed
refactor migrate this._cwd logic to NodeDepper
1 parent 35fb74f commit 5b67503

File tree

2 files changed

+57
-15
lines changed

2 files changed

+57
-15
lines changed

depper.js

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// @ts-check
22
/** @typedef {import('glsl-resolve')} GlslResolve */
3-
var path = require('path')
43
var Emitter = require('events/')
54
var inherits = require('inherits')
65
var map = require('map-limit')
@@ -70,7 +69,6 @@ var {
7069
/**
7170
* @typedef {Object} DepperOptions
7271
* @prop {Boolean} [async] Defines the mechanism flow resolution.
73-
* @prop {String} [cwd] The root directory of your shader. Defaults to process.cwd().
7472
* @prop {Function} [readFile] pass in a custom function reading files.
7573
* @prop {GlslResolve} [resolve] pass in a custom function for resolving require calls. It has the same signature as glsl-resolve.
7674
* @prop {Object<string, string>} [files] a filename/source object mapping of files to prepopulate the file cache with. Useful for overriding.
@@ -101,17 +99,12 @@ function Depper(opts) {
10199

102100
this._cache = {}
103101
this._fileCache = parseFiles(Object.assign({}, opts.files) || {})
104-
this._cwd = opts.cwd || process.cwd()
105102

106103
/** @type {TransformDefinition[]} */
107104
this._transforms = []
108105
/** @type {TransformDefinition[]} */
109106
this._globalTransforms = []
110107

111-
if (typeof this._cwd !== 'string') {
112-
throw new Error('glslify-deps: cwd must be a string path')
113-
}
114-
115108
if (!opts.readFile) {
116109
throw new Error('glslify-deps: readFile must be defined')
117110
}
@@ -139,10 +132,9 @@ function Depper(opts) {
139132
}
140133
}
141134

142-
Depper.prototype.inline = function(source, basedir, done) {
143-
var inlineFile = path.resolve(basedir || this._cwd, this._inlineName)
135+
Depper.prototype.inline = function(source, filename, done) {
144136
this._inlineSource = source
145-
return this.add(inlineFile, done)
137+
return this.add(filename || this._inlineName, done)
146138
}
147139

148140
/**
@@ -263,11 +255,16 @@ Depper.prototype.transform = function(transform, opts) {
263255
*
264256
*
265257
* @param {String|GlslTransform} transform
258+
* @param {Object} [opts] The options will be pased to transformRequire function.
266259
* @param {(err: Error, transform?: GlslTransform) => any} [done] Applies if is defined
267260
* @return {Function}
268261
*/
269-
Depper.prototype.resolveTransform = function(transform, done) {
270-
var opts = { cwd: this._cwd }
262+
Depper.prototype.resolveTransform = function(transform, opts, done) {
263+
if (typeof opts === 'function') {
264+
done = opts
265+
opts = {}
266+
}
267+
271268
var self = this
272269

273270
if (typeof transform === 'function') {
@@ -358,7 +355,7 @@ Depper.prototype._addDep = function(file, extra) {
358355
}
359356

360357
/**
361-
* Internal method to register
358+
* Internal method to register transforms
362359
* @param {TransformDefinition[]} transforms
363360
* @param {(err: Error, resolved?: TransformResolved[]) => any} cb
364361
* @returns {TransformResolved[]}
@@ -444,7 +441,7 @@ Depper.prototype._resolveImports = function(imports, opts, done) {
444441
}
445442

446443
Depper.prototype.readFile = function(filename, done) {
447-
if (path.basename(filename) !== this._inlineName)
444+
if (filename !== this._inlineName)
448445
return this._readFile(filename, done)
449446

450447
if(this._async) {

node.js

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/** @typedef {import('./depper').DepperOptions} DepperOptions */
22
var Depper = require('./depper')
33
var path = require('path')
4-
var map = require('map-limit')
54
var inherits = require('inherits')
65
var fs = require('graceful-fs')
76
var findup = require('@choojs/findup')
@@ -32,6 +31,7 @@ function createDefaultRead() {
3231
*//**
3332
* @constructor
3433
* @param {DepperOptions} opts
34+
* @param {String} [opts.cwd] The root directory of your shader. Defaults to process.cwd().
3535
*/
3636
function NodeDepper(opts) {
3737
if (!(this instanceof NodeDepper)) return new NodeDepper(opts)
@@ -41,7 +41,24 @@ function NodeDepper(opts) {
4141
opts.transformRequire = opts.transformRequire || transformRequire.sync
4242
opts.readFile = opts.readFile || createDefaultRead()
4343
Depper.call(this, opts)
44+
45+
this._cwd = opts.cwd || process.cwd()
4446
this._trCache = {}
47+
48+
if (typeof this._cwd !== 'string') {
49+
throw new Error('glslify-deps: cwd must be a string path')
50+
}
51+
}
52+
53+
/**
54+
* @override
55+
* @param {*} source
56+
* @param {*} basedir
57+
* @param {*} done
58+
*/
59+
NodeDepper.prototype.inline = function(source, basedir, done) {
60+
var inlineFile = path.resolve(basedir || this._cwd, this._inlineName)
61+
return Depper.prototype.inline.call(this, source, inlineFile, done);
4562
}
4663

4764
/**
@@ -56,6 +73,34 @@ NodeDepper.prototype.add = function(filename, done) {
5673
}, done)
5774
}
5875

76+
/**
77+
* @override
78+
* @param {String|GlslTransform} transform
79+
* @param {(err: Error, transform?: GlslTransform) => any} [done] Applies if is defined
80+
* @return {Function}
81+
*/
82+
NodeDepper.prototype.resolveTransform = function(transform, done) {
83+
return Depper.prototype.resolveTransform.call(this, transform, {
84+
cwd: this._cwd
85+
}, done)
86+
}
87+
88+
/**
89+
* @override
90+
* @param {*} filename
91+
* @param {*} done
92+
*/
93+
NodeDepper.prototype.readFile = function(filename, done) {
94+
if (path.basename(filename) !== this._inlineName)
95+
return this._readFile(filename, done)
96+
97+
if(this._async) {
98+
return done(null, this._inlineSource)
99+
}
100+
return this._inlineSource
101+
}
102+
103+
59104
/**
60105
* Determines which transforms to use for a particular file.
61106
* The rules here are the same you see in browserify:

0 commit comments

Comments
 (0)