ajv-cli icon indicating copy to clipboard operation
ajv-cli copied to clipboard

Add `--code-esm` support

Open willfarrell opened this issue 3 years ago • 16 comments

As of [email protected], compiling to esm is now supported. This PR allows that functionality to be accessed.

willfarrell avatar Jan 20 '22 17:01 willfarrell

Thank you. It all fails because of some typescript change - the same happened in other places, the code needs to be updated to compile…

epoberezkin avatar Jan 20 '22 22:01 epoberezkin

Note the inclusion of "useUnknownInCatchVariables": false. I tried the other patterns suggested from [1], but they still errored when using err.code (Property 'code' does not exist on type 'Error'.)

[1] https://stackoverflow.com/questions/60151181/object-is-of-type-unknown-typescript-generics

willfarrell avatar Jan 24 '22 23:01 willfarrell

PR reviewed and tested 👍 (I didn't realize this PR was already open when I sent https://github.com/ajv-validator/ajv-cli/pull/203).

@epoberezkin is there anything else that needs to happen before this can be merged? Thanks!

bitjson avatar Apr 14 '22 15:04 bitjson

@epoberezkin could this PR be merged? (Or maybe @jessedc?) It looks like other teams are also having to work around this issue where ajv-cli is missing the esm option from ajv. Or maybe is there some other way you'd prefer that the option be supported by ajv-cli?

bitjson avatar May 17 '22 15:05 bitjson

I would like to second the request for merging this. It's not easily usable directly from the fork repo.

Update: In case it helps anyone else, I forked the fork to swap prepublish with prepare, which allows use directly from the source repo: github:trevorr/ajv-cli#code-esm-prepare.

trevorr avatar Jun 16 '22 22:06 trevorr

Hey there @epoberezkin can you merge this PR, please? I had to use patch-package to get around this issue.

My diff file ajv-cli+5.0.0.patch

diff --git a/node_modules/ajv-cli/dist/commands/options.js b/node_modules/ajv-cli/dist/commands/options.js
index 23493ad..5d8c8c7 100644
--- a/node_modules/ajv-cli/dist/commands/options.js
+++ b/node_modules/ajv-cli/dist/commands/options.js
@@ -26,6 +26,7 @@ const ajvOptions = {
     multipleOfPrecision: boolOrNat,
     messages: { type: "boolean" },
     [`${CODE}es5`]: { type: "boolean" },
+    [`${CODE}esm`]: { type: "boolean" },
     [`${CODE}lines`]: { type: "boolean" },
     [`${CODE}optimize`]: boolOrNat,
     [`${CODE}formats`]: { type: "string" },

Creskendoll avatar Jul 01 '22 13:07 Creskendoll

Any news on this?

jonyw4 avatar Jul 20 '22 18:07 jonyw4

If you're having this issue, I recommend you to either use patch-package as I described above, or do a similar thing without using the cli:

import standaloneCode from 'ajv/dist/standalone/index.js'
import glob from 'glob'
import Ajv from 'ajv'

// Read the .json schemas
const definitions = glob('validations/**/*.json', { sync: true })
const files = await Promise.all(definitions.map(async (f) => JSON.parse((await readFile(f)).toString())))

// Use Ajv with esm set to true 
const ajv = new Ajv({
  schemas: files,
  code: {
    source: true,
    esm: true,
  },
})

// Compile and save the code
writeFileSync('out.js', standaloneCode(ajv))

Creskendoll avatar Jul 25 '22 12:07 Creskendoll

Sorry for just 👍🏼 bumping this PR, but what is the holdback for getting the merge done? ESM is getting more and more popular and having to copy and run some code is not optimal solution. Thanks!

sondreb avatar Nov 23 '22 23:11 sondreb

Thread bump; this simple change would enable pipelines to generate ESM schema instead of baking generation code into the runtime. It would unlock massive perf gains for us!

yelper avatar Jan 05 '23 22:01 yelper

Is there anything preventing the merge of this PR?

MarcDOLF avatar Apr 21 '23 12:04 MarcDOLF

What's the hold up?

dRoskar avatar May 20 '23 16:05 dRoskar

I believe the PR has escaped @epoberezkin's attention since this CLI option is already documented at https://ajv.js.org/standalone.html#two-step-process

... or pass the --code-esm (CLI) flag if you want ESM exported code.

but apparently was forgotten to be added.

Perhaps @jessedc can be of help here?

MatthiasKunnen avatar Jun 16 '23 01:06 MatthiasKunnen

It would be great to get this merged for those of us trying to pre-compile validators.

jimkang avatar Aug 07 '23 22:08 jimkang

@epoberezkin Anything new about merging this PR ? Thanks.

damienflament avatar May 18 '24 07:05 damienflament