Skip to content

Commit

Permalink
Merge pull request #179 from breautek/shebang-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jhnns committed Dec 18, 2021
2 parents 869fb2c + 0244069 commit 54440b6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/moduleEnv.js
Expand Up @@ -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;

Expand Down Expand Up @@ -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
);
};
Expand Down
5 changes: 5 additions & 0 deletions testLib/sharedTestCases.js
Expand Up @@ -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();
});
};
7 changes: 7 additions & 0 deletions testLib/shebangModule.js
@@ -0,0 +1,7 @@
#!/usr/bin/env node

function shebangs() {
return true;
}

module.exports.shebangs = shebangs;

0 comments on commit 54440b6

Please sign in to comment.