Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add polyfill to Angular library

Is there a way within an Angular CLI project to have a library provide a polyfill? Within angular.json, the main app is by default configured with "polyfills": "src/polyfills.ts". This option is not available for a library project.

My specific use case is that my library has a dependency that requires a polyfill. Without the ability of the library automatically providing the polyfill upon it being imported in an application, I need to document and inform the library user of adding the polyfill themselves.

like image 294
Sam Herrmann Avatar asked Sep 13 '18 17:09

Sam Herrmann


People also ask

What is polyfill file in angular?

Polyfills in angular are few lines of code which make your application compatible for different browsers. The code we write is mostly in ES6(New Features: Overview and Comparison) and is not compatible with IE or firefox and needs some environment setups before being able to be viewed or used in these browsers.

Why do we use Polyfills in angular?

Polyfillslink Angular is built on the latest standards of the web platform. Targeting such a wide range of browsers is challenging because they do not support all features of modern browsers. You compensate by loading polyfill scripts ("polyfills") for the browsers that you must support.


1 Answers

This capability is not available in the CLI and it will not become available for good reasons. The following is A. Agius' comment on the GitHub issue that I had created to enquire about this feature:

IMHO, a library should never bundle a polyfill. It’s up to the consumers of the library based on their targeted browsers to include a polyfill or not.

If I want to target only ever green browsers I certainly don’t want that a library that I am using has an embedded polyfill.

As an application develop, I don’t want to end up having duplicate polyfills for the same thing, because it was already shipped in library that I am using.

like image 181
Sam Herrmann Avatar answered Sep 19 '22 07:09

Sam Herrmann