paulmillr / es6-shim

ECMAScript 6 compatibility shims for legacy JS engines
http://paulmillr.com
MIT License
3.11k stars 387 forks source link

babel-polyfill and es6-shim #467

Open gnh1201 opened 3 years ago

gnh1201 commented 3 years ago

Related items

Description

To run ES5 or higher Javascript on WSH(Windows Scripting Host), we need a babel-polyfill. for example:

function _extend(dst) {
  for (var _len = arguments.length, sources = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
    sources[_key - 1] = arguments[_key];
  }

  if (dst && sources) {
    var _loop = function _loop(src) {
      if ((typeof src === 'undefined' ? 'undefined' : _typeof(src)) === 'object') {
        Object.getOwnPropertyNames(src).forEach(function (key) {
          dst[key] = src[key];
        });
      }
    };

    var _iteratorNormalCompletion = true;
    var _didIteratorError = false;
    var _iteratorError = undefined;

    try {
      for (var _iterator = sources[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
        var src = _step.value;

        _loop(src);
      }
    } catch (err) {
      _didIteratorError = true;
      _iteratorError = err;
    } finally {
      try {
        if (!_iteratorNormalCompletion && _iterator.RETURN) {  // es5: _iterator.return, babel: _iterator.RETURN
          _iterator.RETURN();
        }
      } finally {
        if (_didIteratorError) {
          throw _iteratorError;
        }
      }
    }
  }

  return dst;
};

This code works well with only babel-polyfill and es5 shim and sham. The problem occurs when adding es6-shim here.

In this case, it should be changed as below to works.

  var defineProperty = function (object, name, value, force) {
    if (!object || (!force && name in object)) { return; }

This change has also been proposed in https://github.com/paulmillr/es6-shim/pull/466.

I think that this change should apply. Or a better idea?

gnh1201 commented 3 years ago

This code works well with only babel-polyfill and es5 shim and sham. The problem occurs when adding es6-shim here.

ljharb commented 3 years ago

Is this still an issue after #466 is merged?

gnh1201 commented 3 years ago

Yes. This seems to be another problem. I'll track it down.

gnh1201 commented 3 years ago

shimtest.js: https://gist.github.com/gnh1201/2627d860e41f8bd2dba858536836c64e

zloirock commented 3 years ago

@gnh1201 why do you need both - core-js and es-shims? It's a bad practice. What does es-shims fix in this case? BTW babel-polyfill is obsolete for a long time ago and the actual version of core-js should be used directly.

gnh1201 commented 3 years ago

@zloirock I had no choice but to choose babel-polyfill and es5-shim. It worked well and successfully executed the JS library file written in ES5. Trying es6-shim was an option for me, but I thought I'd still test it.

ljharb commented 3 years ago

While most of the es-shims should work fine alongside core-js, it's definitely odd to mix them.

I would expect that es5-shim and es6-shim alone could get you mostly up to speed, except for the Symbol shams that core-js includes but es6-shim does not.

gnh1201 commented 2 years ago

I tested core-js and confirmed that the example code works well. But I still have to wait and see. I think I need to check the impact when es-shims is excluded from my project.