babel-polyfills icon indicating copy to clipboard operation
babel-polyfills copied to clipboard

`Symbol.iterator` should not load full `Symbol` polyfill

Open zloirock opened this issue 4 years ago • 8 comments

// Now:
arr[Symbol.iterator]();
// =>
import "core-js/modules/es.symbol.iterator.js";
import "core-js/modules/es.array.iterator.js";
import "core-js/modules/es.object.to-string.js";
import "core-js/modules/es.string.iterator.js";
import "core-js/modules/web.dom-collections.iterator.js";
import "core-js/modules/es.symbol.js";
import "core-js/modules/es.symbol.description.js";
arr[Symbol.iterator]();

es.symbol and es.symbol.description should not be loaded only on Symbol.iterator - it's too expensive. core-js architecture should allow to use es.symbol.iterator and iterators without those modules.

zloirock avatar Mar 24 '21 15:03 zloirock

BTW looking at this output - seems after #78 modules sorted in the scope of a feature, but not a file how it should be and if Symbol.iterator would depend on Symbol - it will not work, es.symbol.js should be before the rest modules - it still could cause problems like #77.

zloirock avatar Mar 26 '21 12:03 zloirock

Ok so:

  • We should only inject es.symbol if it's used not as part of Symbol.iterator
  • If es.symbol is injected, it should always come before es.symbol.iterator.

Is this correct?

nicolo-ribaudo avatar Mar 26 '21 15:03 nicolo-ribaudo

Btw, I don't think we can always fix the "implicit optional dependency" between those two polyfills:

// main.js

import "./dep1.js";
import "./dep2.js";
// dep1.js

Symbol.iterator;
// dep2.js

Symbol();

Either we always inject an import to es.symbol in dep1.js, or es.symbol.iterator will be imported before es.symbol.

nicolo-ribaudo avatar Mar 26 '21 15:03 nicolo-ribaudo

@nicolo-ribaudo I mean that es.symbol before es.symbol.iterator in the recommended modules order. It's not related to this issue, it's related #77 and #78. It should work at least on the file's layer - different files are another problem.

zloirock avatar Mar 26 '21 15:03 zloirock

@nicolo-ribaudo Since #192 is closed does it mean that this issue can be closed?

nyngwang avatar Jan 25 '24 08:01 nyngwang

It's another issue.

zloirock avatar Jan 25 '24 10:01 zloirock

Working on this

nicolo-ribaudo avatar Jan 25 '24 13:01 nicolo-ribaudo

@nicolo-ribaudo note that it also requires some other changes, for example, take a look at

  export default function _createForOfIteratorHelper(o, allowArrayLike) {
    var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];

that on typeof Symbol !== "undefined" will also load full symbol polyfill, or regeneratorRuntime:

var $Symbol = typeof Symbol === "function" ? Symbol : {};
var iteratorSymbol = $Symbol.iterator || "@@iterator";
var asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator";
var toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag";

It's an extended version of something like #199 and changes of some helpers.

zloirock avatar Feb 08 '24 04:02 zloirock