babel-plugin-transform-private-to-hash
v1.1.1
Published
Transform ES private fields/methods to hashed properties.
Maintainers
Readme
babel-plugin-transform-private-to-hash
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
inoperator checks correctly
Installation
npm install --save-dev babel-plugin-transform-private-to-hashUsage
Via .babelrc
{
"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:
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:
__a1b2Enumerable Mode
Config:
{ "enumerable": true }Output:
class MyClass {
__a1b2c3d4 = 42;
static __e5f6g7h8 = 10;
}