Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object.assign Polyfill (For Wechat) #453

Closed
hayeah opened this issue Dec 21, 2016 · 4 comments
Closed

Object.assign Polyfill (For Wechat) #453

hayeah opened this issue Dec 21, 2016 · 4 comments

Comments

@hayeah
Copy link

hayeah commented Dec 21, 2016

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;
  };
}
@arunoda
Copy link
Contributor

arunoda commented Dec 21, 2016

@hayeah We already adding the polyfill for that.
(Actually babel)
So it'll work by default and you don't need to do this hack.

@arunoda arunoda closed this as completed Dec 21, 2016
@xdamman
Copy link

xdamman commented Mar 25, 2017

I'm testing with Firefox 28 which doesn't have Object.assign but it doesn't work with latest 2.0.0-beta.39. Here are my presets in .babelrc:

"presets": [
    "es2015",
    "stage-0",
    "next/babel",
    "react"
  ],

am I missing something?

@arunoda
Copy link
Contributor

arunoda commented Mar 25, 2017

@xdamman could you open a new issue and a possible sample app to re-produce this issue.

@xdamman
Copy link

xdamman commented Mar 25, 2017

@arunoda here: #1506

@lock lock bot locked as resolved and limited conversation to collaborators May 11, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants