swc-project / swc

Rust-based platform for the Web
https://swc.rs
Apache License 2.0
31.15k stars 1.23k forks source link

Minifier rule proposal: mangle private fileds and methods in class #1917

Open Brooooooklyn opened 3 years ago

Brooooooklyn commented 3 years ago

Describe the feature

class WhatEver {
  private someThingUsedInternalOnlyButVeryLongLikeObjectiveCStyle = 0

  public thePublicMethodShouldNotBeMangled() {
    this.privateClassMethodNameShouldAlsoBeMangled()
    return this.someThingUsedInternalOnlyButVeryLongLikeObjectiveCStyle
  }

  private privateClassMethodNameShouldAlsoBeMangled() {
    this.someThingUsedInternalOnlyButVeryLongLikeObjectiveCStyle += 1
  }
}

↓ ↓ ↓ ↓ ↓ ↓

class A {
  private a = 0
  thePublicMethodShouldNotBeMangled() {
    this.b()
    return this.a
  }

  b() {
    this.a += 1
  }
}

This should be an optional rule, it's very dangerous, but very useful.

kdy1 commented 3 years ago

I agree that this would be useful. Marking private methods using special Mark within typescript pass and using the info from the minifier will work.

The logic for private properties (from ecmascript spec) can be applied to such nodes.

chocolateboy commented 2 years ago

Related TypeScript proposals:

And implementations: