0.1.0 • Published 7 years ago

typescript-plugin-iife-enum v0.1.0

Weekly downloads
5
License
WTFPL
Repository
github
Last release
7 years ago

typescript-plugin-iife-enum

A TypeScript transform to wrapper enum in IIFE.

Purpose

For now TypeScript will transform enum from

enum Test {
    Key = 1
}

to

var Test;
(function (Test) {
    Test[Test["Key"] = 1] = "Key";
})(Test || (Test = {}));

This result is not friendly for uglyify.

So just wrapper IIFE for enum

const Test = (() => {
    enum Test {
        Key = 1
    }
  
    return Test
})

Usage

// 1. import default from the plugin module
import { createIIFEEnumTransformer } from 'typescript-plugin-iife-enum');

// 2. add getCustomTransformer method to the loader config
var config = {
    ...
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                loader: 'ts-loader',
                options: {
                    ... // other loader's options
                    getCustomTransformers: () => ({ before: [createIIFEEnumTransformer()] })
                }
            }
        ]
    }
    ...
};

Know Issue

TypeScript will drop leading comments of call expression, can use babel with plugin to annotate `#PURE`