-
Notifications
You must be signed in to change notification settings - Fork 20
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
feat: cjs support in custom middleware file names #80
Conversation
@olegdovger Hi, this is currently blocking us. Would you be able to support here and review this small PR? |
@Daanoz or @valeriiudodov are you able to help here? |
Hello. Thanks for your contribution! I realized that I'm not running tests on pipelines. While I adding tests to PR actions, can you add test cases and test on your local (by running |
@muratcorlu Insisting on additional test cases here is difficult. You would have to duplicate every GET.js, , POST.js, etc. with almost identical files named GET.cjs, GET.mjs, POST.cjs, POST.mjs, etc... Then run the tests in an environment setup to require CJS files to be named *.cjs, then in one where MJS files need to be named *.mjs and then in one where files need to be named *.js. |
I just tried to write test and realized that, if you write the custom middleware in ESM format like below, current implementation doesn't work, since the library is in commonjs format and we can not just "require" an mjs file. export default function (req, res) {
res.json({
result: 'mjs works'
});
} I get error:
So now I'm curious if you managed to run this in your -manual- test? Do you compile mjs files to commonjs before using with connect-api-mocker? |
I moved everything to cjs because of this reason |
Would it be acceptable to modify this pull request to exclusively support CommonJS and eliminate support for Regarding |
index.js
Outdated
|
||
const methodFiles = [jsMockFile, staticMockFile, wildcardJsMockFile, wildcardStaticMockFile]; | ||
const fileExtensions = [methodFileExtension, 'mjs', 'cjs', 'js'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const fileExtensions = [methodFileExtension, 'mjs', 'cjs', 'js'] | |
const fileExtensions = [methodFileExtension, 'cjs', 'js'] |
index.js
Outdated
@@ -12,6 +12,10 @@ function escapeRegExp(str) { | |||
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&'); | |||
} | |||
|
|||
function isJsFile(str) { | |||
return !!str.match(/\.(c|m)?js$/) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return !!str.match(/\.(c|m)?js$/) | |
return !!str.match(/\.c?js$/) |
Feel free to commit my suggestions |
🎉 This PR is included in version 1.11.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
With the new field
"type": "module",
in package.json node js might require different file names.Therefore I added logic that it also handles .cjs and .mjs files