Skip to content

Commit

Permalink
feat(classMapper): add classModuleMap, make findClass require ID
Browse files Browse the repository at this point in the history
  • Loading branch information
AAGaming00 committed Jul 24, 2024
1 parent d83bada commit a370c1f
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/class-mapper.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Module, findAllModules } from './webpack';
import { Module, ModuleID, createModuleMapping } from './webpack';

export interface ClassModule {
[name: string]: string;
}

export const classMap: ClassModule[] = findAllModules((m: Module) => {
export const classModuleMap: Map<ModuleID, ClassModule> = createModuleMapping((m: Module) => {
if (typeof m == 'object' && !m.__esModule) {
const keys = Object.keys(m);
// special case some libraries
Expand All @@ -17,16 +17,19 @@ export const classMap: ClassModule[] = findAllModules((m: Module) => {
return false;
});

export function findClass(name: string): string | void {
return classMap.find((m) => m?.[name])?.[name];
export const classMap = classModuleMap.values();

export function findClass(id: string, name: string): string | void {
return classModuleMap.get(id)?.[name];
}

export function findClassModule(filter: (module: any) => boolean): ClassModule | void {
return classMap.find((m) => filter(m));
// TODO optimize
return [...classModuleMap.values()].find((m) => filter(m));
}

export function unminifyClass(minifiedClass: string): string | void {
for (let m of classMap) {
for (let m of classModuleMap.values()) {
for (let className of Object.keys(m)) {
if (m[className] == minifiedClass) return className;
}
Expand Down

0 comments on commit a370c1f

Please sign in to comment.