From c4102b0e1f381c52ad318099e82ab42d98e0291f Mon Sep 17 00:00:00 2001 From: serapath Date: Fri, 12 May 2017 00:51:56 +0700 Subject: [PATCH] allow a dependent module to specify a default transform field as a fallback --- index.js | 27 ++++++++++++++------------- readme.markdown | 3 +++ 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/index.js b/index.js index 45cbeac..e1d512f 100644 --- a/index.js +++ b/index.js @@ -222,7 +222,8 @@ Deps.prototype.getTransforms = function (file, pkg, opts) { var transforms = [].concat(isTopLevel ? this.transforms : []) .concat(getTransforms(pkg, { globalTransform: this.globalTransforms, - transformKey: this.options.transformKey + transformKey: this.options.transformKey, + defaultTransformKey: this.options.defaultTransformKey })) ; if (transforms.length === 0) return through(); @@ -547,21 +548,21 @@ Deps.prototype.lookupPackage = function (file, cb) { }; function getTransforms (pkg, opts) { - var trx = []; - if (opts.transformKey) { - var n = pkg; - var keys = opts.transformKey; - for (var i = 0; i < keys.length; i++) { - if (n && typeof n === 'object') n = n[keys[i]]; - else break; - } - if (i === keys.length) { - trx = [].concat(n).filter(Boolean); - } - } + var trx = getTrx(opts.transformKey, pkg); + if (!trx) trx = getTrx(opts.defaultTransformKey, pkg) || []; return trx.concat(opts.globalTransform || []); } +function getTrx (keys, n) { + if (keys) { + for (var i = 0; i < keys.length; i++) { + if (n && typeof n === 'object') n = n[keys[i]]; + else break; + } + if (i === keys.length && n) return [].concat(n).filter(Boolean); + } +} + function nextTick (cb) { var args = [].slice.call(arguments, 1); process.nextTick(function () { cb.apply(null, args) }); diff --git a/readme.markdown b/readme.markdown index 8dd1f68..2523f8b 100644 --- a/readme.markdown +++ b/readme.markdown @@ -68,6 +68,9 @@ Optionally pass in some `opts`: package.json for source transformations. If falsy, don't look at the package.json at all. +* `opts.defaultTransformKey` - if provided, used as a fallback +in case `opts.transformKey` was not provided or couldn't find any transforms + * `opts.resolve` - custom resolve function using the `opts.resolve(id, parent, cb)` signature that [browser-resolve](https://github.com/shtylman/node-browser-resolve) has