foreverflying / minify-ts

A minifier based on typescript compiler to safely rename all of the variables, classes, functions, properties and methods to short.
MIT License
7 stars 0 forks source link

minify-ts

minify-ts npm version

A TypeScript minifier based on TypeScript compiler to safely rename all of the variables, classes, functions, properties and methods to short.

It also provides a useful feature: merge the SourceMap files generated in several steps, and overwrite the last one with the merged one.

What problems do I aim to solve?

Popular traditional minifiers are mostly based on JavaScript, as a result, they cannot get the information of types to decide whether a property name can be changed safely or not. So generally they just leave it unchanged, which stops them achieve the smaller minified size. In another hand, more people like to write code with classes in TypeScript, so more properties and methods are involved. Keeping their names unchanged will also affect another goal: protect your source code. The minify-ts uses TypeScript compiler to find out the safe way and smartly keeps the exports unchanged in your specified files: the exported variables, functions, classes and their public members.

Install

install as a dev dependency and run with npx minify-ts

npm install --save-dev minify-ts

or install it globally and run with command minify-ts

npm install -g minify-ts

Important:

Command line usage

To show the usage: minify-ts -h

Usage: minify-ts [options] <src-dir> <out-dir> <interfaces...>

Minify all the files that used by the interface files.

Arguments:
  src-dir          the source folder path
  out-dir          the output folder path
  interfaces       the interface files (relative path to src-dir)

Options:
  -s --source-map  generate source files in the out-dir
  -o --obfuscate   can change to different names if two same name
                   variables does not have relations
  -h, --help       display help for command

Examples

To minify your project in ./src, generate the minified files in ./dest, while all your exported variables, functions and classes are defined in file ./src/exports/export1.ts and ./src/exports/export2.ts:

minify-ts -s ./src ./dest exports/export1.ts exports/export2.ts

Clarify: All the code files involved will be minified, not only the interface files.

Usage

import { minify, MinifierOptions, writeDestFile } from 'minify-ts'

const options: MinifierOptions = {
    srcDir: '/absolute/path/to/src/folder',
    destDir: '/absolute/path/to/dest/folder',
    interfaceFileArr: ['relative/path/to/src/folder/file.ts'],
    generateSourceMap: true,    // optional, default is false
    obfuscate: false,           // optional, default is false
}

minify(minifierOptions, writeDestFile)

Thanks to

source-map

TODO

The exported namespace's exported members can not be recognised.

Flatten the directory and change all file names to short.

Detect all the implicit declarations and give Warnings or Errors.

The minify implementation is based on TypeScript language service find references API, which makes it very slow, consider if there are better choices.