(CURRENTLY WORK IN PROGRESS)
A TypeScript custom transformer which minify names of private class members.
TypeScript >= 2.9.1
Unfortunately, TypeScript itself does not currently provide any easy way to use custom transformers (see microsoft/TypeScript#14419). The followings are the example usage of the custom transformer.
// webpack.config.js
const minifyPrivatesTransformer = require('ts-transformer-minify-privates').default;
module.exports = {
// ...
module: {
rules: [
{
test: /\.ts$/,
loader: 'ts-loader', // or 'awesome-typescript-loader'
options: {
getCustomTransformers: program => ({
before: [
minifyPrivatesTransformer(program)
]
})
}
}
]
}
};
// rollup.config.js
import typescript from 'rollup-plugin-typescript2';
import minifyPrivatesTransformer from 'ts-transformer-minify-privates';
export default {
// ...
plugins: [
typescript({ transformers: [service => ({
before: [ minifyPrivatesTransformer(service.getProgram()) ],
after: []
})] })
]
};
See ttypescript's README for how to use this with module bundlers such as webpack or Rollup.
// tsconfig.json
{
"compilerOptions": {
// ...
"plugins": [
{ "transform": "ts-transformer-minify-privates" }
]
},
// ...
}
- Tests
- Handle or fail for accessing runtime-based properties, e.g.
this[Math.random() > .5 ? 'privateMethod' : 'privateMethod2']()
. - Implement stable name generation (e.g. calc hash from name, not just random name) to increase stability and avoid changing hashes of files.
- Add option to choose strategy of the name generator (per file, per project, etc).