Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow a dependent module to specify a default transform field #131

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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) });
Expand Down
3 changes: 3 additions & 0 deletions readme.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down