babel-private-properties
When building a library it is common practice to mark properties as private by prefixing them with an underscore.
This plugin appends a few characters to any properties that are prefixed with an underscore to make it even clearer that these properties are intended for internal use only. The characters are the first part of an md5 hash of the identifier. You can provide a custom salt making it possible to generate different hashes for each build.
Install
npm install --save-dev babel-private-properties
Example
Transforms
{ // don't want users to access this directly this_name = name;} // don't want users to call this directlyHelloLibprototype { return this_name;}; // This method is public and should be called externallyHelloLibprototype { var name = this; console;}; moduleexports = HelloLib;
to
{ // don't want users to access this directly this_name9aca = name;} // don't want users to call this directlyHelloLibprototype { return this_name9aca;}; // This method is public and should be called externallyHelloLibprototype { var name = this; console;}; moduleexports = HelloLib;
Usage
.babelrc
Set plugin options using an array of [pluginName, optionsObject]
.
webpack.config.js
'module': 'loaders': 'loader': 'babel-loader' 'test': /\.js$/ 'exclude': /node_modules/ 'query': 'plugins': 'babel-private-properties'
If the replaceCompletely
option is true
the identifiers will be replaced completley with its hash. This isn't recommended as even though it's incredibly unlikely a hash collision could occur. Keeping the original text prevents this.
Vary Hashes Per Build
If you supply the config with webpack you can change the salt dynamically.
'module': 'loaders': 'loader': 'babel-loader' 'test': /\.js$/ 'exclude': /node_modules/ 'query': 'plugins': "babel-private-properties" "salt": Math // Each build will have unique private property names