Skip to content

A TypeScript custom transformer which renames private class members

License

Notifications You must be signed in to change notification settings

bo4arovpavel1989/ts-transformer-minify-privates

Repository files navigation

ts-transformer-minify-privates

(CURRENTLY WORK IN PROGRESS)

A TypeScript custom transformer which minify names of private class members.

Requirement

TypeScript >= 2.9.1

How to use the custom transformer

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 (with ts-loader or awesome-typescript-loader)

// 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 (with rollup-plugin-typescript2)

// 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: []
    })] })
  ]
};

ttypescript

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" }
    ]
  },
  // ...
}

TODO

  1. Tests
  2. Handle or fail for accessing runtime-based properties, e.g. this[Math.random() > .5 ? 'privateMethod' : 'privateMethod2']().
  3. Implement stable name generation (e.g. calc hash from name, not just random name) to increase stability and avoid changing hashes of files.
  4. Add option to choose strategy of the name generator (per file, per project, etc).

Note

About

A TypeScript custom transformer which renames private class members

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 65.4%
  • JavaScript 33.1%
  • Shell 1.5%