npm.io
0.1.0 • Published 5 years ago

babel-plugin-transform-private-class-members

Licence
MIT
Version
0.1.0
Deps
0
Size
8 kB
Vulns
0
Weekly
0
Stars
1

babel-plugin-transform-private-class-members npm

Mangle JavaScript private class members.

Installation

$ npm install babel-plugin-transform-private-class-members

Usage

.babelrc

{
  "plugins": ["babel-plugin-transform-private-class-members"]
}
Via CLI
$ babel --plugins babel-plugin-transform-private-class-members script.js
Via Node API
require("babel-core").transform("code", {
  plugins: ["babel-plugin-transform-private-class-members"]
});

Example

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;
    }
}

Options

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

License

This plugin is licensed under the MIT license. See LICENSE.

Keywords