Howto: Setup Polyfills in Angular

Angular CLI based projects support polyfills. Here are the steps required to set them up on a newly created project which does not contain them so far:

  1. Create src/polyfills.ts file (file name can be choosen freely)

  2. Include the file in the tsconfig.app.ts and tsconfig.spec.ts configs, e.g.

    1{
    2    "compilerOptions": {
    3    },
    4    "files": [
    5        "src/polyfills.ts",
    6        "src/main.ts"
    7    ],
    8}
    
  3. Make Angular include the file as a ployfill by adding it to the polyfills section in the angular.json file, e.g.

     1{
     2   "projects": {
     3       "architect": {
     4           "build": {
     5               "options": {
     6                   "polyfills": [
     7                       "src/polyfills.ts",
     8                       "zone.js"
     9                   ],
    10               }
    11           },
    12           "test": {
    13               "options": {
    14                   "polyfills": [
    15                       "src/polyfills.ts",
    16                       "zone.js",
    17                       "zone.js/testing"
    18                   ],
    19               }
    20           }
    21       }
    22   }
    23}
    

Those changes did the trick for me. Any code located in the polyfills.ts file was included in the transpiled code and added prior to any other application code.