@bpmn-io/variable-resolver

中文文档
3.3.1发布于 2026/10/23MITCommonJS近 30 天下载 5万

bpmn-js plugin for handling external variable providers

原始使用文档
文档目录6 节
  1. Usage
  2. Retrieving variables
  3. Adding a variable extractor
  4. Advanced use-cases
  5. Development
  6. License

@bpmn-io/variable-resolver

CI

An extension for bpmn-js that makes the data model of the diagram available to other components.

[!NOTE] As of version v3 this library exposes both written and consumed variables, you can filter them via options.

Usage

To add the variable resolver to you application, add it to your bpmn-js configuration:

import BpmnModeler from 'bpmn-js/lib/Modeler';
import ZeebeModdle from 'zeebe-bpmn-moddle/resources/zeebe.json';

import {
  ZeebeVariableResolverModule,     // For Camunda 8 diagrams
  // CamundaVariableResolverModule // for Camunda 7 diagrams
} from '@bpmn-io/variable-resolver';

const modeler = new BpmnModeler({
  container: '#canvas',
  additionalModules: [
    ZeebeVariableResolverModule
  ],
  moddleExtensions = {
    zeebe: ZeebeModdle
  }
});

Retrieving variables

Retrieve the variables from a diagram using the variableResolver service:

const variableResolver = modeler.get('variableResolver');
const elementRegistry = modeler.get('elementRegistry');

// retrieve variables relevant to an element
const task = elementRegistry.get('Task_1');

// default: variables relevant to <task> in its visible scopes
await variableResolver.getVariablesForElement(task);

// variables read by <task> only
await variableResolver.getVariablesForElement(task, {
  read: true,
  written: false
});

// all variables written by <task>
await variableResolver.getVariablesForElement(task, { written: true, read: false });

// local variables only (scope === queried element)
await variableResolver.getVariablesForElement(task, {
  local: true,
  external: false
});

// non-local variables only (scope !== queried element)
await variableResolver.getVariablesForElement(task, {
  local: false,
  external: true
});

// retrieve all variables defined in a process
const processElement = elementRegistry.get('Process_1');

// returns all variables for the process (unfiltered), for local processing
await variableResolver.getProcessVariables(processElement);

getVariablesForElement(element, options) supports five filter switches:

OptionDefaultDescription
readtrueInclude variables consumed by the queried element
writtentrueInclude variables written/created by the queried element
localtrueInclude variables local to the queried element scope
externaltrueInclude variables outside the queried element scope
outputMappingstrueCount output-mapping reads as reads

Adding a variable extractor

To add your own variables, extend the variableProvider class in your extension. It only needs to implement the getVariables method, which takes an element as an argument and returns an array of variables you want to add to the scope of the element. The function can be asynchronous.

// MyExtension.js
import VariableProvider from '@bpmn-io/variable-resolver/lib/VariableProvider';

class MyCustomProvider extends VariableProvider {
  getVariables(element) {
    const variables = [
      {
        name: 'myVariable',
        type: 'String',
        info: 'This is a global variable'
      }
    ];
      if (is(element, 'bpmn:Process')) {
      return variables;
    }
  }
}

export const MyExtension = {
  __init__: [
    'myCustomProvider',
  ],
  myCustomProvider: [ 'type', MyCustomProvider ],
};

Advanced use-cases

By default, getVariablesForElement and getProcessVariables merge variables with the same name and scope into one - entry structure and types are mixed in the merged representation.

Merged variables additionally expose variants, one per writing element. Each variant is shaped like a variable and describes only the value contributed by its single origin:

/**
 * variable = {
 *   name: 'myVariable',
 *   type: 'Number|String',
 *   origin: [ 'Task_1', 'Task_2' ],
 *   variants: [
 *     { name: 'myVariable', type: 'String', origin: [ 'Task_1' ], ... },
 *     { name: 'myVariable', type: 'Number', origin: [ 'Task_2' ], ... }
 *   ],
 *   ...
 * }
 */

In some cases, you might want access to the raw data, e.g. to run lint rules to detect potential schema mismatches between providers. For this, you can use getRawVariables:

const variableResolver = modeler.get('variableResolver');

const raw = await variableResolver.getRawVariables();

/**
 * raw = {
 *  'Process_1': [
 *    {
 *     name: 'myVariable',
 *     type: 'String',
 *     scope: 'Process_1',
 *     origin: [ 'Task_1' ]
 *    },
 *    {
 *     name: 'myVariable',
 *     type: 'Number',  // Same Variable with different types
 *     scope: 'Process_1',
 *     origin: [ 'Task_2' ]
 *    }
 *  ]
 * }
 */

Development

Prepare the project by installing all dependencies:

npm install

Then, start the example with:

npm start

License

MIT

版本与发布记录

@bpmn-io/variable-resolver 共发布 33 个版本,首次发布于 2023/02/21;过去 12 个月发布了 16 个版本。

版本发布日期距今
3.3.1latest2026/10/2315 天前
3.3.02026/07/213 个月前
3.2.02026/07/014 个月前
3.1.02026/06/304 个月前
3.0.12026/04/246 个月前
3.0.02026/03/237 个月前

关于 @bpmn-io/variable-resolver 的常见问题

@bpmn-io/variable-resolver 是什么?

bpmn-js plugin for handling external variable providers

如何安装 @bpmn-io/variable-resolver?

在项目目录运行 npm install @bpmn-io/variable-resolver,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:bpmn-js(*)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@bpmn-io/variable-resolver 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/bpmn-io__variable-resolver 是否存在。

@bpmn-io/variable-resolver 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@bpmn-io/variable-resolver 可以商用吗?

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

@bpmn-io/variable-resolver 的下载量有多少?

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

@bpmn-io/variable-resolver 还在维护吗?

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