@wessberg/di-compiler
A Custom Transformer for Typescript that enables compile-time Dependency Injection
Last updated 2 months ago by wessberg .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @wessberg/di-compiler 
SYNC missed versions from official npm registry.
Logo

A Custom Transformer for Typescript that enables compile-time Dependency Injection

Downloads per month NPM version Dependencies Contributors code style: prettier License: MIT Support on Patreon

Description

This is a CustomTransformer for Typescript that enables you to use the DI library.

This has been implemented as a TypeScript Custom Transformer in order to be so low-level that it can be used as the underlying implementation in anything you want, whether it be directly with Typescript's Compiler APIs, Webpack loaders, Rollup plugins, or something else.

Features

  • Really lightweight
  • Really fast
  • Low-level implementation that can be used as the foundation for other tools such as Loaders, Plugins, and others.
  • It doesn't ask you to reflect metadata or to annotate your classes with decorators. "It just works".

Table of Contents

Install

NPM

$ npm install @wessberg/di-compiler

Yarn

$ yarn add @wessberg/di-compiler

Usage

Since this is a Custom Transformer, it can be used practically anywhere you use TypeScript.

The most obvious place would be to use it directly with Typescript's compiler APIs:

Usage with TypeScript's Compiler APIs

There's several ways to do this, but here's a simple example:

import {
  createProgram,
  getDefaultCompilerOptions,
  createCompilerHost
} from "typescript";
import { di } from "@wessberg/di-compiler";

const compilerOptions = getDefaultCompilerOptions();
const compilerHost = createCompilerHost(compilerOptions);

// Create a Typescript program
const program = createProgram(
  ["my-file-1.ts", "my-file-2.ts"],
  compilerOptions,
  compilerHost
);

// Transform the SourceFiles within the program, and pass them through the 'di' transformer
program.emit(undefined, undefined, undefined, undefined, di({ program }));

Usage with Rollup

There are two popular TypeScript plugins for Rollup that support Custom Transformers:

Usage with rollup-plugin-ts

import ts from "@wessberg/rollup-plugin-ts";
import { di } from "@wessberg/di-compiler";

export default {
  input: "...",
  output: [
    /* ... */
  ],
  plugins: [
    ts({
      transformers: [di]
    })
  ]
};

Usage with rollup-plugin-typescript2

import ts from "rollup-plugin-typescript2";
import { di } from "@wessberg/di-compiler";

export default {
  input: "...",
  output: [
    /* ... */
  ],
  plugins: [
    ts({
      transformers: [service => di({ program: service.getProgram() })]
    })
  ]
};

Usage with Webpack

There are two popular TypeScript loaders for Webpack that support Custom Transformers:

Usage with awesome-typescript-loader

import { di } from "@wessberg/di-compiler";
const config = {
  // ...
  module: {
    rules: [
      {
        // Match .mjs, .js, .jsx, and .tsx files
        test: /(\.mjs)|(\.[jt]sx?)$/,
        loader: "awesome-typescript-loader",
        options: {
          // ...
          getCustomTransformers: program => di({ program })
        }
      }
    ]
  }
  // ...
};

Usage with ts-loader

import { di } from "@wessberg/di";
const config = {
  // ...
  module: {
    rules: [
      {
        // Match .mjs, .js, .jsx, and .tsx files
        test: /(\.mjs)|(\.[jt]sx?)$/,
        loader: "ts-loader",
        options: {
          // ...
          getCustomTransformers: program => di({ program })
        }
      }
    ]
  }
  // ...
};

Contributing

Do you want to contribute? Awesome! Please follow these recommendations.

Maintainers

Frederik Wessberg
Frederik Wessberg
Twitter: @FredWessberg
Github: @wessberg
Lead Developer

Backers

Patreon

Become a backer and get your name, avatar, and Twitter handle listed here.

Backers on Patreon

FAQ

How does it work, exactly?

First, classes that are discovered as part of your Typescript program/bundle will be parsed for their constructor argument types and positions. Then, instances of the DIContainer will be discovered and their expressions will be upgraded. For example, an expression such as:

import { DIContainer } from "@wessberg/di";
import { MyInterface } from "./my-interface";
import { Myimplementation } from "./my-implementation";

