Can Scala.JS obfuscate case class names in the generated JS source?

Hello,

I have noticed that Scala.JS preserves case class names in the generated JS code.

This is not ideal for 2 reasons:

  1. It exposes the internal implementation details of the project.
  2. It inflates the bundle size somewhat.

Could case class names get renamed to the same name that is used by the Closure compiler? Could an exception be thrown when .toString is called instead?

1 Like

Yes, you can configure the linker to alter the run-time name of classes. To erase them all to empty strings, you can do

import org.scalajs.linker.interface.Semantics.RuntimeClassNameMapper

scalaJSLinkerConfig ~= { prev =>
  prev.withSemantics(
    _.withRuntimeClassNameMapper(
      RuntimeClassNameMapper.discardAll()
    )
  )
}

More possibilities for the RuntimeClassNameMapper argument can be found in the linker API documentation:
https://www.scala-js.org/api/scalajs-linker-interface/latest/org/scalajs/linker/interface/Semantics$$RuntimeClassNameMapper$.html

1 Like

Right, but this will still keep the original names of the case classes, if I am not mistaken, right?

1 Like

Yes. That’s part of their case class semantics. There’s nothing Scala.js can do about that.

How would one go about solving this issue? Taking the final JS generated source and replace those methods with a Regex?

You could preprocess the IR before it reaches the linker. You can do that by passing the Compile / scalaJSIR setting through whatever filter you’d like.