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

Bug: static class members defined via an IIFE cause the class to not be removed #4116

Open
bradzacher opened this issue Sep 7, 2023 · 1 comment

Comments

@bradzacher
Copy link

bradzacher commented Sep 7, 2023

Input

function getFoo() {
  class Foo {
  }
  (() => {
    Foo.x = 1;
  })();
  return Foo;
}
const Foo1 = getFoo();

class Bar {
  constructor(num) {
    this.num = num;
  }

  n() {
    console.log(this.num);
  }

  neverCalled() {
    console.log(Foo1);
  }
}

new Bar(5).n();

Expected output

function b() {
  this.g = 5;
}
b.prototype.n = function() {
  console.log(this.g);
};
(new b()).n();

Actual output

(function() {
  function a() {
  }
  a.x = 1;
  return a;
})();
function b() {
  this.g = 5;
}
b.prototype.n = function() {
  console.log(this.g);
};
(new b()).n();

closure compiler playground

From the looks of it, CC's side-effect detection doesn't correctly understand the IIFE and CC decides the enclosing scope is impure. This in turn means that CC won't remove the unused code paths.

We ran into this because this is the code that swc generates for static class members. I'm trying to migrate our codebase from tsc to swc for transpilation and we noticed a bundle size increase. A lot of investigation later we eventually narrowed it down to this problem.

cc @WearyMonkey

@lauraharker
Copy link
Contributor

We are not able to prioritize this right now, but probably should do a better job handling IIFEs and appreciate the report.

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

No branches or pull requests

2 participants