[go: up one dir, main page]

@vgerbot/channel-transformer
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

@vgerbot/channel-transformer

A typescript transformer, which is used to simplify the use of the @vgerbot/channel library. It has the following conversion features:

  • Convert the members of the interface passed to the get_class method to an array:

    // Automatically the generate class id
    interface RmAPI {
        method();
    }
    
    // before:
    chnl.get_class<RmAPI>();
    // after transformed:
    var RmAPIMembers1 = ['method'];
    chnl.get_class('RmAPI', RmAPIMembers1);
    // Use Interface name as class Id
    interface RmAPI {
      method();
    }
    
    // before
    chnl.get_class<RmAPI>('other-RmAPI');
    // after
    var RmAPIMembers1 = ['method'];
    chnl.get_class('other-RmAPI', RmAPIMembers1);
  • Auto-generated class id and passed to def_class method:

    class ClassA {}
    // before
    chnl.def_class(ClassA);
    // after
    chnl.def_class('ClassA', ClassA);
    interface API {
        method();
    }
    class APIImpl implements API{
        method() {}
    }
    // before
    chnl.def_class(APIImpl);
    // after
    chnl.def_class('APIImpl', APIImpl);
  • Auto-generated method id and passed to def_method method:

    // before
    chnl.def_method(function named_method() {});
    // after
    chnl.def_method('named_method', function named_method() {});
    function named_method() {}
    // before
    chnl.def_method(named_method);
    // after
    chnl.def_method('named_method', named_method);
    const named_method = () => {};
    // before
    chnl.def_method(named_method);
    // after
    chnl.def_method('named_method', named_method);

For more examples, please refer to Unit Tests and Snapshots.

Requirement

Typescript >= 3.2.2

Setup

Webpack(with ts-loader or awesome-typescript-loader)

// webpack.config.js
const tschannelTransformer = require('@vgerbot/channel-transformer').default;
module.exports = {
    // ...
    module: {
        rules: [{
            test: /\.tsx?/,
            loader: 'ts-loader', // or 'awesome-typescript-loader'
            options: {
                getCustomTransformers:program => tschannelTransformer(program)
            }
        }]
    }
}

Rollup(with rollup-plugin-typescript2)

// rollup.config.js
import typescript from "rollup-plugin-typescript2";
const tschannelTransformer = require('@vgerbot/channel-transformer').default;
export default {
  // ...
  plugins: [
    typescript({
      transformers: [
        (languageService) => {
            const program = languageService.getProgram();
            return {
                before: [tschannelTransformer(program)]
            };
        }
      ]
    })
  ]
};

ttypescript

tsconfig.json

{
    "compilerOptions": {
        "plugins": [{
            "transform": "@vgerbot/channel-transformer"
        }]
    }
}

License

License MIT

Package Sidebar

Install

npm i @vgerbot/channel-transformer

Weekly Downloads

1

Version

1.0.1

License

MIT

Unpacked Size

42.3 kB

Total Files

17

Last publish

Collaborators

  • y1j2x34