@hlang-org编译器
编译一个hlang程序(IGraph)提前转换为独立的ES模块。模块以相同的行为运行图形 @hlang-org/interpreter,在运行时没有解释器或图形JSON。
安装
npm install -D @hlang-org/compiler
相同的构建发布为 @hlang-tech/compiler。ESM和CommonJS的构建带有类型声明。
使用
import { writeFile } from 'node:fs/promises';
import { compile } from '@hlang-org/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-org/runtime', 'rxjs', './nodes/numbers.js', '@acme/hlang-nodes', './nodes/log.js'].
运行编译后的程序:
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 是一个纯函数。它不写入任何文件,也不安装任何东西。
生成模块
compile 用验证图 planGraph 从 @hlang-org/ir,构建一个ESTree程序,并使用 astring。值是由打印机作为字面值发出的,因此输出是任何节点id或参数的有效JavaScript。同样的图形总是产生同样的文本。
// Generated by @hlang-org/compiler. Do not edit.
import {Flow} from "@hlang-org/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) 接受 flowId, env (默认 'prod'), middleware, onError. 它返回 { flowId, nodes, dispose }.
API
compile(graph, options)
| 选项 | 描述 |
|---|---|
nodes | 必需的。映射每个节点 code 来 { from, name? }:模块说明符和导出名称(默认导出时) name 省略)。 |
runtime | 导出的模块 Flow。默认的 '@hlang-org/runtime'. |
rxjs | 导出的模块 Subject。默认的 'rxjs'. |
返回 { code, imports }.
错误
GraphError(再出口@hlang-org/ir)处理无效图。CompileError与code:unresolved-node:节点code没有入口options.nodes.invalid-import:导出名称不是有效的标识符。unserializable-config:一个bizDatavalue不是JSON(例如undefined,NaN,函数,或Date). 该消息命名路径。
从1.x迁移
版本2是重写的。1.X仍然在npm上,并且 ^1.0.0 范围不能解析为2.x。
| 1.倍 | 2.倍 |
|---|---|
默认的出口 build(graph) 将一个项目写入临时目录并运行 npm install. | compile(graph, options) 返回 { code, imports }。自己编写和安装它们。 |
节点模块来自每个节点 runtimeResource. | 地图每一个 code 在 options.nodes. |
产出取决于 @hset/xlang-runtime-middlewarelodash和子进程IPC。 | 输出只依赖于运行时、RxJS和节点模块。将服务作为 middleware. |
complier 出口。 | 移除。使用 compile. |
来源和文件: github.com/hlang-tech/hlang.
许可证
麻省理工学院