feat: cjs support in custom middleware file names#80
feat: cjs support in custom middleware file names#80muratcorlu merged 3 commits intomuratcorlu:masterfrom
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 wildcardStaticMockFile = `ANY.${methodFileExtension}`; | ||
|
|
||
| const methodFiles = [jsMockFile, staticMockFile, wildcardJsMockFile, wildcardStaticMockFile]; | ||
| const fileExtensions = [methodFileExtension, 'mjs', 'cjs', 'js'] |
There was a problem hiding this comment.
| const fileExtensions = [methodFileExtension, 'mjs', 'cjs', 'js'] | |
| const fileExtensions = [methodFileExtension, 'cjs', 'js'] |
index.js
Outdated
| } | ||
|
|
||
| function isJsFile(str) { | ||
| return !!str.match(/\.(c|m)?js$/) |
There was a problem hiding this comment.
| 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