5

I setup webpack + babel config

webpack.config.js

...
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        loader: 'babel-loader'
      },
...

.babelrc

{
  "plugins": ["lodash", "transform-object-rest-spread"],
  "presets": [
    ["env", {
      "targets": [
        "> 4%",
        "ie 11",
        "safari 8"
      ]
    }],
    "react",
    "react-optimize"
  ],
  "env": {
    "test": {
      "presets": ["es2015", "react"]
    }
  }
}

In google chrome everything is ok, but in IE 11i have an error

Object doesn't support property or method 'assign'

2 Answers 2

5

You would need to add the object assign transform as well Check https://babeljs.io/docs/plugins/transform-object-assign/

Also notice that it is best practice to include a polyfill instead. The MDN usually gives a polyfill code for ES2015 features. Check https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

For the record, it is:

if (typeof Object.assign != '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;
  };
}

You want to include this code in your app for browsers that do not support Object.assign. The Babel transform plugin referred above also recommends this approach when building an app and not a library.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, when i add babel-plugin-transform-object-assign to plugins section i get an error with promises. I should add babel-plugin-es6-promise and etc. I was think that preset env resolve my problem.
Or install and load babel-polyfill, which is a general polyfill for ES6 stuff.
I was using this but it doesn t seem to be working in our case. I am referrng to babeljs.io/docs/plugins/transform-object-assign When I add it to .babelrc I still see the same error message.
0

It looks like babel compilation doesn't work. Object.assign is ECMAScript 2015 feature and chrome supports it without compilation.

1 Comment

Babel doesn't compile things that aren't syntax. The expectation is that you load babel-polyfill.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.