Skip to content

Make all the polyfills available without global namespace pollution #8928

@millsp

Description

@millsp

Feature Request

This is a draft proposal that aims to make all the polyfills available without global namespace pollution.

Is your feature request related to a problem? Please describe.

Make all the polyfills available on the runtime to make development easier. Development of libraries is possible but some of de features (polyfills) are not available when using babel-runtime. These are the instance methods (polyfills) like .includes(), .repeat()... that can pollute our global scope.

Describe the solution you'd like

Include all the missing instance methods like (.includes, .repeat ...) and without polluting the global scope.
To to so, babel would have to determine the type of the instance at runtime to transfer the call to a static one.

 console.log(['a', 'b', 'c'].includes('a'));

would be transformed to

var _arrayIncludes = require("core-js-pure/features/array/includes");
var _stringIncludes = require("core-js-pure/features/string/includes");

// Here is decided what built-in method to call based on instance type
var _polyfillsCalls = (instance, methodName, methodParams) => {
     // ... Use methodName to determine that it can only be 'String' or 'Array' (because of includes)
     // This feature has to be generic and return the maybe-type of the instance calling the method
     // This can be possibly done by mapping function names to their possible instance types
     // For example .includes() calls can be mapped to types => ['string', 'Array'] !

     // Then we proceed with the execution with type detection #map

     if (Array.isArray(instance))
          return _arrayIncludes(instance, ...methodParams);
     
     if (typeof instance === 'string')
          return _stringIncludes(instance, ...methodParams);

     // No polyfill could be mached, default call
     return instance[methodName](methodParams);
};

console.log(_polyfillsCalls(['a', 'b', 'c'], 'includes', ['a']);

For this, core-js features could be used directly. Not sure that all polyfills can be covered this way, but it should cover most of them...

Metadata

Metadata

Assignees

No one assigned

    Labels

    outdatedA closed issue/PR that is archived due to age. Recommended to make a new issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions