Skip to content

Commit

Permalink
feat: major refactoring with support for CommonJS, AMD, and SystemJS …
Browse files Browse the repository at this point in the history
…as targets
  • Loading branch information
wessberg committed May 25, 2020
1 parent 398b573 commit bfeedef
Show file tree
Hide file tree
Showing 39 changed files with 8,409 additions and 731 deletions.
8 changes: 8 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"env": {
"es2020": true,
"shared-node-browser": true,
"node": true
},
"extends": "./node_modules/@wessberg/ts-config/.eslintrc.json"
}
2 changes: 2 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github: wessberg
patreon: wessberg
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright © 2019 [Frederik Wessberg](mailto:frederikwessberg@hotmail.com) ([@FredWessberg](https://twitter.com/FredWessberg)) ([Website](https://github.com/wessberg))
Copyright © 2020 [Frederik Wessberg](mailto:frederikwessberg@hotmail.com) ([@FredWessberg](https://twitter.com/FredWessberg)) ([Website](https://github.com/wessberg))

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
134 changes: 101 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
<a href="https://www.npmjs.com/package/%40wessberg%2Fdi-compiler"><img alt="NPM version" src="https://badge.fury.io/js/%40wessberg%2Fdi-compiler.svg" /></a>
<a href="https://david-dm.org/wessberg/di-compiler"><img alt="Dependencies" src="https://img.shields.io/david/wessberg%2Fdi-compiler.svg" /></a>
<a href="https://github.com/wessberg/di-compiler/graphs/contributors"><img alt="Contributors" src="https://img.shields.io/github/contributors/wessberg%2Fdi-compiler.svg" /></a>
<a href="https://github.com/prettier/prettier"><img alt="code style: prettier" src="https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square" /></a>
<a href="https://opensource.org/licenses/MIT"><img alt="License: MIT" src="https://img.shields.io/badge/License-MIT-yellow.svg" /></a>
<a href="https://www.patreon.com/bePatron?u=11315442"><img alt="Support on Patreon" src="https://img.shields.io/badge/patreon-donate-green.svg" /></a>

Expand Down Expand Up @@ -55,22 +54,16 @@ This has been implemented as a TypeScript Custom Transformer in order to be so l
- [Features](#features)
- [Table of Contents](#table-of-contents)
- [Install](#install)
- [NPM](#npm)
- [npm](#npm)
- [Yarn](#yarn)
- [pnpm](#pnpm)
- [Peer Dependencies](#peer-dependencies)
- [Usage](#usage)
- [Usage with TypeScript's Compiler APIs](#usage-with-typescripts-compiler-apis)
- [Usage with Rollup](#usage-with-rollup)
- [Usage with rollup-plugin-ts](#usage-with-rollup-plugin-ts)
- [Usage with rollup-plugin-typescript2](#usage-with-rollup-plugin-typescript2)
- [Usage with Webpack](#usage-with-webpack)
- [Usage with awesome-typescript-loader](#usage-with-awesome-typescript-loader)
- [Usage with ts-loader](#usage-with-ts-loader)
- [Contributing](#contributing)
- [Maintainers](#maintainers)
- [Backers](#backers)
- [Patreon](#patreon)
- [FAQ](#faq)
- [How does it work, exactly?](#how-does-it-work-exactly)
- [License](#license)

<!-- SHADOW_SECTION_TOC_END -->
Expand All @@ -79,7 +72,7 @@ This has been implemented as a TypeScript Custom Transformer in order to be so l

## Install

### NPM
### npm

```
$ npm install @wessberg/di-compiler
Expand All @@ -91,6 +84,16 @@ $ npm install @wessberg/di-compiler
$ 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.

<!-- SHADOW_SECTION_INSTALL_END -->

<!-- SHADOW_SECTION_USAGE_START -->
Expand All @@ -111,7 +114,48 @@ There's several ways to do this, but here's a simple example:
import {
createProgram,
getDefaultCompilerOptions,
createCompilerHost
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`](https://github.com/TypeStrong/ts-node):

```
node -r @wessberg/di-compiler/register <some-file.ts>
```

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

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

require("ts-node").register({
transpileOnly: false,
transformers: (program) => di({ program }),
});
```

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

Expand Down Expand Up @@ -149,9 +193,9 @@ export default {
],
plugins: [
ts({
transformers: [di]
})
]
transformers: [di],
}),
],
};
```

Expand All @@ -168,9 +212,9 @@ export default {
],
plugins: [
ts({
transformers: [service => di({ program: service.getProgram() })]
})
]
transformers: [(service) => di({ program: service.getProgram() })],
}),
],
};
```

Expand All @@ -195,11 +239,11 @@ const config = {
loader: "awesome-typescript-loader",
options: {
// ...
getCustomTransformers: program => di({ program })
}
}
]
}
getCustomTransformers: (program) => di({ program }),
},
},
],
},
// ...
};
```
Expand All @@ -218,15 +262,37 @@ const config = {
loader: "ts-loader",
options: {
// ...
getCustomTransformers: program => di({ program })
}
}
]
}
getCustomTransformers: (program) => di({ program }),
},
},
],
},
// ...
};
```

### Usage with ava

You can also use DI-compiler with the [`ava`](https://github.com/avajs/ava) test runner
with the `require` property in the `ava` configuration:

```json
{
// 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. |

<!-- SHADOW_SECTION_CONTRIBUTING_START -->

## Contributing
Expand All @@ -239,7 +305,7 @@ Do you want to contribute? Awesome! Please follow [these recommendations](./CONT

## Maintainers

| <img alt="Frederik Wessberg" src="https://avatars2.githubusercontent.com/u/20454213?s=460&v=4" height="70" /> |
| <a href="mailto:frederikwessberg@hotmail.com"><img alt="Frederik Wessberg" src="https://avatars2.githubusercontent.com/u/20454213?s=460&v=4" height="70" /></a> |
| -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [Frederik Wessberg](mailto:frederikwessberg@hotmail.com)<br><strong>Twitter</strong>: [@FredWessberg](https://twitter.com/FredWessberg)<br><strong>Github</strong>: [@wessberg](https://github.com/wessberg)<br>_Lead Developer_ |

Expand All @@ -249,11 +315,13 @@ Do you want to contribute? Awesome! Please follow [these recommendations](./CONT

## Backers

### Patreon
| <a href="https://usebubbles.com"><img alt="Bubbles" src="https://uploads-ssl.webflow.com/5d682047c28b217055606673/5e5360be16879c1d0dca6514_icon-thin-128x128%402x.png" height="70" /></a> |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [Bubbles](https://usebubbles.com)<br><strong>Twitter</strong>: [@use_bubbles](https://twitter.com/use_bubbles) |

[Become a backer](https://www.patreon.com/bePatron?u=11315442) and get your name, avatar, and Twitter handle listed here.
### Patreon

<a href="https://www.patreon.com/bePatron?u=11315442"><img alt="Backers on Patreon" src="https://patreon-badge.herokuapp.com/11315442.png" width="500" /></a>
<a href="https://www.patreon.com/bePatron?u=11315442"><img alt="Patrons on Patreon" src="https://img.shields.io/endpoint.svg?url=https://shieldsio-patreon.herokuapp.com/wessberg" width="200" /></a>

<!-- SHADOW_SECTION_BACKERS_END -->

Expand Down Expand Up @@ -284,7 +352,7 @@ Will be compiled into:
// ...
container.registerSingleton(undefined, {
identifier: "MyInterface",
implementation: MyImplementation
implementation: MyImplementation,
});
```

Expand Down
93 changes: 63 additions & 30 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@
"version": "2.0.5",
"description": "A Custom Transformer for Typescript that enables compile-time Dependency Injection",
"scripts": {
"generate:readme": "scaffold readme --yes",
"generate:license": "scaffold license --yes",
"generate:contributing": "scaffold contributing --yes",
"generate:coc": "scaffold coc --yes",
"generate:scaffold": "scaffold all --yes",
"generate:changelog": "standard-changelog --first-release",
"generate:all": "npm run generate:license && npm run generate:contributing && npm run generate:coc && npm run generate:readme && npm run generate:changelog",
"clean:dist": "rm -rf dist",
"clean": "npm run clean:dist",
"lint": "tsc --noEmit && tslint -c tslint.json --project tsconfig.json",
"prettier": "prettier --write '{src,documentation}/**/*.{js,ts,json,html,xml,css,md}'",
"prebuild": "npm run clean:dist",
"build": "npm run rollup",
"watch": "npm run rollup -- --watch",
"generate:all": "pnpm run generate:scaffold && pnpm run generate:changelog",
"clean:dist": "rimraf dist",
"clean": "pnpm run clean:dist",
"lint": "tsc --noEmit && eslint \"src/**/*.ts\" --color",
"prettier": "prettier --write '{src,test,documentation}/**/*.{js,ts,json,html,xml,css,md}'",
"test": "ava",
"prebuild": "pnpm run clean:dist",
"build": "pnpm run rollup",
"prewatch": "pnpm run clean:dist",
"watch": "pnpm run rollup -- --watch",
"rollup": "rollup -c rollup.config.js",
"preversion": "npm run lint && NODE_ENV=production npm run build",
"version": "npm run generate:all && git add .",
"preversion": "pnpm run lint && NODE_ENV=production pnpm run build",
"version": "pnpm run generate:all && git add .",
"release": "np --no-cleanup --no-yarn"
},
"keywords": [
Expand All @@ -35,7 +34,8 @@
"compiler"
],
"files": [
"dist/**/*.*"
"dist/**/*.*",
"register/*.*"
],
"contributors": [
{
Expand All @@ -50,20 +50,37 @@
],
"license": "MIT",
"devDependencies": {
"@wessberg/scaffold": "^1.0.18",
"@wessberg/ts-config": "^0.0.41",
"@wessberg/rollup-plugin-ts": "1.1.57",
"standard-changelog": "2.0.11",
"rollup": "1.15.6",
"tslint": "5.17.0",
"prettier": "1.18.2",
"pretty-quick": "1.11.1",
"husky": "2.4.1",
"np": "5.0.3"
"@types/prettier": "^2.0.1",
"@types/node": "^14.0.5",
"@typescript-eslint/eslint-plugin": "^3.0.1",
"@typescript-eslint/parser": "^3.0.1",
"@wessberg/rollup-plugin-ts": "^1.2.24",
"@wessberg/scaffold": "^1.0.29",
"@wessberg/di": "^2.0.3",
"@wessberg/ts-config": "^1.0.10",
"ava": "^3.8.2",
"eslint": "^7.1.0",
"slash": "^3.0.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-jsdoc": "^25.4.2",
"husky": "^4.2.5",
"np": "^6.2.3",
"pnpm": "^4.14.4",
"prettier": "^2.0.5",
"pretty-quick": "^2.0.1",
"rimraf": "^3.0.2",
"rollup": "^2.10.9",
"standard-changelog": "^2.0.24",
"ts-node": "^8.10.1",
"typescript": "^3.9.3"
},
"dependencies": {
"@types/node": "^12.0.8",
"typescript": "^3.5.2"
"@wessberg/ts-evaluator": "^0.0.25",
"tslib": "^2.0.0"
},
"peerDependencies": {
"typescript": "^3.x"
},
"repository": {
"type": "git",
Expand All @@ -72,18 +89,34 @@
"bugs": {
"url": "https://github.com/wessberg/di-compiler/issues"
},
"engines": {
"node": ">=9.0.0"
},
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
"browser": "./dist/esm/index.js",
"types": "./dist/esm/index.d.ts",
"typings": "./dist/esm/index.d.ts",
"es2015": "./dist/esm/index.js",
"engines": {
"node": ">=8.0.0"
},
"husky": {
"hooks": {
"pre-commit": "pretty-quick --staged"
}
},
"ava": {
"files": [
"test/**.test.ts"
],
"verbose": true,
"timeout": "40s",
"extensions": [
"ts"
],
"environmentVariables": {
"NODE_OPTIONS": "--max_old_space_size=4096"
},
"require": [
"ts-node/register/transpile-only"
]
}
}
Loading

0 comments on commit bfeedef

Please sign in to comment.