diff --git a/src/vite-plugin-symfony/src/modules.d.ts b/src/vite-plugin-symfony/src/modules.d.ts index cb95f7d9..0eff11c2 100644 --- a/src/vite-plugin-symfony/src/modules.d.ts +++ b/src/vite-plugin-symfony/src/modules.d.ts @@ -1,7 +1,7 @@ declare module "virtual:symfony/controllers" { + import { type ControllerConstructor } from "@hotwired/stimulus"; const modules: { - identifier: string; - controllerLoader: () => any; - }[]; + [controllerName: string]: ControllerConstructor; + }; export default modules; } diff --git a/src/vite-plugin-symfony/src/stimulus-helpers/index.ts b/src/vite-plugin-symfony/src/stimulus-helpers/index.ts index eaa83b3c..d6a4cbec 100644 --- a/src/vite-plugin-symfony/src/stimulus-helpers/index.ts +++ b/src/vite-plugin-symfony/src/stimulus-helpers/index.ts @@ -1,50 +1,17 @@ -import { Application } from "@hotwired/stimulus"; +import { Application, type ControllerConstructor } from "@hotwired/stimulus"; import thirdPartyControllers from "virtual:symfony/controllers"; +import { identifierFromFilePath } from "./util"; -console.log("from controllers", thirdPartyControllers); +// console.log("from controllers", thirdPartyControllers); type Module = { - [x: string]: unknown; + default: ControllerConstructor; }; export type ImportedModules = { [filePath: string]: () => Promise; }; -export const CONTROLLER_FILENAME_REGEX = /^(?:.*?(?:controllers)\/|\.?\.\/)?(.+)(?:[/_-]controller\.[jt]sx?)$/; - -export function identifierFromFilePath(key: string): string | undefined { - const extract = (key.match(CONTROLLER_FILENAME_REGEX) || [])[1]; - if (extract) return extract.replace(/_/g, "-").replace(/\//g, "--"); -} - -export function registerStimulusControllers(app, thirdPartyControllers, modules: ImportedModules) { - Object.entries(modules).forEach(([filePath, controllerLoader]) => { - const identifier = identifierFromFilePath(filePath); - controllerLoader().then((controllerConstructor) => { - if (identifier && typeof controllerConstructor.default === "function") { - app.register(identifier, controllerConstructor.default); - } - }); - }); - console.log("third party controllers", thirdPartyControllers); - setTimeout(() => { - for (const controllerName in thirdPartyControllers) { - // eslint-disable-next-line no-prototype-builtins - if (!thirdPartyControllers.hasOwnProperty(controllerName)) { - continue; - } - app.register(controllerName, thirdPartyControllers[controllerName]); - } - // app.load(thirdPartyControllers); - // thirdPartyControllers.forEach(({ identifier, controllerLoader }) => { - // controllerLoader().then((controllerConstructor) => { - // app.register(identifier, controllerConstructor.default); - // }); - // }); - }, 3000); -} - export function startStimulusApp(modules: ImportedModules) { const app = Application.start(); app.debug = true; @@ -53,7 +20,6 @@ export function startStimulusApp(modules: ImportedModules) { const identifier = identifierFromFilePath(filePath); controllerLoader().then((controllerConstructor) => { if (identifier && typeof controllerConstructor.default === "function") { - // @ts-ignore app.register(identifier, controllerConstructor.default); } }); @@ -65,7 +31,6 @@ export function startStimulusApp(modules: ImportedModules) { if (!thirdPartyControllers.hasOwnProperty(controllerName)) { continue; } - // @ts-ignore app.register(controllerName, thirdPartyControllers[controllerName]); } diff --git a/src/vite-plugin-symfony/src/stimulus-helpers/util.ts b/src/vite-plugin-symfony/src/stimulus-helpers/util.ts new file mode 100644 index 00000000..fe624c6d --- /dev/null +++ b/src/vite-plugin-symfony/src/stimulus-helpers/util.ts @@ -0,0 +1,6 @@ +export function identifierFromFilePath(key: string): string | undefined { + const extract = (key.match(CONTROLLER_FILENAME_REGEX) || [])[1]; + if (extract) return extract.replace(/_/g, "-").replace(/\//g, "--"); +} + +export const CONTROLLER_FILENAME_REGEX = /^(?:.*?(?:controllers)\/|\.?\.\/)?(.+)(?:[/_-]controller\.[jt]sx?)$/; diff --git a/src/vite-plugin-symfony/src/tests/pluginOptions.test.ts b/src/vite-plugin-symfony/src/tests/pluginOptions.test.ts index de40e7fc..c365be4d 100644 --- a/src/vite-plugin-symfony/src/tests/pluginOptions.test.ts +++ b/src/vite-plugin-symfony/src/tests/pluginOptions.test.ts @@ -15,6 +15,7 @@ describe("resolvePluginOptions", () => { "refresh": false, "servePublic": "public", "sriAlgorithm": false, + "stimulus": false, "viteDevServerHostname": null, } `); @@ -37,6 +38,7 @@ describe("resolvePluginOptions", () => { "refresh": false, "servePublic": "public", "sriAlgorithm": false, + "stimulus": false, "viteDevServerHostname": null, } `); diff --git a/src/vite-plugin-symfony/src/tests/stimulus/index.test.ts b/src/vite-plugin-symfony/src/tests/stimulus/index.test.ts index 1623863b..7c52a708 100644 --- a/src/vite-plugin-symfony/src/tests/stimulus/index.test.ts +++ b/src/vite-plugin-symfony/src/tests/stimulus/index.test.ts @@ -1,5 +1,6 @@ import { describe, it } from "vitest"; -import { identifierFromFilePath, identifierFromThirdParty } from "../../stimulus"; +import { generateStimulusId } from "../../stimulusBridge"; +import { identifierFromFilePath } from "../../stimulus-helpers/util"; describe("stimulus", () => { it("identifierFromFilePath generate correct identifier", ({ expect }) => { @@ -27,7 +28,7 @@ describe("stimulus", () => { // ["not a controller", undefined], ]; list.forEach(([input, result]) => { - expect(identifierFromThirdParty(input)).toBe(result); + expect(generateStimulusId(input)).toBe(result); }); }); });