fast-json-stringify icon indicating copy to clipboard operation
fast-json-stringify copied to clipboard

emit CJS/ESM syntax in standalone mode based on AJV code options

Open mouhannad-sh opened this issue 1 year ago • 1 comments

Prerequisites

  • [X] I have written a descriptive issue title
  • [X] I have searched existing issues to ensure the feature has not already been requested

🚀 Feature Proposal

Use the AJV code option to decide if the standalone code generator would emit CJS (require/module) or ESM (import/export) instead of hardcoding require and module.exports as shown below in the standalone.js file

https://github.com/fastify/fast-json-stringify/blob/74c35c0ee4c2dd12cc8edf8acad4b2f46c0542bb/lib/standalone.js#L14-L23

Motivation

Typescript and ESM users will have to go through some build system configurations to resolve the require in ESM runtime headache during development. Especially that the StandaloneValidator from the @fastify/ajv-compiler package respects the ESM AJV option out of the box.

If we can leverage the serializerOptions, Fastify users in ESM envirmenment can easily use a standalone serializers with the AJV options

ajv: {
  code: { source: true, esm: true } 
}

Example

No response

mouhannad-sh avatar Feb 13 '24 09:02 mouhannad-sh

Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests.

mcollina avatar Feb 13 '24 09:02 mcollina