const container = new DIContainer();
container.registerSingleton<MyInterface, MyImplementation>();

Will be compiled into:

// ...
container.registerSingleton(undefined, {
  identifier: "MyInterface",
  implementation: MyImplementation
});

License

MIT © Frederik Wessberg (@FredWessberg) (Website)

Current Tags

  • 2.0.5                                ...           latest (2 months ago)

78 Versions

  • 2.0.5                                ...           2 months ago
  • 2.0.4                                ...           3 months ago
  • 2.0.3                                ...           3 months ago
  • 2.0.2                                ...           9 months ago
  • 2.0.1                                ...           9 months ago
  • 2.0.0                                ...           9 months ago
  • 1.0.76                                ...           a year ago
  • 1.0.75                                ...           a year ago
  • 1.0.74                                ...           a year ago
  • 1.0.73                                ...           a year ago
  • 1.0.72                                ...           a year ago
  • 1.0.71                                ...           a year ago
  • 1.0.70                                ...           a year ago
  • 1.0.69                                ...           a year ago
  • 1.0.68                                ...           a year ago
  • 1.0.67                                ...           a year ago
  • 1.0.66                                ...           a year ago
  • 1.0.65                                ...           a year ago
  • 1.0.64                                ...           a year ago
  • 1.0.63                                ...           a year ago
  • 1.0.62                                ...           2 years ago
  • 1.0.61                                ...           2 years ago
  • 1.0.60                                ...           2 years ago
  • 1.0.59                                ...           2 years ago
  • 1.0.58                                ...           2 years ago
  • 1.0.57                                ...           2 years ago
  • 1.0.56                                ...           2 years ago
  • 1.0.55                                ...           2 years ago
  • 1.0.54                                ...           2 years ago
  • 1.0.53                                ...           2 years ago
  • 1.0.52                                ...           2 years ago
  • 1.0.51                                ...           2 years ago
  • 1.0.49                                ...           2 years ago
  • 1.0.47                                ...           2 years ago
  • 1.0.46                                ...           2 years ago
  • 1.0.45                                ...           2 years ago
  • 1.0.44                                ...           2 years ago
  • 1.0.43                                ...           2 years ago
  • 1.0.42                                ...           2 years ago
  • 1.0.40                                ...           2 years ago
  • 1.0.39                                ...           2 years ago
  • 1.0.38                                ...           2 years ago
  • 1.0.36                                ...           2 years ago
  • 1.0.35                                ...           2 years ago
  • 1.0.34                                ...           2 years ago
  • 1.0.33                                ...           2 years ago
  • 1.0.32                                ...           2 years ago
  • 1.0.31                                ...           2 years ago
  • 1.0.30                                ...           2 years ago
  • 1.0.29                                ...           2 years ago
  • 1.0.28                                ...           2 years ago
  • 1.0.27                                ...           2 years ago
  • 1.0.25                                ...           2 years ago
  • 1.0.24                                ...           2 years ago
  • 1.0.23                                ...           2 years ago
  • 1.0.22                                ...           2 years ago
  • 1.0.21                                ...           2 years ago
  • 1.0.20                                ...           2 years ago
  • 1.0.19                                ...           2 years ago
  • 1.0.18                                ...           2 years ago
  • 1.0.17                                ...           2 years ago
  • 1.0.16                                ...           2 years ago
  • 1.0.15                                ...           2 years ago
  • 1.0.14                                ...           2 years ago
  • 1.0.13                                ...           2 years ago
  • 1.0.12                                ...           2 years ago
  • 1.0.11                                ...           2 years ago
  • 1.0.10                                ...           2 years ago
  • 1.0.9                                ...           2 years ago
  • 1.0.8                                ...           2 years ago
  • 1.0.7                                ...           2 years ago
  • 1.0.6                                ...           2 years ago
  • 1.0.5                                ...           2 years ago
  • 1.0.4                                ...           2 years ago
  • 1.0.3                                ...           2 years ago
  • 1.0.2                                ...           2 years ago
  • 1.0.1                                ...           2 years ago
  • 1.0.0                                ...           2 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 5
Last Day 0
Last Week 3
Last Month 166
Dependencies (2)
Dev Dependencies (10)

Copyright 2014 - 2016 © taobao.org |