vercel / next.js

The React Framework
https://nextjs.org
MIT License
126.64k stars 26.93k forks source link

Object.assign Polyfill (For Wechat) #453

Closed hayeah closed 7 years ago

hayeah commented 7 years ago

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 commented 7 years ago

@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.

xdamman commented 7 years ago

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 commented 7 years ago

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

xdamman commented 7 years ago

@arunoda here: https://github.com/zeit/next.js/issues/1506