@dunx/transform

中文文档
3.10.6发布于 2026/10/30MITTypeScriptES Module构建来源已验证近 30 天下载 3230

Load-time transform that records constructor dependencies for the dunx container

原始使用文档
文档目录6 节
  1. Install
  2. Setup
  3. Publishing a package
  4. What it does
  5. Parameters it will not guess
  6. If you forget to register it

@dunx/transform

Load-time transform that lets the dunx container use constructor injection.

export class UsersService {
  constructor(private readonly repo: UsersRepository) {}
}

No @Injectable, no @Inject, no reflect-metadata, no experimentalDecorators. The Providers guide is canonical.

Install

bun add @dunx/transform

Setup

# bunfig.toml
preload = ["@dunx/transform/preload"]

[test]
preload = ["@dunx/transform/preload"]

Or with no config file at all:

bun --preload @dunx/transform/preload src/main.ts

For a production build, pass the plugin to Bun.build:

import { depsPlugin } from '@dunx/transform';

await Bun.build({ entrypoints: ['src/main.ts'], plugins: [depsPlugin] });

Publishing a package

The preload skips node_modules, so a package on npm has to carry its own records or nothing can inject its classes. @dunx/transform/build does that build, reading entrypoints from your exports and bin:

// build.ts
import { buildPackage } from '@dunx/transform/build';

await buildPackage();

It assumes sources under src/, output to dist/, and one tsconfig.json at the package root. Declarations come from typescript, an optional peer, because Bun emits none. The full walkthrough is in the publishing guide.

What it does

It parses each file with oxc-parser and reads every class declaration's constructor parameter types. Then it appends one statement per class:

Object.defineProperty(UsersService, Symbol.for('dunx.deps'), {
  value: () => [UsersRepository],
});

@dunx/core reads that record and resolves the arguments before calling new.

The record is a thunk, a function that returns the parameter types. The container calls it when it resolves the class, not when the file loads. So a dependency declared later in the file, or reached through a circular import, needs no forwardRef.

Only the appended statement is added. Every other byte of the original source is preserved, so comments, formatting, and the line numbers in stack traces are unchanged.

Parameters it will not guess

A parameter whose type names nothing that exists at runtime is recorded as unresolved, together with its original source text. The container then throws at boot:

UsersService cannot be constructed: parameter 2 (private readonly cfg: AppConfig)
names nothing that exists at runtime, so there is no token to resolve. Replace
the type with an abstract class, or bind it with token() and read it with
inject(TOKEN) in a field initializer.

This error covers a type-only import, an inline type specifier, a local interface or type alias, a class type parameter, a primitive and a union. For a type-only import, the error tells you to use a value import instead. For the rest, use an abstract class or a token(). A token has no type to put on a parameter, so read it with inject(TOKEN) in a field initializer.

Class expressions are skipped: const X = class Inner {} binds Inner only inside the class body, so a statement appended after it could not reference the name.

If you forget to register it

The container checks ctor.length against the recorded dependencies, so a class the plugin never saw is a boot error rather than a set of undefined fields:

UsersController declares 1 constructor parameter(s) but no dependencies were
recorded for it, so @dunx/transform did not transform UsersController.

版本与发布记录

@dunx/transform 共发布 83 个版本,首次发布于 2026/08/02;过去 12 个月发布了 83 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.10.6latest2026/10/3014 天前
3.10.52026/10/2222 天前
3.10.42026/10/2024 天前
3.10.32026/10/1430 天前
3.10.22026/10/081 个月前
3.10.12026/09/261 个月前

关于 @dunx/transform 的常见问题

@dunx/transform 是什么?

Load-time transform that records constructor dependencies for the dunx container

如何安装 @dunx/transform?

在项目目录运行 npm install @dunx/transform,或使用 pnpm add / yarn add / bun add。

@dunx/transform 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@dunx/transform 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@dunx/transform 可以商用吗?

@dunx/transform 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@dunx/transform 的下载量有多少?

根据 npm 官方下载统计,@dunx/transform 在 2026/10/13 – 2026/11/11 这 30 天内被下载 3,230 次,日均约 108 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@dunx/transform 还在维护吗?

最新版本 3.10.6 发布于 2026/10/30(14 天前);至今共发布 83 个版本,其中过去 12 个月发布了 83 个;首次发布于 2026/08/02。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。