ToddThomson / tsproject

Typescript minifier and modular typescript bundle optimizer for gulp (Ts Vinyl Adapter).

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bundle Minification

ToddThomson opened this issue · comments

I will be adding experimental support for bundle minification in release 1.2.0.

The goal for the minification feature in TsProject is to take advantage of the Typescript AST to efficiently shorten identifiers and eliminate whitespace so that the resulting transform is 100% safe.

I have added the minification branch. ( Now merged into Master )

This branch uses TsProject with minification to build TsProject.

Please note that this is preliminary, experimental code that is not yet complete. The structure and rough implementation for identifier shortening and whitespace removal is there, but is not production ready.

I have created this branch so that others may comment and hopefully be involved.

Please note that I developed TsProject for my own internal needs. If you find it useful then great! I try to get a few hours each week to work on features and to fix issues. I will try to update the minification branch weekly and if all goes well the NPM release should be up within 2 or 3 weeks.

Reference to the Typescript minification issue for those interested.
microsoft/TypeScript#8

TODO for bundle minification...

Identifier Shortening

  1. Add variable declarations (now testing)
  2. Add parameter declarations (now testing)
  3. Add private property declarations (now testing)
  4. Add private method declarations (now testing)
  5. Add all internal symbols (now testing)
    • non-visible/internal classifiable symbols
    • non-visible properties and methods

Whitespace Removal

Add additional catches for removal of all whitespace (now testing)

Script Target

ES5 support tested and completed then ES6 testing.

Here is an example of minifier in TsProject ( whitespace elimination turned off )
Source:

export class TestClass {

    private gerb1: number = 1;
    private gerb2: number = 2;
    private gerb3: number = 3;
    private gerb4: number = 4;

    public fPublic(): number {
        let result = this.f1();

        return result;
    }

    private f1(): number {
        let result = this.f2( 5, 2 ) + this.f3( 19 );

        return result;
    }

    private f2( parm1: number, parm2: number ): number {
        let result = parm1 + parm2;

        return result;
    }
    private f3( parm1: number ): number {
        return parm1 + this.f2( 1, 2 );
    }
}

var test = new TestClass();
var t1 = test.fPublic();
console.log( t1 );
var TestClass = (function () {
    function TestClass() {
        this.a = 1;
        this.b = 2;
        this.c = 3;
        this.d = 4;
    }
    TestClass.prototype.fPublic = function () {
        var a = this.e();
        return a;
    };
    TestClass.prototype.e = function () {
        var a = this.f(5, 2) + this.g(19);
        return a;
    };
    TestClass.prototype.f = function (a, b) {
        var c = a + b;
        return c;
    };
    TestClass.prototype.g = function (a) {
        return a + this.f(1, 2);
    };
    return TestClass;
})();
exports.TestClass = TestClass;
var a = new TestClass();
var b = a.fPublic();
console.log(b);

Minification branch updated. TsProject.min.js is built with TsProject ( whitespace elimination not enabled for readability / testing ).

Minification feature updated with release 1.2.0-rc.3 ( fixes class inheritance issue #68 )

Minification feature updated with release 1.2.0-rc.4 ( full whitespace removal )

The first phase of the minification feature in TsProject is now close to being complete. The next phase will add additional identifier shortening by looking at the exports of the whole bundle ( a module/component ) and reduce space by shortening class names and public class methods.

With the addition of bundle configuration options "package" and "packageNamespace", TsProject will now shorten identifier names of all internal declarations that are not in the packageNamespace when the package is set to "component".

Adding namespace import aliases to the list of identifiers which can be shortened.

Provided in TsProject 1.2.