How do I modularize polyfills in Angular?

Josh Beam

I have various polyfills/shims I would like to use in my AngularJS app.

For example, classList is not supported in IE9 and below.

I've tried two ways of defining polyfills in my app:

First, by simply placing all polyfills at the top of my app.js page, wherein all other modules are defined and configured.

Second, I've tried this:

angular.module('polyfills',[]);
angular.module('polyfills')
    .config([function() {
        // all polyfills in here
    }]);

Both options work. However, both options seem a little weird to me. Intuitively, using .config feels like I should be sticking to configuring things like providers, interceptors, etc. It seems like this might be an issue that has already been resolved in the past, and I have the feeling some sort of best practice has come about as a result.

Is there a better way of doing this?

codepuzzler

Don't put your polyfills in an angular module, that's weird and not what you want. What you want is for the js implementation in each browser to look the same, so load your polyfills before any other js resource. Create a js file (or more if you want each polyfill in its own file) that contains your polyfill(s) and is loaded in a script tag before any of your other js files. Your polyfills should also be inside IIFE's.

Here's what an example polyfill for Array.from (this is a very simple polyfill for demo purposes):

array-from-polyfill.js

(function() {
  'use strict';
  Object.defineProperty(Array, 'from', {
    configurable: false,
    enumerable: false,
    value: function (object) {
        'use strict';
        return [].slice.call(object);
    }
    });
})();

Then put the following tag in your index.html (or whatever you call your top level template) before any other js files. Yes, even before angular or bootstrap resources.

<script src="path/to/array-from-polyfill.js"></script>

Two last items of note. First, always use Object.defineProperty, and set 'enumerable' to false. That is unless you want certain ways of iterating over an object's properties to pick up your polyfills. This means that you'll have to fiddle with MDN's polyfills, but that's fine. You should probably know your way around defineProperty anyways.

Second, I suggest that you load your polyfills in every browser, even if the browser already has that function. Basically, your executed code should be the same everywhere, as much as possible. Nothing is more irritating that having a bug in one specific browser, because it is (or is not as the case may be) using your polyfill. This isn't a universal truth, so do what feels right to you.

Don't forget to make sure that your polyfills are also loaded in your testing environment, and to regularly check if you can retire a polyfill.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

How do I count selected checkboxes in Angular?

来自分类Dev

Angular4中polyfills的含义是什么?

来自分类Dev

How do I reload an Angular 6 component when a different component (that is not the parent/child) says to do so

来自分类Dev

How do I dynamically inject an Angular2 sub component via typescript code?

来自分类Dev

How Do I Structure Multiple Select Options Over an Array of Objects in Angular

来自分类Dev

Angular 版本 4- 在 IE 11 上运行,无法找到 polyfills.ts

来自分类Dev

现在将非beta Angular 2打包为@angular时,angular2-polyfills在哪里?

来自分类常见问题

How do I resolve ClassNotFoundException?

来自分类Dev

How do i sort my listview alphabetically?

来自分类Dev

How do I parse a RestSharp response into a class?

来自分类Dev

How do I override a default keybinding in LightTable?

来自分类Dev

How do I declare a driver as global?

来自分类Dev

How do I combine lenses and functors?

来自分类Dev

How do I parse JSON in Racket?

来自分类Dev

How do I link to a pdf in AngularJS?

来自分类Dev

How do I include jquery in javafx webview?

来自分类Dev

How do I retrieve the text of a cell comment

来自分类Dev

How do I write a custom module for AngularJS?

来自分类Dev

I do not understand how execlp() works in Linux

来自分类Dev

How do I iterate over a file?

来自分类Dev

How do I define the size of a CollectionView on rotate

来自分类Dev

How do I combine three observables such that

来自分类Dev

Basic Ocaml: How do I compile this?

来自分类Dev

How do I free socket descriptors in RabbitMQ

来自分类Dev

How do I convert synchronous ajax to asynchronous?

来自分类Dev

puppet - How do I append to path variable?

来自分类Dev

How do I link boost to my program?

来自分类Dev

Passing File with intent, how do i retrieve it

来自分类Dev

How do I call an intent in Retrofit Callback?

Related 相关文章

  1. 1

    How do I count selected checkboxes in Angular?

  2. 2

    Angular4中polyfills的含义是什么?

  3. 3

    How do I reload an Angular 6 component when a different component (that is not the parent/child) says to do so

  4. 4

    How do I dynamically inject an Angular2 sub component via typescript code?

  5. 5

    How Do I Structure Multiple Select Options Over an Array of Objects in Angular

  6. 6

    Angular 版本 4- 在 IE 11 上运行,无法找到 polyfills.ts

  7. 7

    现在将非beta Angular 2打包为@angular时,angular2-polyfills在哪里?

  8. 8

    How do I resolve ClassNotFoundException?

  9. 9

    How do i sort my listview alphabetically?

  10. 10

    How do I parse a RestSharp response into a class?

  11. 11

    How do I override a default keybinding in LightTable?

  12. 12

    How do I declare a driver as global?

  13. 13

    How do I combine lenses and functors?

  14. 14

    How do I parse JSON in Racket?

  15. 15

    How do I link to a pdf in AngularJS?

  16. 16

    How do I include jquery in javafx webview?

  17. 17

    How do I retrieve the text of a cell comment

  18. 18

    How do I write a custom module for AngularJS?

  19. 19

    I do not understand how execlp() works in Linux

  20. 20

    How do I iterate over a file?

  21. 21

    How do I define the size of a CollectionView on rotate

  22. 22

    How do I combine three observables such that

  23. 23

    Basic Ocaml: How do I compile this?

  24. 24

    How do I free socket descriptors in RabbitMQ

  25. 25

    How do I convert synchronous ajax to asynchronous?

  26. 26

    puppet - How do I append to path variable?

  27. 27

    How do I link boost to my program?

  28. 28

    Passing File with intent, how do i retrieve it

  29. 29

    How do I call an intent in Retrofit Callback?

热门标签

归档