Search code examples
reactjspolyfills

old react and IE11 polyfills


Im running React 0.13 and cant upgrade (long story). Cant use Babel-Polyfill. I need to make the application IE11 compatable so I need to implement a lot of polyfills.

How do I or can I include the polyfills in a singular file and make that file globally visible?

I have a file containing just the polyfills ( like this one )

But im unsure how to include it to just be visible globally by default.

if (typeof Object.assign != 'function') {
    Object.assign = function(target) {
        'use strict';
        if (target == null) {
            throw new TypeError('Cannot convert undefined or null to object');
        }

        target = Object(target);
        for (var index = 1; index < arguments.length; index++) {
            var source = arguments[index];
            if (source != null) {
                for (var key in source) {
                    if (Object.prototype.hasOwnProperty.call(source, key)) {
                        target[key] = source[key];
                    }
                }
            }
        }
        return target;
    };
}

Solution

  • you can implement a function that wraps all the polyfills and import it wherever you need it.

    export function loadCustomPolyfills() {
    
        if (typeof Object.assign != 'function') {
            Object.assign = function(target) {
                ..
            };
        }
    
        //other polyfills
    
    }
    

    and in you entry point file, at the beginning

    import { loadCustomPolyfills } from './custom-polyfills';
    loadCustomPolyfills();