From 61da92ba675ba1d01fdfce79b186e05e58026713 Mon Sep 17 00:00:00 2001 From: Svyatoslav Kryukov Date: Sat, 15 Jun 2024 22:33:56 +0300 Subject: [PATCH] Extract the `registerComponentsBase` function --- packages/turbo-mount/src/index.ts | 1 + .../turbo-mount/src/registerComponents.ts | 51 +++++------------ .../turbo-mount/src/registerComponentsBase.ts | 57 +++++++++++++++++++ 3 files changed, 73 insertions(+), 36 deletions(-) create mode 100644 packages/turbo-mount/src/registerComponentsBase.ts diff --git a/packages/turbo-mount/src/index.ts b/packages/turbo-mount/src/index.ts index 6736aea..1d5daf4 100644 --- a/packages/turbo-mount/src/index.ts +++ b/packages/turbo-mount/src/index.ts @@ -1,2 +1,3 @@ export { TurboMountController } from "./turbo-mount-controller"; export * from "./turbo-mount"; +export * from "./registerComponentsBase"; diff --git a/packages/turbo-mount/src/registerComponents.ts b/packages/turbo-mount/src/registerComponents.ts index db2e2a1..f344279 100644 --- a/packages/turbo-mount/src/registerComponents.ts +++ b/packages/turbo-mount/src/registerComponents.ts @@ -1,11 +1,12 @@ import { definitionsFromGlob } from "stimulus-vite-helpers"; import { Definition } from "@hotwired/stimulus"; -import { TurboMount, Plugin } from "turbo-mount"; - -import { camelToKebabCase } from "./helpers"; - -type ComponentModule = { default: never } | never; +import { + TurboMount, + Plugin, + registerComponentsBase, + ComponentModule, +} from "turbo-mount"; type RegisterComponentsProps = { plugin: Plugin; @@ -14,43 +15,21 @@ type RegisterComponentsProps = { controllers?: Record; }; -const identifierNames = (name: string) => { - const controllerName = camelToKebabCase(name); - - return [`turbo-mount--${controllerName}`, `turbo-mount-${controllerName}`]; -}; - export const registerComponents = ({ plugin, turboMount, components, controllers, }: RegisterComponentsProps) => { - const controllerModules = controllers ? definitionsFromGlob(controllers) : []; - - for (const [componentPath, componentModule] of Object.entries(components)) { - const name = componentPath - .replace(/\.\w*$/, "") - .replace(/^[./]*components\//, ""); - - const identifiers = identifierNames(name); - - const controller = controllerModules.find(({ identifier }) => - identifiers.includes(identifier), - ); - const component = componentModule.default ?? componentModule; - - if (controller) { - turboMount.register( - plugin, - name, - component, - controller.controllerConstructor, - ); - } else { - turboMount.register(plugin, name, component); - } - } + return registerComponentsBase({ + plugin, + turboMount, + components: Object.entries(components).map(([filename, module]) => ({ + filename, + module, + })), + controllers: controllers ? definitionsFromGlob(controllers) : [], + }); }; export const buildRegisterComponentsFunction = (plugin: Plugin) => { diff --git a/packages/turbo-mount/src/registerComponentsBase.ts b/packages/turbo-mount/src/registerComponentsBase.ts new file mode 100644 index 0000000..084e3e2 --- /dev/null +++ b/packages/turbo-mount/src/registerComponentsBase.ts @@ -0,0 +1,57 @@ +import { Definition } from "@hotwired/stimulus"; + +import { TurboMount, Plugin } from "./turbo-mount"; +import { camelToKebabCase } from "./helpers"; + +export type ComponentModule = { default: never } | never; + +type ComponentDefinition = { + filename: string; + module: ComponentModule; +}; + +type RegisterComponentsProps = { + plugin: Plugin; + turboMount: TurboMount; + components: ComponentDefinition[]; + controllers?: Definition[]; +}; + +const identifierNames = (name: string) => { + const controllerName = camelToKebabCase(name); + + return [`turbo-mount--${controllerName}`, `turbo-mount-${controllerName}`]; +}; + +export const registerComponentsBase = ({ + plugin, + turboMount, + components, + controllers, +}: RegisterComponentsProps) => { + const controllerModules = controllers ?? []; + + for (const { module, filename } of components) { + const name = filename + .replace(/\.\w*$/, "") + .replace(/^[./]*components\//, ""); + + const identifiers = identifierNames(name); + + const controller = controllerModules.find(({ identifier }) => + identifiers.includes(identifier), + ); + const component = module.default ?? module; + + if (controller) { + turboMount.register( + plugin, + name, + component, + controller.controllerConstructor, + ); + } else { + turboMount.register(plugin, name, component); + } + } +};