Skip to content

Object.assign Polyfill (For Wechat) #453

@hayeah

Description

@hayeah

WeChat is a popular chat app in China. On Android it embeds Tecent's custom Webkit browser "X5". This browser requires polyfill for Object.assign.

Maybe it would be nice to have a more declarative way to add polyfills. Right now I used next.conig.js to modify the main.js entry:

// next.config.js
module.exports = {
  cdn: false,
  webpack: (cfg, { dev }) => {
    // prepend polyfill
    const main = cfg.entry["main.js"]
    cfg.entry["main.js"] = [require.resolve(__dirname + "/polyfill"), main]
    // console.log("entries", cfg.entry)

    return cfg
  }
}

Where polyfill.js is the following (note that the feature detection has to use string concat to prevent babel from transforming it to its own assign):

if (typeof Object["ass"+"ign"] != 'function') {
  Object.assign = function (target, varArgs) { // .length of function is 2
    'use strict';
    if (target == null) { // TypeError if undefined or null
      throw new TypeError('Cannot convert undefined or null to object');
    }

    var to = Object(target);

    for (var index = 1; index < arguments.length; index++) {
      var nextSource = arguments[index];

      if (nextSource != null) { // Skip over if undefined or null
        for (var nextKey in nextSource) {
          // Avoid bugs when hasOwnProperty is shadowed
          if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
            to[nextKey] = nextSource[nextKey];
          }
        }
      }
    }
    return to;
  };
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions