babel-plugin-transform-private-to-hash
Version:
Transform ES private fields/methods to hashed properties.
126 lines (94 loc) • 2.79 kB
Markdown
# babel-plugin-transform-private-to-hash
[](https://www.npmjs.com/package/babel-plugin-transform-private-to-hash)
[](https://opensource.org/licenses/MIT)
[](https://github.com/linsk1998/babel-plugin-transform-private-to-hash/actions)
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
```bash
npm install --save-dev babel-plugin-transform-private-to-hash
```
## Usage
### Via `.babelrc`
```json
{
"plugins": [
["transform-private-to-hash", {
"salt": "my-secret-salt",
"enumerable": false,
"hashLength": 8
}]
]
}
```
## Configuration Options
| Option | Type | Default | Description |
| -------------- | ------- | ------- | ------------------------------------------------------- |
| **salt** | string | `''` | Salt value for hash generation |
| **enumerable** | boolean | `false` | Whether the transformed properties should be enumerable |
| **hashLength** | number | `8` | Length of the hash substring (1-32) |
## Examples
### Basic Transformation
**Input:**
```javascript
class MyClass {
#privateField = 42;
#method() {}
static #staticField = 10;
check() {
return #privateField in this && this.#method();
}
}
```
**Output (with default config):**
```javascript
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:**
```json
{ "hashLength": 4 }
```
**Output Property Name:**
```javascript
__a1b2
```
### Enumerable Mode
**Config:**
```json
{ "enumerable": true }
```
**Output:**
```javascript
class MyClass {
__a1b2c3d4 = 42;
static __e5f6g7h8 = 10;
}
```