Skip to content

Commit

Permalink
feat: make compatible with TypeScript v5.6 and Node >= 18.20, includi…
Browse files Browse the repository at this point in the history
…ng the new loader mechanism
  • Loading branch information
wessberg committed Oct 1, 2024
1 parent c502c7d commit 9e5fe79
Show file tree
Hide file tree
Showing 17 changed files with 3,024 additions and 4,733 deletions.
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 © 2023 [Frederik Wessberg](mailto:frederikwessberg@hotmail.com) ([@FredWessberg](https://twitter.com/FredWessberg)) ([Website](https://github.com/wessberg))
Copyright © 2024 [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
126 changes: 12 additions & 114 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ so that it works most efficiently.

## Backers

| <a href="https://changelog.me"><img alt="Trent Raymond" src="https://avatars.githubusercontent.com/u/1509616?v=4" height="70" /></a> | <a href="https://scrubtheweb.com/computers/programming/1"><img alt="scrubtheweb" src="https://avatars.githubusercontent.com/u/41668218?v=4" height="70" /></a> |
| -------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [Trent Raymond](https://changelog.me) | [scrubtheweb](https://scrubtheweb.com/computers/programming/1) |

### Patreon

<a href="https://www.patreon.com/bePatron?u=11315442"><img alt="Patrons on Patreon" src="https://img.shields.io/endpoint.svg?url=https%3A%2F%2Fshieldsio-patreon.vercel.app%2Fapi%3Fusername%3Dwessberg%26type%3Dpatrons" width="200" /></a>
Expand Down Expand Up @@ -89,14 +85,11 @@ so that it works most efficiently.
- [Usage as a TypeScript Custom Transformer](#usage-as-a-typescript-custom-transformer)
- [Usage with TypeScript's Compiler APIs](#usage-with-typescripts-compiler-apis)
- [Usage with ts-nodes programmatic API](#usage-with-ts-nodes-programmatic-api)
- [Usage with ttypescript](#usage-with-ttypescript)
- [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 @rollup/plugin-typescript](#usage-with-rollupplugin-typescript)
- [Usage with Webpack](#usage-with-webpack)
- [Usage with awesome-typescript-loader](#usage-with-awesome-typescript-loader)
- [Usage with ts-loader](#usage-with-ts-loader)
- [Usage with ava](#usage-with-ava)
- [Options](#options)
- [Optimization](#optimization)
- [Optimization 1: Activate `preserveValueImports` in your tsconfig CompilerOptions](#optimization-1-activate-preservevalueimports-in-your-tsconfig-compileroptions)
Expand Down Expand Up @@ -136,6 +129,8 @@ $ pnpm add @wessberg/di-compiler

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

You may also need to install `pirates` depending on the features you are going to use. Refer to the documentation for the specific cases where it may be relevant.

<!-- SHADOW_SECTION_INSTALL_END -->

<!-- SHADOW_SECTION_USAGE_START -->
Expand Down Expand Up @@ -219,27 +214,27 @@ A very convenient way to use `DI-Compiler` is as a loader directly with Node.js.
If your codebase is based on **native ESM**, and **if you use Node.js v.18.6.0 or newer**, pass it as a loader via the command line

```
node --loader @wessberg/di-compiler/loader
node --import @wessberg/di-compiler/loader
```

This is not enough on its own to teach Node.js to understand TypeScript syntax, so you'll still need to couple it with a loader like [`ts-node`](https://github.com/TypeStrong/ts-node), [`tsx`](https://github.com/esbuild-kit/tsx) or [`esm-loader`](https://github.com/esbuild-kit/esm-loader).

For example, here's how to use it with the native ESM loader for [`ts-node`](https://github.com/TypeStrong/ts-node):

```
node --loader @wessberg/di-compiler/loader --loader ts-node/esm
node --import @wessberg/di-compiler/loader --import ts-node/esm
```

And, here's how to use it with [`tsx`](https://github.com/esbuild-kit/tsx):

```
node --loader @wessberg/di-compiler/loader --loader tsx
node --import @wessberg/di-compiler/loader --import tsx
```

Finally, here's how you can use it with [`esm-loader`](https://github.com/esbuild-kit/esm-loader):

```
node --loader @wessberg/di-compiler/loader --loader @esbuild-kit/esm-loader
node --import @wessberg/di-compiler/loader --import @esbuild-kit/esm-loader
```

Alternatively, if you don't use ESM in your project, or if you're running an older version of Node.js, DI-Compiler can be used as a loader too.
Expand All @@ -251,14 +246,6 @@ node -r @wessberg/di-compiler/loader -r ts-node

> In all of the above configurations, for both ESM and CommonJS loaders, there is no TypeScript _Program_ context, nor is there a Type checker, so `DI-Compiler` will attempt to determinate programmatically whether or not the identifiers across your files reference instances of `DIContainer` or not, by performing partial evaluation on compile time. Please see the [Optimization](#optimization) section for details on how this process can be optimized.
A convenience loader is exposed that combines `ts-node` and DI-Compiler in a single loader for CommonJS projects, which also exposes a TypeScript Program to DI-Compiler, which is the most robust way to use it as a loader, at the expense of slower performance:

```
node -r @wessberg/di-compiler/ts-node-loader
```

> Note: You must install ts-node yourself for this configuration to work, as it is not listed as a dependency of DI-Compiler.
### Loader SourceMaps

By default, SourceMaps will be generated and inlined inside the loaded modules if the `sourceMap` option is `true` inside the resolved tsconfig.
Expand Down Expand Up @@ -303,7 +290,6 @@ A few examples of ways to use DI-Compiler as a Custom Transformer include:

- [With TypeScript's Compiler APIs](#usage-with-typescripts-compiler-apis)
- [With ts-nodes programmatic API](#usage-with-ts-nodes-programmatic-api)
- [With ttypescript](#usage-with-ttypescript)
- [With Rollup](#usage-with-rollup)

### Usage with TypeScript's Compiler APIs
Expand Down Expand Up @@ -336,63 +322,16 @@ require("ts-node").register({
});
```

### Usage with ttypescript

To use DI-compiler with [`ttypescript`](https://github.com/cevek/ttypescript), create a file that wraps the invocation of `di`:

```typescript:transformer.ts
import type { Program } from 'typescript'
import { di } from "@wessberg/di-compiler";

const transformer = (program: Program) => di({ program })

export default transformer
```

Then add a record to the `plugins` array of your `tsconfig.json` that maps a key named `transform` to the relative path to the file you just created:

```json
{
"compilerOptions": {
"plugins": [{"transform": "path/to/transformer.ts"}]
}
}
```

### Usage with Rollup

There are a few TypeScript plugins for Rollup that support Custom Transformers, and DI-Compiler can be easily integrated with them:

- [Usage with rollup-plugin-ts](#usage-with-rollup-plugin-ts)
- [Usage with rollup-plugin-typescript2](#usage-with-rollup-plugin-typescript2)

#### Usage with rollup-plugin-ts

To use DI-Compiler with [rollup-plugin-ts](https://github.com/wessberg/rollup-plugin-ts), all you have to do is pass it to the list of transformers given as a plugin option:

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

export default {
input: "...",
output: [
/* ... */
],
plugins: [
ts({
transformers: [di]
})
]
};
```
There are a few TypeScript plugins for Rollup that support Custom Transformers, and DI-Compiler can be easily integrated with them.

#### Usage with rollup-plugin-typescript2
#### Usage with @rollup/plugin-typescript

Here's how you may integrate DI-Compiler with [rollup-plugin-typescript2](https://github.com/ezolenko/rollup-plugin-typescript2):
Here's how you may integrate DI-Compiler with [@rollup/plugin-typescript](https://www.npmjs.com/package/@rollup/plugin-typescript):

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

export default {
Expand All @@ -402,7 +341,7 @@ export default {
],
plugins: [
ts({
transformers: [service => di({program: service.getProgram()})]
transformers: program => di({program})
})
]
};
Expand Down Expand Up @@ -465,47 +404,6 @@ const config = {
};
```

### Usage with ava

You can also use DI-compiler with the [`ava`](https://github.com/avajs/ava) test runner by using DI-Compiler as a loader.
See [this section](#usage-as-a-nodejs-loader) for more details on how to configure it.

For a CommonJS project, you can use the `require` property in the `ava` configuration. For example:

```js
{
"ava": {
// Other options...
"extensions": ["ts"],
"require": [
"@wessberg/di-compiler/loader",
// For teaching Node.js about TypeScript specific syntax and extensions
"ts-node"
]
}
}
```

Whereas for an ESM project, the syntax is a little different:

```js
{
"ava": {
// Other options...
"extensions": {
"ts": "module"
},
"nodeArguments": [
"--loader=@wessberg/di-compiler/loader",
// For teaching Node.js about TypeScript specific syntax and extensions.
"--loader=ts-node/esm"
]
}
}
```

> Note, we use ts-node in this example, but we could have used other tools like tsx or esm-loader just as well.
## Options

The [`transform`](#usage-as-an-api) function, as well as the [`di` Custom Transformer](#usage-as-a-typescript-custom-transformer) takes
Expand Down
3 changes: 1 addition & 2 deletions loader.cjs
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
// eslint-disable-next-line @typescript-eslint/no-require-imports
require("./dist/cjs/loader.cjs");
require("./dist/loader/cjs/loader.cjs");
57 changes: 23 additions & 34 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
"prettier": "prettier --write \"{src,test,documentation}/**/*.{js,ts,json,html,xml,css,md}\"",
"test": "node --import tsx --test \"./test/**/*.test.ts\"",
"prebuild": "pnpm run clean",
"build": "tsup --entry \"src/index.ts\" --entry \"src/loader/esm.ts\" --sourcemap --dts --format cjs,esm",
"build": "pnpm run clean && pnpm run build:esm && pnpm run build:cjs",
"build:cjs": "tsup --entry=\"src/index.ts\" --entry=\"src/loader/esm/loader.ts\" --entry=\"src/loader/esm/hooks/index.ts\" --sourcemap --dts --format esm",
"build:esm": "tsup --entry=\"src/index.ts\" --entry=\"src/loader/cjs/loader.ts\" --sourcemap --splitting --dts --format cjs",
"preversion": "pnpm run lint && pnpm run build",
"version": "pnpm run preversion && pnpm run generate:all && git add .",
"release": "np --no-cleanup --no-yarn",
Expand All @@ -33,8 +35,7 @@
],
"files": [
"dist/**/*.*",
"loader.*",
"ts-node-loader.*"
"loader.*"
],
"contributors": [
{
Expand All @@ -52,17 +53,15 @@
"@types/node": "^22.7.1",
"@types/prettier": "^3.0.0",
"@types/semver": "^7.5.8",
"@typescript-eslint/eslint-plugin": "^8.7.0",
"@typescript-eslint/parser": "^8.7.0",
"eslint-plugin-deprecation": "3.0.0",
"@wessberg/ts-config": "^5.0.17",
"@wessberg/prettier-config": "^1.0.0",
"@prettier/sync": "0.5.2",
"semver": "^7.6.3",
"eslint": "^9.11.1",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "^2.30.0",
"eslint-plugin-jsdoc": "^50.2.4",
"eslint-plugin-prettier": "^5.2.1",
"typescript-eslint": "^8.7.0",
"semver": "^7.6.3",
"husky": "^9.1.6",
"np": "^10.0.7",
"pnpm": "^9.11.0",
Expand Down Expand Up @@ -102,13 +101,18 @@
"dependencies": {
"crosspath": "^2.0.0",
"helpertypes": "^0.0.19",
"pirates": "^4.0.6",
"get-tsconfig": "^4.8.1",
"ts-evaluator": "^1.2.0",
"compatfactory": "^4.0.2"
},
"peerDependencies": {
"typescript": ">=3.x || >= 4.x || >= 5.x"
"typescript": ">=3.x || >= 4.x || >= 5.x",
"pirates": ">=4.x"
},
"peerDependenciesMeta": {
"pirates": {
"optional": true
}
},
"repository": {
"type": "git",
Expand All @@ -119,38 +123,23 @@
},
"exports": {
"./loader": {
"import": "./dist/esm/loader.js",
"require": "./dist/cjs/loader.cjs"
},
"./ts-node-loader": {
"require": "./ts-node-loader.cjs"
"import": "./dist/loader/esm/loader.js",
"require": "./dist/loader/cjs/loader.cjs"
},
".": {
"import": "./dist/esm/index.js",
"require": "./dist/cjs/index.cjs",
"types": "./dist/esm/index.d.ts"
"import": "./dist/index.js",
"require": "./dist/index.cjs"
}
},
"type": "module",
"types": "./dist/esm/index.d.ts",
"main": "./dist/cjs/index.cjs",
"module": "./dist/esm/index.js",
"types": "./dist/index.d.ts",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"engines": {
"node": ">=16.15.1"
"node": ">=18.20.0"
},
"lint-staged": {
"*": "prettier --ignore-unknown --write"
},
"prettier": "@wessberg/prettier-config",
"ava": {
"files": [
"test/**/*.test.ts"
],
"verbose": true,
"timeout": "400s",
"concurrency": 1,
"extensions": {
"ts": "module"
}
}
"prettier": "@wessberg/prettier-config"
}
Loading

0 comments on commit 9e5fe79

Please sign in to comment.