Skip to content

Commit

Permalink
allow deep require
Browse files Browse the repository at this point in the history
(cherry picked from commit e5238d2)
  • Loading branch information
jamestalmage committed Dec 15, 2015
1 parent 975a689 commit 73f7aba
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module.exports = appendTransform;

function appendTransform(transform, ext, extensions) {
var key = __filename + ':' + Math.random(); // eslint-disable-line

This comment has been minimized.

Copy link
@jamestalmage

jamestalmage Dec 18, 2015

Author Member

I think this could probably be better. __filename protects against multiple non-deduped copies of append-transform being used. I don't know that random() is necessary. I could just increment an integer with every call (if you install more than Number.MAX_VALUE require extensions your system is not going to accomplish much.

I'm not sure why I chose random. Maybe to protect against require cache deletions? Seems unlikely.

This comment has been minimized.

Copy link
@novemberborn

novemberborn Dec 18, 2015

Different transforms will have a different key. That seems good. But incrementing an integer would be better since you won't have key collisions.

ext = ext || '.js';
extensions = extensions || require.extensions;

Expand Down Expand Up @@ -36,28 +37,24 @@ function appendTransform(transform, ext, extensions) {
};
}

var isEntry = true;

function wrapCustomHook(hook) {
return function (module, filename) {
var wasEntry = isEntry;
isEntry = false;
var isEntry = !module[key];
if (isEntry) {
module[key] = true;

This comment has been minimized.

Copy link
@novemberborn

novemberborn Dec 18, 2015

Should this be set to false at any point?

This comment has been minimized.

Copy link
@jamestalmage

jamestalmage Dec 18, 2015

Author Member

No, module[key] will be undefined if we have never seen this before (meaning we are at the top of the stack - the first transform to see a given module), since we immediately set it to true and never change it !modue[key] tells us if we are the "entry" transform (the top of the stack, where the transform should be applied before calling the final ._compile

This comment has been minimized.

Copy link
@novemberborn

novemberborn Dec 18, 2015

I'm having some trouble seeing what this accomplishes. It's per module, so that seems better than the earlier code. But replacementCompile() changes module._compile, so it shouldn't really be called more than once anyway. Unless the hook keeps a reference to the replacementCompile and somehow calls it more than once. That'd be weird though.

This comment has been minimized.

Copy link
@jamestalmage

jamestalmage Dec 18, 2015

Author Member

Good call.

See ad3d22b

}

var originalCompile = module._compile;

module._compile = function replacementCompile(code) {
module._compile = originalCompile;
if (wasEntry) {
if (isEntry) {
code = transform(code, filename);
}
module._compile(code, filename);
};

hook(module, filename);

if (wasEntry) {
isEntry = true;
}
};
}

Expand Down

0 comments on commit 73f7aba

Please sign in to comment.