Prerequisitesโ
- Node.js version 18 or above
- TypeScript version 5.0 โ 5.4
Installationโ
- yarn
- npm
yarn add @clawject/di
npm install @clawject/di
Remember to install language service plugin to get support for Clawject features right in your code editor!
tsconfig.jsonโ
Firstly, you need to define custom transformers in your tsconfig.json.
@clawject/di/transformer/metadata
is optional, it's used to generate metadata for your configuration classes which allows you to share configuration via npm packages.
Visit sharing configurations section to learn more about it.
{
"compilerOptions": {
"plugins": [
{ "transform": "@clawject/di/transformer" },
{
"transform": "@clawject/di/transformer/metadata",
"afterDeclarations": true
}
]
}
}
ts-patchโ
Patches typescript to allow custom transformers (plugins) during build.
ts-patch is the best way to utilize Clawject features. It allows you to use Clawject with any build tool that uses typescript compiler.
For detailed ts-patch configuration guide - please refer to ts-patch documentation.
ts-patch, webpack, ts-loaderโ
When using webpack with ts-loader - make sure transpileOnly mode is disabled. Basically, transpileOnly disables the ability to perform static type checking, which is required for Clawject to work. Also, if you're using Babel - please make sure that it's applied after ts-loader. ts-loader#transpileOnly
import { ClawjectWebpackPlugin } from '@clawject/di/webpack';
export default {
module: {
rules: [{
test: /\.ts$/,
loader: 'ts-loader',
options: {
// This option needed if you have typescript >= 5 and using
// live-compiler method of ts-patch
// https://github.com/nonara/ts-patch#method-1-live-compiler
compiler: 'ts-patch/compiler'
}
}]
},
plugins: [
new ClawjectWebpackPlugin()
]
};
ts-patch, vite, rollup-plugin-typescript2โ
Currently Vite or rollup-plugin-typescript2 not correctly utilizes ts-patch, it's ignoring compilation errors produced by Clawject, so it could lead to runtime errors.
import { defineConfig } from 'vite';
import typescript from 'rollup-plugin-typescript2';
export default defineConfig({
esbuild: false,
plugins: [
typescript(),
],
});
ts-patch, pure typescript (tsc)โ
To make Clawject work with pure typescript (tsc), you need to define custom transformers in your tsconfig.json, and that's all.
Clawject will work pretty well in native tsc watch mode as well!
Without ts-patchโ
If you don't want to use ts-patch for some reason - you can utilize Clawject features without it.
webpack, ts-loaderโ
import { ClawjectTransformer } from '@clawject/di/transformer';
import { ClawjectMetadataTransformer } from '@clawject/di/transformer/metadata';
import { ClawjectWebpackPlugin } from '@clawject/di/webpack'
export default {
module: {
rules: [{
test: /\.ts$/,
loader: 'ts-loader',
options: {
getCustomTransformers: (program, getProgram) => ({
before: [ClawjectTransformer(getProgram)],
afterDeclarations: [ClawjectMetadataTransformer(getProgram)]
})
}
}]
},
plugins: [
new ClawjectWebpackPlugin()
]
};