Mangle JavaScript private class members.
$ npm install babel-plugin-transform-private-class-members
.babelrc
{
"plugins": ["babel-plugin-transform-private-class-members"]
}
$ babel --plugins babel-plugin-transform-private-class-members script.js
require("babel-core").transform("code", {
plugins: ["babel-plugin-transform-private-class-members"]
});
Input file:
class Rectangle {
result = null;
constructor(height, width) {
this._height = height;
this._width = width;
}
area() {
if (this.result === null) {
this.result = this._calcArea();
}
return this.result;
}
_calcArea() {
return this._height * this._width;
}
}
Output:
class Rectangle {
result = null;
constructor(height, width) {
this.$1 = height;
this.$2 = width;
}
area() {
if (this.result === null) {
this.result = this.$3();
}
return this.result;
}
$3() {
return this.$1 * this.$2;
}
}
Option | Description | Default |
---|---|---|
blacklist | A RegEx defining which members to ignore | [ /^_super./, /^__./, /^_$/, /^[^_]./ ] |
memoize | Keep mangle position and keys for another transform | false |
onlyClass | Replace only private class members and properties | true |
This plugin is licensed under the MIT license. See LICENSE.