rollup-plugin-typescript-privatify
v0.2.3
Published
Custom transformers for @rollup/plugin-typescript to turn TypeScript private members into runtime-private implementations.
Maintainers
Readme
rollup-plugin-typescript-privatify
Custom transformer preset for @rollup/plugin-typescript.
It rewrites TypeScript private class members into runtime-private implementations in 2 modes.
Note: this package is not a direct Rollup plugin. It is a preset for
@rollup/plugin-typescript'stransformersoption. Note:pnpm add typescriptis required.
Install
pnpm add -D rollup-plugin-typescript-privatify @rollup/plugin-typescript typescriptUsage
import typescript from '@rollup/plugin-typescript';
import typescriptPrivatify from 'rollup-plugin-typescript-privatify';
export default {
plugins: [
typescript({
transformers: typescriptPrivatify({
mode: 'hash', // or "weakmap"
}),
}),
],
};This package is not a direct Rollup plugin. If you put it in
plugins: [], it will throw a usage error and tell you to move it intotypescript({ transformers: ... }).
Options
mode: "hash" | "weakmap"(default:"hash")hash: convertprivate footo#foo.weakmap: generate a companionClassName__privateand aWeakMapto store private state/methods.
hidePrivateDeclarations: boolean(default:false)- when
true, registers anafterDeclarationstransformer to removeprivatemembers from declaration AST. - if your declarations are produced by another toolchain stage (for example
rollup-plugin-dts), this option will not affect that stage.
- when
Use With rollup-plugin-dts
When you bundle declarations with rollup-plugin-dts, use the named plugin export:
import { dts } from 'rollup-plugin-dts';
import { hidePrivateDeclarationsForDts } from 'rollup-plugin-typescript-privatify';
export default {
input: 'dist/types/index.d.ts',
output: [{ file: 'dist/index.d.ts', format: 'es' }],
plugins: [dts(), hidePrivateDeclarationsForDts()],
};hidePrivateDeclarationsForDts() can run before or after dts(), but placing it after is usually clearer because it operates on bundled declaration output.
weakmap mode shape
For class A, the transformer emits:
class A__private { ... }const __A_private = new WeakMap();__A_private.set(this, new A__private())in constructor- private method calls become
__A_private.get(this).method.call(this, ...)
Notes
- This package is a transformer preset, not a direct Rollup plugin.
- Anonymous classes in
weakmapmode fall back tohashmode.
