siddharthkp / cost-of-modules

Find out which of your dependencies are slowing you down 🐢
MIT License
2.84k stars 41 forks source link

yarn support #67

Open JLarky opened 1 year ago

JLarky commented 1 year ago

I played with this a bit and there's a way to make this package work with yarn, so if someone wants to work on it, I just want to leave what code I tried:

diff --git a/src/helpers.js b/src/helpers.js
index c157a54..97eabd8 100644
--- a/src/helpers.js
+++ b/src/helpers.js
@@ -59,8 +59,20 @@ let setup = (includeDev) => {
     Adds them with --include-dev
 */
 let getDependencyTree = () => {
-  let result = syncExec(`npm ls --json ${productionModifier}`);
-  return JSON.parse(result.stdout).dependencies;
+  let result = syncExec(`yarn list --json`);
+  const transform = (packages) => {
+    const out = {};
+    packages.forEach((x) => {
+      const names = x.name.split("@");
+      const name = names[0] || "@" + names[1];
+      out[name] = { name };
+      if ("children" in x) {
+        out[name].dependencies = transform(x.children);
+      }
+    });
+    return out;
+  };
+  return transform(JSON.parse(result.stdout).data.trees);
 }; 

p.s. this doesn't work for my project because it has the same issue as in #66 p.p.s. I'm trying out analyze-module-size for now