@wessberg/di-compiler
A Custom Transformer for Typescript that enables compile-time Dependency Injection
Last updated 2 days 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 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

pnpm

$ pnpm add @wessberg/di-compiler

Peer Dependencies

@wessberg/di-compiler depends on typescript, so you need to manually install these as well.

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 ts-node

One of the simplest ways to use DI-compiler is with ts-node:

node -r @wessberg/di-compiler/register

You can also do it programmatically. Here's an example using CommonJS:

const { di } = require("@wessberg/rollup-plugin-ts");

require("ts-node").register({
  transformers: (program) => di({ program }),
});
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 }),
        },
      },
    ],
  },
  // ...
};

Usage with ava

You can also use DI-compiler with the ava test runner with the require property in the ava configuration:

{
  // Other options...
  extensions: ["ts"],
  require: ["@wessberg/di-compiler/register"],
}

Options

You can provide options to the di Custom Transformer to configure its behavior:

Option Description
program A full TypeScript program (required).
typescript (optional) If given, the TypeScript version to use internally for all operations.

Contributing

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

Maintainers

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

Backers

Bubbles
Bubbles
Twitter: @use_bubbles

Patreon

Patrons 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.1.1                                ...           latest (2 days ago)

80 Versions

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

Copyright 2014 - 2017 © taobao.org |