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

babel-plugin-minify-dead-code-elimination is too eager on jquery.js #691

Closed
lydell opened this issue Sep 19, 2017 · 4 comments
Closed

babel-plugin-minify-dead-code-elimination is too eager on jquery.js #691

lydell opened this issue Sep 19, 2017 · 4 comments
Labels
bug Confirmed bug

Comments

@lydell
Copy link

lydell commented Sep 19, 2017

Input

function test(props) {
  var isBox = "width" in props;

  for (prop in props) {
    delete props[prop];
  }
  // This also triggers the bug:
  // delete props.width;

  if (isBox) {
    console.log("hello");
  }
}

Output

function test(props) {

  for (prop in props) {
    delete props[prop];
  }
  // This also triggers the bug:
  // delete props.width;

  if ("width" in props) {
    console.log("hello");
  }
}

Problem

The minifier inlines the isBox variable, which moves the "width" in props expression. However, at that point the "width" property has been removed from props, effectively making "width" in props always be false, so that console.log("hello") never runs.

Here's how I tested: https://github.com/lydell/babel-plugin-minify-dead-code-elimination-bug/blob/master/minimal.js

jQuery!

This bugs affects jQuery!

A minimal repo with test cases can be found here: https://github.com/lydell/babel-plugin-minify-dead-code-elimination-bug

What happens is that after I’ve run babel-plugin-minify-dead-code-elimination on jquery.js (node_modules/jquery/dist/jquery.js), the .slideToggle() method no longer behaves correctly: It does animate the height of elements, but fails to apply overflow: hidden; while animating, making text inside elements overflow.

Info

Here’s exactly how I run babel-plugin-minify-dead-code-elimination: https://github.com/lydell/babel-plugin-minify-dead-code-elimination-bug/blob/master/minify.js

Here’s the diff between input and output of jquery.js, so you can see what babel-plugin-minify-dead-code-elimination has done: https://github.com/lydell/babel-plugin-minify-dead-code-elimination-bug/blob/master/jquery.diff

I've also manually reverted changes made by the minifier until only the breaking change was left. Here's the diff for that: https://github.com/lydell/babel-plugin-minify-dead-code-elimination-bug/blob/master/jquery-edit.diff

In the production app where I discovered this, I use babel-minify-webpack-plugin and new MinifyPlugin({deadcode: false}) is a workaround.

Expected behavior (before minify)

Click to show gif

Actual behavior (after minify)

Click to show gif
@boopathi
Copy link
Member

Were you able to find out which transformation in the diff is causing this bug?

@lydell
Copy link
Author

lydell commented Sep 19, 2017

No, not yet. But I’ll try to look into that later today.

@lydell
Copy link
Author

lydell commented Sep 19, 2017

@boopathi I've managed to reduce the diff to only the relevant part now: https://github.com/lydell/babel-plugin-minify-dead-code-elimination-bug/blob/master/jquery.diff

I've also updated the issue description with a minimal test case.

@boopathi boopathi added the bug Confirmed bug label Sep 19, 2017
@lydell
Copy link
Author

lydell commented Oct 4, 2017

Thanks for fixing this! 💯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Confirmed bug
Projects
None yet
Development

No branches or pull requests

2 participants