Option to generate ESM for standalone validators
See original GitHub issueWhat version of Ajv you are you using?
8.0.1
What problem do you want to solve?
Importing CommonJS modules creates non-ideal code when bundling with Rollup or Webpack and let them not participate in tree shaking. Code will be smaller and more efficient when ESM is used for bundling. This will also gain in importance as npm packages will soon ship more and more ESM code.
What do you think is the correct solution to problem?
There are two parts to be done here:
- Add an
esm
option toajv compile
/standaloneCode
- Ship ESM versions of the runtime files and have an
exports
map for Bundles and Node.js to pick them up correctly.
The first change is the more important one but both are necessary for the full effect.
Will you be able to implement it?
I’m not sure. I started looking through the code a bit and I see how to get started but I expect to get stuck at some point. I’d definitely need help down the line.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:10
- Comments:12 (9 by maintainers)
Top Results From Across the Web
Standalone validation code - Ajv JSON schema validator
The first step is to generate the standalone validation function code. This is done at compile/build time of your project and the output...
Read more >Ajv JSON schema validator - npm
Ajv generates code to turn JSON Schemas into super-fast validation functions that are efficient for v8 optimization.
Read more >Dynamic modules | NestJS - A progressive Node.js framework
Using dynamic module features, we can make our configuration module ... Our requirement is to make ConfigModule accept an options object to customize...
Read more >SIEM 11.x – Upgrade Guide - McAfee
ESM Scalability- Clustering. ▫. Flexible configuration options: replication and scaling are now posible. By replication, all data is duplicated into the ...
Read more >babel/preset-env
For example, if you want to create a snapshot of a query run npx browserslist --mobile-to-desktop ">0.25%, not dead" . Options. For more...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Both Webpack and Rollup have invested a lot of time into this and it works from interoperability standpoint. It does however generate less optimal code (everything will be wrapped in a closure) and in the case of Webpack the module won’t participate in tree shaking.
ESM is the strictest of all the module formats as every export and import has to be static. This makes it easiest to transpile into any other module format. With both browsers and Node.js converging on it as the shared standard, I believe it’s where the future is heading. It’d be great to gain of some the benefits with standalone validators generated by AJV.
I would also be interested in this. I am using AJV in an environment where I can’t easily add dependencies without shipping the entire dependency with my code. So the standalone code is already a really nice feature. However that environment only uses ESM, so currently I have to resort back to string replacement or similar. It would be great if AJV would have at least an option for the code generation to generate ESM instead of CommonJS.