Skip to content

Commit

Permalink
Extract the registerComponentsBase function
Browse files Browse the repository at this point in the history
  • Loading branch information
skryukov committed Jun 16, 2024
1 parent 9e052bd commit 61da92b
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 36 deletions.
1 change: 1 addition & 0 deletions packages/turbo-mount/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { TurboMountController } from "./turbo-mount-controller";
export * from "./turbo-mount";
export * from "./registerComponentsBase";
51 changes: 15 additions & 36 deletions packages/turbo-mount/src/registerComponents.ts
Original file line number Diff line number Diff line change
@@ -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<T> = {
plugin: Plugin<T>;
Expand All @@ -14,43 +15,21 @@ type RegisterComponentsProps<T> = {
controllers?: Record<string, Definition>;
};

const identifierNames = (name: string) => {
const controllerName = camelToKebabCase(name);

return [`turbo-mount--${controllerName}`, `turbo-mount-${controllerName}`];
};

export const registerComponents = <T>({
plugin,
turboMount,
components,
controllers,
}: RegisterComponentsProps<T>) => {
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 = <T>(plugin: Plugin<T>) => {
Expand Down
57 changes: 57 additions & 0 deletions packages/turbo-mount/src/registerComponentsBase.ts
Original file line number Diff line number Diff line change
@@ -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<T> = {
plugin: Plugin<T>;
turboMount: TurboMount;
components: ComponentDefinition[];
controllers?: Definition[];
};

const identifierNames = (name: string) => {
const controllerName = camelToKebabCase(name);

return [`turbo-mount--${controllerName}`, `turbo-mount-${controllerName}`];
};

export const registerComponentsBase = <T>({
plugin,
turboMount,
components,
controllers,
}: RegisterComponentsProps<T>) => {
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);
}
}
};

0 comments on commit 61da92b

Please sign in to comment.