-
-
Notifications
You must be signed in to change notification settings - Fork 66
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
CLI compile - problem with CommonJS imports/exports and nonexistent require() paths #98
Comments
What is the use case not to support CommonJS syntax? It is the default in node.js and in general supported wider than import/export - in other words, is there a [widely adopted] build system that supports import/export but does not support CommonJS?
See my comments here: ajv-validator/ajv#1376 . There are places/users for whom it is working (e.g. see gajus/table), it is either a problem of configuration or compatibility with a particular build system. Please show the minimal setup to reproduce the actual problem.
It has a dependency only on some specific small files bundled with Ajv, the whole of Ajv code will not be executed (or even included in the browser bundle), so standalone code is independent of at least 90% of Ajv source (if not more). There is an argument for deploying these components separately, but the advantages are lower than the costs of maintaining/versioning/deploying/etc of some sort of "ajv runtime" package. You still need full Ajv to compile schemas to code, so it won't improve development experience or build time (on the opposite). Also, including it directly in code has performance / code-size disadvantages - they would be repeated in each module you generate. Your bundler does exactly that already - replacing "requires" with the actual code, in the most efficient way, so there is no much value in Ajv doing it. |
Hello @epoberezkin . Thank you for a very quick and detailed answer. Much appreciated! Point 2: You're right - I created a new isolated project and it's not missing there. Looks like some issue on my side, which I'm investigating. Point 3: I think you're right with the the cost vs benefit argument. It can be achieved with bundlers if desired. Point 1 though: It's supported by Node since version 12 as experimental feature. |
Thanks for the update - maybe something should be added in docs (standalone.md) or at least explained in the issues... Re point 1 - I don't mind generating different "exports" for the functions themselves in "standalone" code, but there is no point doing it without changing "imports" as well in generated code - and it would lead to function failing to instantiate (even ignoring the fact that currently the imports are made just in front of the functions that use them - but it is probably ok to convert assignments to imports without moving them to the top). It's tricky because the same code is instantiated as closures (and it has to happen even if you need it only as standalone - ajv is not a pure offline compiler first - it generates closures first to have efficient runtime compilation and then uses these closures to generate standalone code)... Maybe the solution is to have these particular files in dist in both .js and .mjs format (some tooling will be needed to compile just these files to .mjs) so that the runtime compilation succeeds in node starting from 15 and then a single option like `code: {esm: true}} would change both exports in standalone code and imports in generated code. Or maybe a possible approach is just to use some code-processing from third party that would convert CJS to ESM format? Anyway, this is best submitted / discussed / planned as a separate issue in ajv repo. |
It's probably worth adding this clarification in standalone.md about what is the dependency of standalone code on the small parts of Ajv |
Hello!
In our project, we're using a typescript monorepo setup with a shared model.
The model interfaces are annotated with YousefED/typescript-json-schema to produce json schemas in build time. These schemas are later used on the API level, as well as complementary validation of the forms in an Angular frontend, using AJV. So far, so good.
We ran into a problem with Content Security Policy and unsafe-eval.
As a remedy, I'm trying to use AJV CLI to precompile validator functions. That, however, leads to a couple issues. It is generating some code, but there are caveats.
var func8 = require("ajv/dist/compile/ucs2length").default;
In reality, there's no such file under ajv/dist/, but there's one under ajv/lib/
The text was updated successfully, but these errors were encountered: