# @exodus/dependency-preprocessors 中文文档

规范地址：https://npmcn.com/package/%40exodus/dependency-preprocessors/

版本：6\.3\.4
npm 元数据更新时间：2026-10-08T20:38:11.579Z
中文译文更新时间：2026-11-14T04:08:09.034Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40exodus/dependency-preprocessors
原文对照：https://npmcn.com/package/%40exodus/dependency-preprocessors/?lang=original
项目主页：https://github.com/ExodusMovement/exodus/tree/master/libs/libraries/dependency-preprocessors
源码仓库：https://github.com/ExodusMovement/exodus

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：56.1 kB，34 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- 运行依赖：6 个
- 首次发布：2023-01-24T21:59:33.351Z
- 最新版本发布：2026-10-08T20:38:11.401Z
- 近 30 天下载量：10982（2026-10-14 至 2026-11-12，npm 官方下载统计）
- 已发布版本数：42（近 12 个月 2 个）

## 中文简介

依赖性预处理器,以便在将依赖性定义传递到@exodus/dependency-注入之前,使依赖性定义得到声明性增强

## 安装

```sh
npm install @exodus/dependency-preprocessors
```

也可以使用：`pnpm add @exodus/dependency-preprocessors`、`yarn add @exodus/dependency-preprocessors`、`bun add @exodus/dependency-preprocessors`

## README

# @ exodus/ 依赖性预处理器

IOC 预处理器功能

## 使用量

运行内置和自定义预处理器如下:

```js
import createIocContainer from '@exodus/dependency-injection'
import preprocess from '@exodus/dependency-preprocessors'
import { logify, config, alias, namespaceStorage } from '@exodus/dependency-preprocessors'

const createLogger =
  (namespace) =>
  (...args) =>
    console.log(namespace, ...args)

const ioc = createIocContainer({ logger: createLogger('exodus:ioc') })

const customPreprocessor = ({ definition, ...extras }) => ({ definition, ...extras })
const deps = preprocess({
  dependencies: createDependencies({ adapters, config }),
  preprocessors: [
    //
    logify({ createLogger }),
    config(),
    alias(),
    namespaceStorage(),
    customPreprocessor,
  ],
})

ioc.registerMultiple(deps)
ioc.resolve()
```

## 预处理器

### 登录

通过一个命名空格日志实例

```js
import createIocContainer from '@exodus/dependency-injection'
import preprocess from '@exodus/dependency-preprocessors'

const createLogger =
  (namespace) =>
  (...args) =>
    console.log(namespace, ...args)

const ioc = createIocContainer({ logger: createLogger('exodus:ioc') })

const deps = preprocess({
  dependencies: [
    {
      definition: {
        id: 'myModule',
        factory: ({ logger }) => logger.warn('sync finished'), // Logs will be prefixed with [myModule:warn]
      },
    },
  ],
  preprocessors: [logify({ createLogger })],
})

ioc.registerMultiple(deps)
ioc.resolve()
```

### 配置

执行全局配置自动绑定给定的模块代号, 或从节点注入, 如果该节点上定义了“ 配置 ” .

```js
import createIocContainer from '@exodus/dependency-injection'
import preprocess from '@exodus/dependency-preprocessors'
import { config } from '@exodus/dependency-preprocessors'

const createLogger =
  (namespace) =>
  (...args) =>
    console.log(namespace, ...args)

const ioc = createIocContainer({ logger: createLogger('exodus:ioc') })

const deps = preprocess({
  dependencies: [
    {
      definition: {
        id: 'config',
        factory: () => ({ myModule: { apiUrl: 'https://exodus.com' } }),
      },
    },
    {
      definition: {
        id: 'myModule',
        factory: ({ config }) => config.apiUrl,
        dependencies: ['config'],
      },
    },
    {
      definition: {
        id: 'nodeInjected',
        factory: ({ config }) => config.potter.spells, // [lumos]
        dependencies: ['config'],
      },
      config: {
        potter: {
          spells: ['lumos'],
        },
      },
    },
  ],
  preprocessors: [config()],
})

ioc.registerMultiple(deps)
ioc.resolve()
```

### 别名

别名注入依赖, 例如您可以注入全局依赖 `myMobileImplementationOfSomething` 作为本地命名选项 `something` 到一个模块.

见 [实例](https://npmcn.com/package/%40exodus/dependency-preprocessors/__tests__/examples/voldemort-roulette.test.ts)。 。 。 .

### 命名空间集

指定您依赖的存储位置 .

见 [实例](https://npmcn.com/package/%40exodus/dependency-preprocessors/__tests__/examples/namespace-storage.test.ts)。 。 。 .

### 仅读原子

使所有原子只读, 除非配置为可写入

```js
import { readOnlyAtoms } from '@exodus/dependency-preprocessors'

const deps = preprocess({
  dependencies: [
    {
      definition: {
        id: 'balances',
        factory: () => ({ myModule: { apiUrl: 'https://exodus.com' } }),
        dependencies: ['currencyAtom', 'balancesAtom'],
      },
    },
  ],
  preprocessors: [readOnlyAtoms()],
})
```

警告你们,不要抛下它 `warn` 旗为 `true` 并注入一个日志存储器 :

```js
import { readOnlyAtoms } from '@exodus/dependency-preprocessors'

const deps = preprocess({
  // ...
  preprocessors: [readOnlyAtoms({ warn: true, logger: createLogger('readOnlyAtoms') })],
})
```

### 选项

允许有条件地使用 `if` 属性 :

```js
import { optional } from '@exodus/dependency-preprocessors'

const deps = preprocess({
  dependencies: [
    {
      if: ENABLE_OPTIMISTIC_ACTIVITY,
      definition: {
        id: 'optmisticBalances',
        factory: () => ({ myModule: { apiUrl: 'https://exodus.com' } }),
        dependencies: ['balancesAtom'],
      },
    },
  ],
  preprocessors: [optional()],
})
```

### devMode原子

帮助检测各种众所周知的原子问题

```js
import { devModeAtoms } from '@exodus/dependency-preprocessors'

const deps = preprocess({
  dependencies,
  preprocessors: [
    devModeAtoms({
      logger,
      // throw when observers seem to hang
      timeoutObservers: {
        delay: 1000,
      },
      // warn is set() is called with the same value as the current value
      warnOnSameValueSet: true,
      // warn when observers throw/reject instead of crashing set()
      swallowObserverErrors: true,
    }),
  ],
})
```

### 性能监视器

环绕方法在一个通知超过配置阈值的方法的执行时间的代理中向IOC节点调用.

```js
import { performanceMonitor } from '@exodus/dependency-preprocessors'

const deps = preprocess({
  dependencies,
  preprocessors: [
    performanceMonitor({
      now: performance.now, // default
      onAboveThreshold: ({ id, method, duration }) => {
        console.log(`[exodus:${id}:perf]`, `${method} took ${duration}ms`)
      },
      config: {
        threshold: 50, // default, logs any method calls taking longer than 50ms
      },
    }),
  ],
})
```

## 常见问题

### @exodus/dependency-preprocessors 是什么？

依赖性预处理器,以便在将依赖性定义传递到@exodus/dependency-注入之前,使依赖性定义得到声明性增强

### 如何安装 @exodus/dependency-preprocessors？

在项目目录运行 npm install @exodus/dependency-preprocessors，或使用 pnpm add / yarn add / bun add。

### @exodus/dependency-preprocessors 支持 TypeScript 吗？

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

### @exodus/dependency-preprocessors 支持 ESM 还是 CommonJS？

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

### @exodus/dependency-preprocessors 可以商用吗？

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

### @exodus/dependency-preprocessors 的下载量有多少？

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

### @exodus/dependency-preprocessors 还在维护吗？

最新版本 6\.3\.4 发布于 2026/10/09（1 个月前）；至今共发布 42 个版本，其中过去 12 个月发布了 2 个；首次发布于 2023/01/25。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

