Skip to content
This repository has been archived by the owner on Jun 17, 2024. It is now read-only.

Fixes to include/exclude behavior #1

Merged
merged 3 commits into from
May 3, 2018
Merged
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.DS_Store
npm-debug.log
node_modules
/node_modules
/npm-alias
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
],
"dependencies": {
"babylon": "^7.0.0-beta.41",
"node-modules-regexp": "^1.0.0",
"pirates": "^3.0.2",
"vlq": "^0.2.1"
}
Expand Down
13 changes: 10 additions & 3 deletions register.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = function setOptions(newOptions) {

var jsLoader = require.extensions['.js'];
var exts = [ '.js', '.mjs', '.jsx', '.flow', '.es6' ];
var nodeModulesRegexp = require('node-modules-regexp');

var revert = pirates.addHook(function hook(code, filename) {
try {
Expand All @@ -25,15 +26,21 @@ var revert = pirates.addHook(function hook(code, filename) {
e.message = filename + ': ' + e.message;
throw e;
}
}, { exts: exts, matcher: shouldTransform });
}, { exts: exts, matcher: shouldTransform, ignoreNodeModules: false });

function shouldTransform(filename) {
var includes = options && regexpPattern(options.includes || options.include);
var excludes =
options && 'excludes' in options ? regexpPattern(options.excludes) :
options && 'exclude' in options ? regexpPattern(options.exclude) :
/\/node_modules\//;
return (!includes || includes.test(filename)) && !(excludes && excludes.test(filename));
nodeModulesRegexp;
if (includes && includes.test(filename)) {
return true;
}
if (excludes && excludes.test(filename)) {
return false;
}
return true;
}

// Given a null | string | RegExp | any, returns null | Regexp or throws a
Expand Down
10 changes: 10 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ echo "Test: node require hook"
RES=$(node -e 'require("./register");require("./test/test-node-module.js")');
if [ "$RES" != 42 ]; then echo 'Node require hook failed'; exit 1; fi;

# Test node require hook: exclude override
echo "Test: node require hook with excludes override"
RES=$(node -e 'require("./register")({excludes: null});require("./test/node_modules/test-node-module.js")');
if [ "$RES" != 42 ]; then echo 'Node require hook: exclude override failed'; exit 1; fi;

# Test node require hook: include priority
echo "Test: node require hook with include priority"
RES=$(node -e 'require("./register")({includes: "test-node-module.js"});require("./test/node_modules/test-node-module.js")');
if [ "$RES" != 42 ]; then echo 'Node require hook: include priority failed'; exit 1; fi;

# Test flow-node
echo "Test: flow-node"
FLOW_NODE=$(./flow-node ./test/test-node-module.js);
Expand Down
4 changes: 4 additions & 0 deletions test/node_modules/test-node-module.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.