From 36f3fe04e396b71da32c08b1e0c510ae338464c1 Mon Sep 17 00:00:00 2001 From: karlsbeard <255775675@qq.com> Date: Thu, 22 Aug 2024 22:02:28 +0800 Subject: [PATCH] feat:add the umd & cjs & es6 support --- src/main.ts | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/main.ts b/src/main.ts index 923028f..970188f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -72,3 +72,37 @@ export const TTs = { }, } + +function makeHandlers() { + const handlers = {} as { [K in keyof typeof TTs]: typeof TTs[K] } + for (const key in TTs) { + if (key.startsWith('_')) + continue + handlers[key] = TTs[key] + } + return handlers +} + +const handlers = makeHandlers() + +const treeHandler = { + ...handlers, + createInstance(config: Partial = {}) { + const obj = {} + for (const key in handlers) { + const func = handlers[key] + obj[key] = (...args) => func(...args, config) + } + return obj + }, +} + +if (typeof window !== 'undefined') { + // @ts-expect-error global variable + window.TTs = treeHandler +} +else if (typeof exports !== 'undefined') { + module.exports = treeHandler +} + +export default treeHandler