0.1.0 • Published 3 years ago

babel-plugin-transform-private-class-members v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

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

Mangle JavaScript private class members.

Installation

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

Usage

Via .babelrc (Recommended)

.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

OptionDescriptionDefault
blacklistA RegEx defining which members to ignore[ /^_super./, /^_.*/, /^$/, /^^_.*/ ]
memoizeKeep mangle position and keys for another transformfalse
onlyClassReplace only private class members and propertiestrue

License

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