Skip to content

Commit

Permalink
refactor migrate getTransformsForFile logic to mixed depper class
Browse files Browse the repository at this point in the history
  • Loading branch information
rubeniskov committed Nov 14, 2020
1 parent 877676c commit 2b87d4c
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 157 deletions.
91 changes: 90 additions & 1 deletion depper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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) {
Expand All @@ -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)
Expand Down
81 changes: 0 additions & 81 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
75 changes: 0 additions & 75 deletions sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
37 changes: 37 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
@@ -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})[]}
*/
const 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
}

0 comments on commit 2b87d4c

Please sign in to comment.