Search code examples
angularivy

Why shouldn't I publish libraries compiled with Ivy?


I have a toolkit-library, which we use in our company. Currently, we are migrating all our projects to angular 11. So naturally, we updated our toolkit lib to Angular 11 as well.

As of ng11, Ivy is the default engine, I thought turning it on would be a good idea, so our applications won't need to compile it with ngcc. So I set this in our package.json:

  "angularCompilerOptions": { "enableIvy": true },

But as I tried to push it, I saw the error:

npm ERR! [email protected] prepublishOnly: node --eval "console.error('ERROR: Trying to publish a package that has been compiled by Ivy. This is not allowed.\nPlease delete and rebuild the package, without compiling with Ivy, before attempting to publish.\n')" && exit 1

it seems that this error is generated into the dist compiled code and prevents me from pushing to our nexus if I compile with Ivy.

Found this report and with official answers here:

https://github.com/angular/angular/issues/37973

My question is: Why is that? Why is it not recommended to publish it with Ivy? This way lib is compiled with ViewEngine and then installed (in our angular 11 projects) as a dependency only to be pre-compiled with ngcc to be compatible with Ivy, which seems like a bad practice to me.

I feel like I am misunderstanding something here, can somebody explain this to me please?


Solution

  • As stated in this answer on that github link

    The reason is actually forward compatibility. If we allow ivy compiled libraries to be published right now, then we lock ourselves into a very specific use of the ivy instruction set and data structures. This would affect us from being able to make implementation changes that would fix bugs, improvement performance, reduce code size etc. By keeping the ivy internals private we can be more agile going forward.

    The current way to achieve this is by requiring ViewEngine compiled code (since this leaves the decorator information in place), which ngcc is able to convert to ivy code at library installation/ application build time. Going forward we are looking into a partially compiled library format that will have the best of both worlds: not requiring a lengthy ngcc build step, but also not exposing the internals of the ivy runtime.