connect-api-mocker icon indicating copy to clipboard operation
connect-api-mocker copied to clipboard

Add mjs and cjs support

Open valoricDe opened this issue 2 years ago • 3 comments

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

valoricDe avatar Dec 20 '23 13:12 valoricDe

@olegdovger Hi, this is currently blocking us. Would you be able to support here and review this small PR?

valoricDe avatar Jan 05 '24 15:01 valoricDe

@Daanoz or @valeriiudodov are you able to help here?

valoricDe avatar Jan 10 '24 09:01 valoricDe

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 avatar Jan 17 '24 19:01 muratcorlu

@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?

riverrun-git avatar Mar 18 '24 12:03 riverrun-git

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?

muratcorlu avatar Mar 18 '24 15:03 muratcorlu

I moved everything to cjs because of this reason

valoricDe avatar Mar 19 '24 11:03 valoricDe

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.

muratcorlu avatar Mar 19 '24 12:03 muratcorlu

Feel free to commit my suggestions

valoricDe avatar Mar 20 '24 11:03 valoricDe

:tada: This PR is included in version 1.11.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

github-actions[bot] avatar Mar 20 '24 17:03 github-actions[bot]