fastify / fastify/fast-json-stringify

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

Open
#679 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
3.7k
Forks
226
Avg merge
1d 59m
Merged PRs (30d)
3

Description

### 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](https://ajv.js.org/standalone.html#generating-functions-s-for-multiple-schemas-using-the-js-library-es6-and-esm-exports) 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](https://github.com/fastify/ajv-compiler) respects the ESM AJV option out of the box.

If we can leverage the [serializerOptions](https://fastify.dev/docs/latest/Reference/Server/#serializeropts), Fastify users in ESM envirmenment can easily use a standalone serializers with the AJV options
```
ajv: {
code: { source: true, esm: true }
}
```

### Example

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.