1.1.1 β€’ Published 4 months ago

babel-plugin-transform-private-to-hash v1.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

babel-plugin-transform-private-to-hash

npm License: MIT CI Status

A Babel plugin that transforms ES2022+ private class fields/methods into public properties with hashed names, supporting configurable salt and hash length.

Features

  • πŸ”’ Converts private fields/methods to uniquely hashed public properties
  • πŸ§‚ Configurable salt for hash generation
  • πŸ“ Customizable hash length (1-8 chars)
  • 🚫 Configurable enumerability (default: false)
  • πŸ’ͺ Full support for static/instance properties and methods
  • βœ… Handles in operator checks correctly

Installation

npm install --save-dev babel-plugin-transform-private-to-hash

Usage

ViaΒ .babelrc

{
  "plugins": [
    ["transform-private-to-hash", {
      "salt": "my-secret-salt",
      "enumerable": false,
      "hashLength": 8
    }]
  ]
}

Configuration Options

OptionTypeDefaultDescription
saltstring''Salt value for hash generation
enumerablebooleanfalseWhether the transformed properties should be enumerable
hashLengthnumber8Length of the hash substring (1-32)

Examples

Basic Transformation

Input:

class MyClass {
  #privateField = 42;
  #method() {}
  static #staticField = 10;

  check() {
    return #privateField in this && this.#method();
  }
}

Output (with default config):

class MyClass {
  constructor() {
    Object.defineProperty(this, "__e5f6g7h8", {
      value: 42,
      enumerable: false,
      configurable: true,
      writable: true
    });
  }

  __i9j0k1l2() {}

  check() {
    return "__e5f6g7h8" in this && this.__i9j0k1l2();
  }

  static {
    Object.defineProperty(this, "__a1b2c3d4", {
      value: 10,
      enumerable: false,
      configurable: true,
      writable: true
    });
  }
}

Configurable Hash Length

Config:

{ "hashLength": 4 }

Output Property Name:

__a1b2

Enumerable Mode

Config:

{ "enumerable": true }

Output:

class MyClass {
  __a1b2c3d4 = 42;
  static __e5f6g7h8 = 10;
}
1.1.1

4 months ago

1.1.0

4 months ago

1.0.1

4 months ago

1.0.0

4 months ago