Theo
Theo

Reputation: 473

Angular 2 non-cli add polyfills.ts

I have an angular 2 app, I didn't use angular-cli to generate it. I am new to Angular and I am trying to make my app work with IE, old Safari, etc...

I read a lot about the polyfills.ts, how can I add this to my angular 2 app (I am using systemjs to load the app)

Here is the scripts I am currently loading :

<script src = "node_modules/core-js/client/shim.min.js"></script>
<script src = "node_modules/zone.js/dist/zone.js"></script>
<script src = "node_modules/reflect-metadata/Reflect.js" ></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>

And the systemjs.config :

(function (global) {
  System.config({
    paths: {
      // paths serve as alias
      'npm:': '/node_modules/'
    },
    // map tells the System loader where to look for things
    map: {
      // our app is within the app folder
       app: '/js/angular/eventticketspage',

      // angular bundles
      '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
      '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
      '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
      '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
      '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
      '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
      '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
      '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',

      // other libraries
      'rxjs': 'npm:rxjs',
      'core-js': 'npm:core-js'
    },
    // packages tells the System loader how to load when no filename and/or no extension
    packages: {
        app: {
            main: './main.js',
            defaultExtension: 'js'
        },
        rxjs: {
            defaultExtension: 'js'
        },
        'core-js': {
            defaultExtension: 'js'
        }
    }
  });
})(this);

Edit: I am able to resolve the 'intl' is undefined by adding the script:

    <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=Intl.~locale.en"></script>

<script src="//cdnjs.cloudflare.com/ajax/libs/classlist/2014.01.31/classList.min.js"></script>

But I want to do it the right way and using Polyfills.ts

Upvotes: 2

Views: 3191

Answers (1)

user917170
user917170

Reputation: 1641

It looks like you just have to do it manually. There is no magic. I have just copied the polyfills.ts from https://github.com/angular/angular/blob/master/aio/src/polyfills.ts and included it in my source code folder. I have un commented the lines for IE but left the zone and reflect lines commented out. Then in my app I have

import "../shared/pollyfills" // I have put pollyfills.ts in a shared folder
import "zone.js";
import "reflect-metadata";

which imports the pollyfills.ts file, which in turn imports the core-js/es6 packages.

I am using webpack as a module bundler though, so i am not sure how system.js works - but the principal is the same, copy the pollyfills file over and import it like any other module. Oh and of course in your package.json you need

"core-js": "^2.4.1",

or similar.

Upvotes: 1

Related Questions