Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed import errors on modules with shebang declarations #179

Merged
merged 2 commits into from
Dec 18, 2021
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
6 changes: 5 additions & 1 deletion lib/moduleEnv.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env node

function shebangs() {
return true;
}

module.exports.shebangs = shebangs;