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

Script to generate bundle including subset of keywords #460

Closed
seeden opened this issue Apr 12, 2017 · 11 comments
Closed

Script to generate bundle including subset of keywords #460

seeden opened this issue Apr 12, 2017 · 11 comments
Labels

Comments

@seeden
Copy link

seeden commented Apr 12, 2017

Hi @epoberezkin

I really like ajv. Version 5 is great because I have no problem with webpack as well.
Currently I am trying to reduce bundle size of my projects and ajv took almost 196kb / 35kb minified gziped version.
Did you think about spliting your package? or do it more modular with ES6?
Currently I am using just very simple funcionality from ajv like required properties and min max values.

Thank you for your time

@epoberezkin
Copy link
Member

@seeden I'm not sure how Ajv can be split... Theoretically keywords can be plugins and optional, but I'm not sure I like this idea. In any case it is not related to using ES6.

For your application a better idea could be to precompile your schemas into modules using ajv-cli (that uses ajv-pack inside). It may result in a smaller bundle size (depending on the number and the size of schemas) as only a small portion of Ajv will be included in the compiled code.

@seeden
Copy link
Author

seeden commented Apr 12, 2017

@epoberezkin I am generating schemas on demand because they are dynamic

@epoberezkin
Copy link
Member

epoberezkin commented Apr 12, 2017

You can also try using some build tools that remove unused code - e.g. if there are some Ajv methods/functions you don't use they can be removed. It requires bundling Ajv together with your app, but it's easier than managing manually what should and shouldn't be included.

@epoberezkin
Copy link
Member

@seeden
Copy link
Author

seeden commented Apr 12, 2017

I am using webpack 2 (modules false) with uglify as well. But as I told the final size is 196kb / 35kb minified gziped version.

@epoberezkin
Copy link
Member

epoberezkin commented Apr 12, 2017

196kb not minified? If so, you already remove 25%.
I can suggest forking and removing unused keywords from these two files:
https://github.com/epoberezkin/ajv/blob/master/lib/compile/_rules.js
https://github.com/epoberezkin/ajv/blob/master/lib/compile/rules.js

That should reduce the bundle substantially.
You can also contribute a script that automates generating bundle including only certain keywords (e.g. by updating and then restoring these files after the bundle is created).

@epoberezkin epoberezkin changed the title reduce final size Script to generate bundle including subset of keywords Apr 12, 2017
@jonathansp
Copy link

jonathansp commented Jan 24, 2018

Hi @epoberezkin @seeden I believe we could have the compile module imported optionally since it's not necessary and by removing it the bundle's size would be reduced substantially.

@epoberezkin
Copy link
Member

Can you please explain?

@jonathansp
Copy link

My first thought was to remove ajv.compile function, but after going over the code I realized it is quite complex so it won't happen. The final bundle size is an issue sometimes. Our project has 5kb + 111kb from ajv.

@epoberezkin
Copy link
Member

you can precompile your schemas with ajv-cli - the code can be smaller (unless you have large schemas, in which case it can become bigger :), as it will only include used run time parts of ajv and not the compilation logic.

@epoberezkin
Copy link
Member

This is now possible in v7 using AjvCore (import AjvCore from "ajv/dist/code") that does not import any keywords, so only the needed keywords can be added.

The size reduction is not as substantial as may be expected - Ajv without any keywords is approximately 35% smaller than Ajv with all draft-2019-09 keywords, and 30% smaller than with draft-7 keywords - the big part is the core logic that is required for all keywords.

A bigger potential to reduce the size could be to ship with compiled schemas (PR #1332 - in progress), but it depends on the number of schemas - a compiled validation code is bigger than the schema itself - so you have to do this estimation for your application.

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

No branches or pull requests

3 participants