Skip to content

Commit

Permalink
vite-plugin-symfony refactor stimulus-helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
lhapaipai committed Nov 9, 2023
1 parent 432e76c commit 9747382
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 44 deletions.
6 changes: 3 additions & 3 deletions src/vite-plugin-symfony/src/modules.d.ts
Original file line number Diff line number Diff line change
@@ -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;
}
43 changes: 4 additions & 39 deletions src/vite-plugin-symfony/src/stimulus-helpers/index.ts
Original file line number Diff line number Diff line change
@@ -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<Module>;
};

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;
Expand All @@ -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);
}
});
Expand All @@ -65,7 +31,6 @@ export function startStimulusApp(modules: ImportedModules) {
if (!thirdPartyControllers.hasOwnProperty(controllerName)) {
continue;
}
// @ts-ignore
app.register(controllerName, thirdPartyControllers[controllerName]);
}

Expand Down
6 changes: 6 additions & 0 deletions src/vite-plugin-symfony/src/stimulus-helpers/util.ts
Original file line number Diff line number Diff line change
@@ -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?)$/;
2 changes: 2 additions & 0 deletions src/vite-plugin-symfony/src/tests/pluginOptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe("resolvePluginOptions", () => {
"refresh": false,
"servePublic": "public",
"sriAlgorithm": false,
"stimulus": false,
"viteDevServerHostname": null,
}
`);
Expand All @@ -37,6 +38,7 @@ describe("resolvePluginOptions", () => {
"refresh": false,
"servePublic": "public",
"sriAlgorithm": false,
"stimulus": false,
"viteDevServerHostname": null,
}
`);
Expand Down
5 changes: 3 additions & 2 deletions src/vite-plugin-symfony/src/tests/stimulus/index.test.ts
Original file line number Diff line number Diff line change
@@ -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 }) => {
Expand Down Expand Up @@ -27,7 +28,7 @@ describe("stimulus", () => {
// ["not a controller", undefined],
];
list.forEach(([input, result]) => {
expect(identifierFromThirdParty(input)).toBe(result);
expect(generateStimulusId(input)).toBe(result);
});
});
});

0 comments on commit 9747382

Please sign in to comment.