JavaScript Scope Hoisting is Broken

Back
This is an AI-generated summary. for details.

The creator of Parcel argues that scope hoisting (bundling modules into a shared top-level scope) has become more problematic than beneficial in real-world applications. While originally beneficial for reducing bundle size and improving performance, this method clashes with modern JavaScript features like code splitting and dynamic imports, often resulting in incorrect execution order and subtle bugs. He highlights examples where side effects run out of sequence, and this binding breaks when modules shift from function wrappers to shared scope.

Given these issues and the limited gains—since most modules still require wrapping—Govett proposes simplifying Parcel by removing scope hoisting in v3, defaulting to function-wrapped modules to ensure correctness. He notes that this change won’t affect optimizations like tree-shaking, dead-code elimination, or constant folding. The post concludes with a call for feedback and a promise to share results from real-world testing under the new approach.

Read the full article