Skip to content
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

Fix hoisting of "var" variables on wrapped modules #1735

Closed
wants to merge 3 commits into from

Conversation

fathyb
Copy link
Contributor

@fathyb fathyb commented Jul 15, 2018

Part of the fixes for #1699

Fix issues with some React apps. One of the problematic files (loggedTypeFailures) :
https://github.com/facebook/prop-types/blob/cfc7f43af3d74978ec82052fe6bb8212f09daca0/checkPropTypes.js

@@ -135,6 +135,36 @@ module.exports = {
) {
scope.push({id: exportsIdentifier, init: t.objectExpression([])});
}

// Move all "var" variables to the top-level to prevent out of order definitions when wrapped.
for (let name in scope.bindings) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm yeah we do something like this here already: https://github.com/parcel-bundler/parcel/blob/master/src/packagers/JSConcatPackager.js#L282-L319 but it wouldn't get variables inside an if statement like that.

Is there a performance hit to doing this on every module? Should we only do it on wrapped modules (in the packager)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a performance hit to doing this on every module?

I did not found any noticeable change on build time on a medium size app, same for vue-hackernews. We only do it when a var variable is not in the root block of the current scope, which is relatively rare.

Should we only do it on wrapped modules (in the packager)?

I guess you could either :

  • build a scope using babel-traverse and do the same as here => slow
  • do a deep AST traversal => slow and error prone

Just found out other some issues with this PR, and I think I should also do it when path.getData('shouldWrap') == true, I'm putting it in WIP.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we don't need to do it when shouldWrap is true, ready to merge!

@fathyb fathyb changed the title Fix hoisting of "var" variables on wrapped modules [WIP] Fix hoisting of "var" variables on wrapped modules Jul 18, 2018
@fathyb fathyb added the 📝 WIP Work In Progress label Jul 18, 2018
@fathyb fathyb changed the title [WIP] Fix hoisting of "var" variables on wrapped modules Fix hoisting of "var" variables on wrapped modules Jul 21, 2018
@fathyb fathyb removed the 📝 WIP Work In Progress label Jul 21, 2018
@fathyb
Copy link
Contributor Author

fathyb commented Sep 25, 2018

This breaks React, closing it.

@fathyb fathyb closed this Sep 25, 2018
@fathyb fathyb deleted the fix/hoist-vars branch September 25, 2018 16:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants