-
-
Notifications
You must be signed in to change notification settings - Fork 9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Excessing object wrapping. #5600
Comments
The reason is to call the function with the correct this context: import x from "module";
x(foo); // <- called without this (undefined/root) var __WEBPACK_IMPORTED_MODULE_1__module__ = __webpack_require__(1);
__WEBPACK_IMPORTED_MODULE_1__module__.default(foo);
// ^ called with this = __WEBPACK_IMPORTED_MODULE_1__module__
Object(__WEBPACK_IMPORTED_MODULE_1__module__.default)(foo);
// ^ called without this (undefined/root) The Object() is generated when calling a imported symbol directly. I omitted this in an older version, assuming nobody care about Most of these are optimized away with the ModuleConcatenationPlugin. A bit of history of this: (This started 1.5 years ago)
Here are up-to-date results:
Note:
|
@sokra very interesting! Thanks for the explanation. Maybe it's a stupid question, but I'll ask anyway primarily out of curiosity: why not use |
Maybe I'm missing something, but why not just const foo = o.foo;
foo(); instead of Object(o.foo)(); |
Our Parser is not so clever to track what's before the identifier. @bmeurer Caching the export is not always correct as these are live bindings which can change anytime. |
If comma version is one of the fastest and is not bloating the generated code so much, why wasnt just |
@sokra I see, thanks for the clarification. Will land the Wrote down some details here. |
When calling Object(value) where the value is known to be a JSReceiver, we can just replace it with value, as the Object constructor call is a no-op in that case. Otherwise when value is known to be not null or undefined then we can replace the Object constructor call with an invocation of ToObject. This covers the common pattern found in bundles generated by Webpack, where the Object constructor is used to call imported functions, i.e. Object(module.foo)(1, 2, 3) There's a lot of detail in webpack/webpack#5600 on this matter and why this pattern was chosen. Bug: v8:6772 Change-Id: I2b4f0b4542b68b97b337ce571d6d79946c73d8bb Reviewed-on: https://chromium-review.googlesource.com/643868 Reviewed-by: Yang Guo <yangguo@chromium.org> Commit-Queue: Benedikt Meurer <bmeurer@chromium.org> Cr-Commit-Position: refs/heads/master@{#47728}
Great to see. If you want to you could improve the error messages for this cases a bit:
This would help people debugging undefined/null functions. |
i. e.
|
I personally don't care so much about performance. But..it it's a pretty rough hit on readability. I would totally trade clarity for correctness during all development. Would you be able to provide a flag to turn it off? |
If anyone's wondering, you can disable this in webpack 4 with the config: {
optimization: {
concatenateModules: true
}
} Side note: |
I noticed that Update: Narrowed it down that when
|
This issue had no activity for at least half a year. It's subject to automatic issue closing if there is no activity in the next 15 days. |
Issue was closed because of inactivity. If you think this is still a valid issue, please file a new issue with additional information. |
I'm noticing with webpack v3 there's a lot of this kind of generated code:
What's the reason for wrapping all references with
Object()
and can this be disabled or optimized away to a single wrap?The text was updated successfully, but these errors were encountered: