diff --git a/.gitignore b/.gitignore index 6ace32b..0820d2c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ .DS_Store npm-debug.log -node_modules +/node_modules /npm-alias diff --git a/package.json b/package.json index b0538d5..72c6c3f 100644 --- a/package.json +++ b/package.json @@ -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" } diff --git a/register.js b/register.js index 7684ca8..75a70ec 100644 --- a/register.js +++ b/register.js @@ -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 { @@ -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 diff --git a/test.sh b/test.sh index 6d8a526..47c1126 100755 --- a/test.sh +++ b/test.sh @@ -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); diff --git a/test/node_modules/test-node-module.js b/test/node_modules/test-node-module.js new file mode 100644 index 0000000..47244aa --- /dev/null +++ b/test/node_modules/test-node-module.js @@ -0,0 +1,4 @@ +// @flow + +var n: number = 42; +console.log(n);