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

When the compression is enabled, used variables are dropped. #1526

Open
Sayan751 opened this issue Apr 28, 2024 · 3 comments
Open

When the compression is enabled, used variables are dropped. #1526

Sayan751 opened this issue Apr 28, 2024 · 3 comments
Labels

Comments

@Sayan751
Copy link

Sayan751 commented Apr 28, 2024

Bug report

As the title already says, when the compression is enabled, even some used variables get dropped. Sometimes it is also happens for imported modules when static blocks come into picture.

Version (complete output of terser -V or specific git commit)
Terser REPL

Complete CLI command or minify() options used

NA

terser input

const testVarOne = 'Hello this is test var one';

const testVarTwo = (/* unused pure expression or super */ null && (`
${testVarOne}

Hello this is test var two
`));

const testObj = {
  one: `${testVarOne}`
};

let _initClass;
function fn($class) {
  return [class extends $class {}, function () {}];
}

let _MyElement;
class MyElement {
  static {
    [_MyElement, _initClass] = fn(this);
  }
  prop = testObj.one;
  static {
    _initClass();
  }
}
// terser options
{
  module: true,
  compress: {},
  mangle: {},
  output: {},
  parse: {},
  rename: {},
}

terser output or error

const t=`${testVarOne}`;let s,a;class c{static{var t;[a,s]=(t=this,[class extends t{},function(){}])}prop=t;static{s()}}

Expected result

const t="Hello this is test var one";let s,a;class c{static{var t;[a,s]=(t=this,[class extends t{},function(){}])}prop=t;static{s()}}
@liuxingbaoyu
Copy link

This doesn't seem to reproduce the problem because you forgot $ before {testVarOne}.
babel/babel#16453 (comment) may be a reproducible code.

@Sayan751
Copy link
Author

This doesn't seem to reproduce the problem because you forgot $ before {testVarOne}. babel/babel#16453 (comment) may be a reproducible code.

Thanks @liuxingbaoyu for pointing it out. Updated the original example.

@fabiosantoscode
Copy link
Collaborator

I managed this reproduction with less elements

const testVarOne = 'Please preserve this';

const usedOnce = testVarOne
const usedTwice = testVarOne

class MyElement {
  static {
    // class refers to itself AND has side-effects.
    sideEffect(this);
  }
  notDeletedReference = usedTwice;
}

The class MyElement looks like it will be dropped because it's unused. But it's not dropped because sideEffect(this) is a use of the class. Using sideEffect(MyElement) would fix the issue, which tells me the problem probably showed up when Terser started to recognize a class this as a reference to it.

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

No branches or pull requests

3 participants