Add mjs and cjs support
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
@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 npm test)
@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. How to achieve that in a test pipeline is unclear. This is going to be a blocker for us soon. Could you not review the (trivial) changes instead?
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:
SyntaxError: Unexpected token 'export'
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 .mjs files, enabling us to proceed with the merge?
Regarding .mjs, we could contemplate transitioning this library to ECMAScript Modules (ESM) to facilitate support for both module systems. This approach is preferable because importing CommonJS files into ESM modules is straightforward, whereas importing .mjs files into CommonJS requires additional effort. Besides, ESM represents the future direction of JavaScript module systems.
Feel free to commit my suggestions
:tada: This PR is included in version 1.11.0 :tada:
The release is available on:
Your semantic-release bot :package::rocket: