Skip to content

Commit 58c923f

Browse files
committed
fix: 延迟加载
1 parent d67259b commit 58c923f

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

src/utils/flowchartBuilder.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Node, Edge } from '@vue-flow/core'
2-
import ELK from 'elkjs/lib/elk.bundled.js'
2+
import type ELK from 'elkjs/lib/elk.bundled.js'
33
import type { TaskInfo, NodeInfo } from '../types'
44
import { sortNodesByGlobalExecutionOrder } from './taskExecutionOrder'
55
import {
@@ -48,7 +48,20 @@ const resolveNodeWidth = (label: string): number => {
4848
return Math.max(NODE_MIN_WIDTH, Math.ceil(estimateLabelWidth(label) + NODE_HORIZONTAL_PADDING))
4949
}
5050

51-
const elk = new ELK()
51+
type ElkLayoutEngine = InstanceType<typeof ELK>
52+
53+
let elkPromise: Promise<ElkLayoutEngine> | null = null
54+
55+
const getElk = async (): Promise<ElkLayoutEngine> => {
56+
if (!elkPromise) {
57+
elkPromise = import('elkjs/lib/elk.bundled.js').then((module) => {
58+
const ElkConstructor = module.default
59+
return new ElkConstructor()
60+
})
61+
}
62+
63+
return elkPromise
64+
}
5265

5366
interface ElkLayoutNode {
5467
id: string
@@ -94,6 +107,7 @@ const resolveFlowNodeStatus = (
94107
}
95108

96109
export async function buildFlowchartData(task: TaskInfo, options: BuildFlowchartOptions = {}): Promise<{ nodes: Node[]; edges: Edge[] }> {
110+
const elk = await getElk()
97111
const orderedNodes = sortNodesByGlobalExecutionOrder(task.nodes)
98112
const orderedExecutions = orderedNodes.map((node) => ({
99113
node,

0 commit comments

Comments
 (0)