replugged-org / replugged

A lightweight Discord client mod focused on simplicity and performance.
https://replugged.dev/
MIT License
644 stars 64 forks source link

getExportsBySource #424

Open yofukashino opened 1 year ago

yofukashino commented 1 year ago

Describe the feature request

an option to get export from module by source like getFunctionBySource but for exports of an module

Why should this be added?

to make it easier for devs to get module export to patch/use

Additional information

No response

Please confirm the following

asportnoy commented 1 year ago

What module would this be useful for and how would you do it now?

yofukashino commented 1 year ago

What module would this be useful for and how would you do it now?

idk you would do it thats something we would have to figure out myself but for example if i get this module webpack.getBySource(".sliderContainer") and it doesnt have a function directly to get by getFunctionBySource but have an export with render function and symbol so if we can just get the export by string/source instead of doing

Object.values(webpack.getBySource(".sliderContainer")).find(
  (m) => m?.render?.toString().includes("sliderContainer"),
)

it would be useful to make it easier to get export from module for devs

yofukashino commented 1 year ago
export const stringifyObj = (obj: ModuleExports): string => {
  const cache = [];
  const isObject = (testMaterial: unknown): boolean =>
  typeof testMaterial === "object" && !Array.isArray(testMaterial) && testMaterial != null;
  return JSON.stringify(obj, (_key, value) => {
    if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
      return value;
    } else if (typeof value === "function") {
      return `${value.toString()},prototype:${stringifyObj(value.prototype)}`
    } else if (value instanceof Error) {
      return value.stack || "";
    } else if (isObject(value) || Array.isArray(value)) {
      if (cache.includes(value)) {
      } else {
        // cache each item
        cache.push(value);
        return value;
      }
    }
  });
};

this piece of code turns export to string for matching to strings/regex,