@hlang-tech/compiler

中文文档
2.0.1发布于 2026/11/01MITTypeScriptESM + CJS

Ahead-of-time compiler for hlang: compiles an IGraph program into a standalone ES module.

原始使用文档
文档目录8 节
  1. Install
  2. Usage
  3. Generated module
  4. API
  5. compile(graph, options)
  6. Errors
  7. Migrating from 1.x
  8. License

hlang compiler

@hlang-tech/compiler

Compiles an hlang program (IGraph) ahead of time into a standalone ES module. The module runs the graph with the same behavior as @hlang-tech/interpreter, without the interpreter or the graph JSON at run time.

npm license

Install

npm install -D @hlang-tech/compiler

The same build is published as @hlang-org/compiler. ESM and CommonJS builds with type declarations.

Usage

import { writeFile } from 'node:fs/promises';
import { compile } from '@hlang-tech/compiler';

const { code, imports } = compile(graph, {
  nodes: {
    numbers: { from: './nodes/numbers.js' },             // default export
    double: { from: '@acme/hlang-nodes', name: 'Double' }, // named export
    log: { from: './nodes/log.js' },
  },
});

await writeFile('program.mjs', code);
// `imports` lists every module the program imports, for example
// ['@hlang-tech/runtime', 'rxjs', './nodes/numbers.js', '@acme/hlang-nodes', './nodes/log.js'].

Run the compiled program:

import { run } from './program.mjs';

const handle = run({ env: 'prod', onError: ({ nodeId, error }) => console.error(nodeId, error) });
handle.nodes.numbers; // the constructed node
handle.dispose();     // stop long-running nodes

compile is a pure function. It writes no files and installs nothing.

Generated module

compile validates the graph with planGraph from @hlang-tech/ir, builds an ESTree program, and prints it with astring. Values are emitted as literals by the printer, so the output is valid JavaScript for any node id or parameter. The same graph always produces the same text.

// Generated by @hlang-tech/compiler. Do not edit.
import {Flow} from "@hlang-tech/runtime";
import {Subject} from "rxjs";
import Node0 from "./nodes/numbers.js";
// ...
export function run(options = {}) {
  const flowId = options.flowId ?? "flow-" + Date.now().toString(36);
  // ...
  const nodes = {
    numbers: new Node0({ nodeId: "numbers", flowId, env, middleware, config: { params: {} }, signal }),
    // ...
  };
  nodes["numbers"].O("out").connect(nodes["double"].I("in"));
  const flow = new Flow();
  flow.run(nodes["numbers"]);
  return { flowId, nodes, dispose: () => { /* ... */ } };
}

run(options) accepts flowId, env (default 'prod'), middleware, and onError. It returns { flowId, nodes, dispose }.

API

compile(graph, options)

OptionDescription
nodesRequired. Maps each node code to { from, name? }: the module specifier and the export name (default export when name is omitted).
runtimeModule that exports Flow. Default '@hlang-tech/runtime'.
rxjsModule that exports Subject. Default 'rxjs'.

Returns { code, imports }.

Errors

  • GraphError (re-exported from @hlang-tech/ir) for an invalid graph.
  • CompileError with code:
    • unresolved-node: a node code has no entry in options.nodes.
    • invalid-import: an export name is not a valid identifier.
    • unserializable-config: a bizData value is not JSON (for example undefined, NaN, a function, or a Date). The message names the path.

Migrating from 1.x

Version 2 is a rewrite. 1.x remains on npm, and ^1.0.0 ranges do not resolve to 2.x.

1.x2.x
Default export build(graph) wrote a project to the temp directory and ran npm install.compile(graph, options) returns { code, imports }. Write and install them yourself.
Node modules came from each node's runtimeResource.Map each code in options.nodes.
The output depended on @hset/xlang-runtime-middleware, lodash, and child-process IPC.The output depends only on the runtime, RxJS, and your node modules. Pass services as middleware.
complier export.Removed. Use compile.

Source and documentation: github.com/hlang-tech/hlang.

License

MIT

版本与发布记录

@hlang-tech/compiler 共发布 3 个版本,首次发布于 2026/11/01;过去 12 个月发布了 3 个版本。

版本发布日期距今
2.0.1latest2026/11/012 天前
2.0.02026/11/012 天前
0.0.0-stage2026/11/012 天前

关于 @hlang-tech/compiler 的常见问题

@hlang-tech/compiler 是什么?

用于hlang的预先编译器:将IGraph程序编译成独立的ES模块。

如何安装 @hlang-tech/compiler?

在项目目录运行 npm install @hlang-tech/compiler,或使用 pnpm add / yarn add / bun add。

@hlang-tech/compiler 支持 TypeScript 吗?

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

@hlang-tech/compiler 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@hlang-tech/compiler 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@hlang-tech/compiler 可以商用吗?

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

@hlang-tech/compiler 还在维护吗?

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