1.4.2 • Published 7 years ago

ts-ezinjector-compiler v1.4.2

Weekly downloads
3
License
ISC
Repository
github
Last release
7 years ago

ts-ezinjector-compiler

ts-ezinjector-compiler is used to compile interfaces as string in typescript compile time. Supports interface intellisense and avoids magic strings in your code.

Works together with ezinjector

https://github.com/jeppeskovsen/ezinjector

Installation

$ npm install ts-ezinjector-compiler --save-dev

Examples

container.register<IService>(Service);
Container.getInstance<IService>();
@Inject<IService>()
@StaticInject<IService>()

Becomes this at compile time:

container.register("IService", Service);
Container.getInstance("IService");
@Inject("IService")
@StaticInject("IService")

Configs

Overwrite configs inside tsEzinjectorCompiler({})

Defaults:

{
  replacements: [{
    find: /(\.register)<([\w\d]+?)>\s?\(/ig,
    buildMethod: (method, params) => method + "(" + params + ", ",
    buildParams: (params) => "\"" + (params.indexOf(',') == -1 ? params : params.split(/\s*,\s*/).join("\", \"")) + "\"",
  }, {
    find: /(@Resolve|@[\w\d]*?Inject[\w\d]*?|@StaticInject|Container\.getInstance)<([\w\d\s,]+?)>\s?\(/ig,
    buildMethod: (method, params) => method + "(" + params,
    buildParams: (params) => "\"" + (params.indexOf(',') == -1 ? params : params.split(/\s*,\s*/).join("\", \"")) + "\"",
  }, {
    find: /(\/\/\s?@ezInject\s*constructor)\s*\(\s*((?:(?:\s*@Inject\(\"[\w\d]+\"\))?[\s\w\d:],?)+)\)/ig,
    buildMethod: (method, params) => method + "(" + params + ")",
    buildParams: (params) => {

      const buildParam = (param) => {
        param = param.replace(/\s*(?:@Inject\(\"[\w\d]+\"\))?\s*((?:[\w]+\s*?)?[\w\d]+)\s*:\s*([\w\d]+)\s*/i, "@Inject(\"$2\") $1: $2");
        return param;
      }

      if (params.indexOf(",") !== -1) {
        var returnValues = [];

        for (let param of params.split(",")) {
          returnValues.push(buildParam(param));
        }

        return returnValues.join(", ");
      }

      return buildParam(params);
    }
  }]
}

Smart inject

You can use //@ezInject comment above the constructor you want to auto inject

export class abc {
    // @ezInject
    constructor(
        private testService: ITestService, 
        public anotherTestService: IAnotherTestService
    ) {}
}

becomes:

export class abc {
    // @ezInject
    constructor(
        @Inject("ITestService") private testService: ITestService, 
        @Inject("IAnotherTestService") public anotherTestService: IAnotherTestService
    ) {}
}

With Gulp

const tsEzinjectorCompiler = require("ts-ezinjector-compiler");

gulp.src("./scripts/*.ts")
    .pipe(tsEzinjectorCompiler())
    .pipe(ts())
    .pipe(gulp.dest('./test/dist'));

Webpack loader

https://github.com/jeppeskovsen/ts-ezinjector-compiler-loader

$ npm install ts-ezinjector-compiler-loader --save-dev

Example:

module: {
    rules: [
        // All files with a '.ts' or '.tsx' extension will be handled by 'awesome-typescript-loader'.
        { 
            test: /\.tsx?$/, 
            loaders: ["awesome-typescript-loader", "ts-ezinjector-compiler-loader"]
        }
    ]
}
1.4.2

7 years ago

1.4.1

7 years ago

1.3.1

7 years ago

1.3.0

7 years ago

1.2.6

7 years ago

1.2.5

7 years ago