webpack: webpack v3 mangling exports name will break `Array.from` of core-js

Do you want to request a feature or report a bug?

Bug

What is the current behavior?

(I wanna cry: )The optimization of Scope Hoisting could break core-js Array.from.

After updated to webpack v3. My code will throw the error:

Uncaught TypeError: Cannot assign to read only property 'length' of function 'function from() { [native code] }'

If the current behavior is a bug, please provide the steps to reproduce.

The error due to wrong context:

  • webpack use __webpack_require__.n & __webpack_require__.d to mangle exports name;
  • Array.from transformed to __WEBPACK_IMPORTED_MODULE_4_babel_runtime_core_js_array_from___default.a;
  • While calling arrayfrom.a(arraylike), the context will be function getDefault;
  • es6.array.from.js#L15, C will be getDefault;
  • #L25 new C() will return function from(arrayLike /* , mapfn = undefined, thisArg = undefined */) {...};
  • createProperty will make the returned function property length read only;
  • finally when it call result.length = index, the error Uncaught TypeError will be threw.

What is the expected behavior?

image

Please mention other relevant information such as the browser version, Node.js version, webpack version and Operating System.

webpack v3

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 16
  • Comments: 17 (11 by maintainers)

Commits related to this issue

Most upvoted comments

This ticket is about 3.0.0, no need to corroborate that you have this bug with the same version of webpack in the original issue.

@blade254353074 why did you close this? It’s not fixed AFAIK. 😃

The problem is a size optimization that webpack does by default by not correctly setting the this when calling an import directly as a function.

Enable strictThisContextOnImports to fix this, as mentioned in https://github.com/webpack/webpack/issues/5135#issuecomment-310331312. It will change your invocation from arrayfrom.a(arraylike) to __webpack__require__.i(arrayfrom.a)(arraylike), so that it will correctly have no context.

babel does the same without using a helper function, though (the babel equivalent would be (0, arrayfrom.a)(arraylike)); maybe webpack could do that too.

It’s definitely not a core-js bug.

On Jun 30, 2017 4:59 AM, “Mateusz Burzyński” notifications@github.com wrote:

Aint sure where this should be fixed, but I’ve proposed a quick fix in the core-js. Maybe @zloirock https://github.com/zloirock will have some insight what should be done to fix it properly.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/webpack/webpack/issues/5135#issuecomment-312227673, or mute the thread https://github.com/notifications/unsubscribe-auth/ABJFPypSDiFEGcZsNq7xcbGimtLhbrWVks5sJMbhgaJpZM4OB8nB .

My 2 cents. I experienced this issue when trying to set up ModuleConcatenationPlugin. For this plugin to work I had to tell babel to not parse modules:

{
  "presets": [["es2015", { "modules": false }], "stage-0", "react"],
  ...
}

It’s exactly that setting that start throwing errors, whether I use ModuleConcatenationPlugin or not.

No, this is the canonical issue and should be reopened. I was chastising the other commenter for saying they had this issue with 3.0.0, because that’s the same version the ticket is about anyway - saying you also have this issue with 3.0.0 is not new information.

On Jun 27, 2017 5:50 AM, “Sebastian Blade” notifications@github.com wrote:

@asmundg https://github.com/asmundg Honestly, I’m not sure what’s @starml mean…

This ticket is about 3.0.0, no need to corroborate that you have this bug with the same version of webpack in the original issue.

I thought he was saying that this issue is duplicated…

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/webpack/webpack/issues/5135#issuecomment-311323122, or mute the thread https://github.com/notifications/unsubscribe-auth/ABJFP6te1EguvO2XDPY-QWkHB_bTOlLaks5sIN5-gaJpZM4OB8nB .