Skip to content

Commit

Permalink
fix: 动态加载npm类型工具类
Browse files Browse the repository at this point in the history
  • Loading branch information
yy-wow committed Jan 15, 2025
1 parent 58a3376 commit 6d22724
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion packages/canvas/render/src/application-function/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ref } from 'vue'
import TinyVue from '@opentiny/vue'
import * as TinyVueIcon from '@opentiny/vue-icon'
import { generateFunction } from '../data-utils'
import { globalNotify } from '../canvas-function'

export interface IUtil {
name: string
Expand All @@ -18,7 +19,7 @@ export function useUtils(context: Record<string, any>) {
const utils: Record<string, Function | any> = {}
const getUtils = () => utils

const setUtils = (data: Array<IUtil>) => {
const setUtils = async (data: Array<IUtil>) => {
if (!Array.isArray(data)) {
return
}
Expand Down Expand Up @@ -52,6 +53,29 @@ export function useUtils(context: Record<string, any>) {
utilsCollection[item.name] = generateFunction(item.content.value, context) || defaultFn
}
})

const npmUtilsImports = data
.filter((item) => item.type === 'npm' && item.content.cdnLink)
.map((item) => import(/* @vite-ignore */ item.content.cdnLink))
const npmUtils = await Promise.allSettled(npmUtilsImports)

npmUtils.forEach((res, index) => {
const { name, content } = data[index]
const { exportName, destructuring, cdnLink } = content

if (res.status !== 'fulfilled') {
globalNotify({
type: 'error',
message: `加载工具类“${name}”失败,请检查cdn链接是否正确,${cdnLink}`
})

return
}

const module = res.value
utilsCollection[name] = destructuring ? module[exportName] : module.default
})

Object.assign(utils, utilsCollection)

refreshKey.value++
Expand Down

0 comments on commit 6d22724

Please sign in to comment.