question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Option to generate ESM for standalone validators

See original GitHub issue

What 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:

  1. Add an esm option to ajv compile/standaloneCode
  2. 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:closed
  • Created 2 years ago
  • Reactions:10
  • Comments:12 (9 by maintainers)

github_iconTop GitHub Comments

8reactions
realitykingcommented, Mar 30, 2021

If anything, arguably, it is definitely more in scope of rollup or webpack to have better support for CommonJS that it is in scope of Ajv to generate ESM, but in any case another tool can be used.

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.

1reaction
kmoschcaucommented, Oct 23, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found