diff --git a/lib/moduleEnv.js b/lib/moduleEnv.js index 3b05bca..e5bb2e8 100644 --- a/lib/moduleEnv.js +++ b/lib/moduleEnv.js @@ -43,6 +43,8 @@ var moduleWrapper0 = Module.wrapper[0], // errors anyway, it's probably still a reasonable trade-off. // Test the regular expresssion at https://regex101.com/r/dvnZPv/2 and also check out testLib/constModule.js. matchConst = /(^|\s|\}|;)const(\/\*|\s|{)/gm, + // Required for importing modules with shebang declarations, since NodeJS 12.16.0 + shebang = /^(#!).+/, nodeRequire, currentModule; @@ -123,7 +125,9 @@ function jsExtension(module, filename) { _compile.call( module, - content.replace(matchConst, "$1let $2"), // replace const with let, while maintaining the column width + content + .replace(shebang, '') // Remove shebang declarations + .replace(matchConst, "$1let $2"), // replace const with let, while maintaining the column width filename ); }; diff --git a/testLib/sharedTestCases.js b/testLib/sharedTestCases.js index a4158d9..803f59e 100644 --- a/testLib/sharedTestCases.js +++ b/testLib/sharedTestCases.js @@ -408,4 +408,9 @@ module.exports = function () { }).to.throwException(/^Assignment to constant variable at .+?wrongConstModule\.js:4:1$/); }); + it("should be possible to rewire shebang modules", function () { + expect(function () { + rewire("./shebangModule"); + }).to.not.throwError(); + }); }; diff --git a/testLib/shebangModule.js b/testLib/shebangModule.js new file mode 100644 index 0000000..da6ac74 --- /dev/null +++ b/testLib/shebangModule.js @@ -0,0 +1,7 @@ +#!/usr/bin/env node + +function shebangs() { + return true; +} + +module.exports.shebangs = shebangs;