Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
2 tasks done
mouhannad-sh opened this issue Feb 13, 2024 · 1 comment
Open
2 tasks done

Comments

@mouhannad-sh
Copy link

Prerequisites

  • I have written a descriptive issue title
  • 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

const { dependencies } = require('fast-json-stringify/lib/standalone')
const { Serializer, Validator } = dependencies
const serializerState = ${JSON.stringify(serializer.getState())}
const serializer = Serializer.restoreFromState(serializerState)
${ajvDependencyCode}
module.exports = ${contextFunc.toString()}(validator, serializer)`

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 mouhannad-sh changed the title emit CJS/EMS syntax in standalone mode based on AJV code options emit CJS/ESM syntax in standalone mode based on AJV code options Feb 13, 2024
@mcollina
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants