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 befunction getDefault
; - es6.array.from.js#L15,
C
will begetDefault
; - #L25
new C()
will returnfunction from(arrayLike /* , mapfn = undefined, thisArg = undefined */) {...}
; createProperty
will make the returned function propertylength
read only;- finally when it call
result.length = index
, the errorUncaught TypeError
will be threw.
What is the expected behavior?
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
- Wrapped fn Array.from export in a function, so the context of the exported function is not tampered with in webpack3 (will fix webpack/webpack#5135) — committed to Andarist/core-js by Andarist 7 years ago
- Call imports with the correct context. Parse arguments of an import call. Support strictThisContextOnImports in ConcatenatedModules fixes #5246 fixes #5135 fixes #5111 — committed to webpack/webpack by sokra 7 years ago
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 fromarrayfrom.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:
See also https://github.com/webpack/webpack/issues/5111#issuecomment-309900873
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:
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: