|
| 1 | +const fs = require("fs") |
| 2 | +const path = require("path") |
| 3 | +const parser = require("@babel/parser") |
| 4 | +const traverse = require("@babel/traverse").default |
| 5 | +const glob = require("glob") |
| 6 | + |
| 7 | +// Function to get all JS/TS files in the project |
| 8 | +function getProjectFiles(projectRoot) { |
| 9 | + return glob.sync("**/*.{js,jsx,ts,tsx}", { |
| 10 | + cwd: projectRoot, |
| 11 | + ignore: ["node_modules/**", ".next/**", "out/**", "build/**", "dist/**"], |
| 12 | + }) |
| 13 | +} |
| 14 | + |
| 15 | +// Function to parse package.json and get dependencies |
| 16 | +function getDependencies(projectRoot) { |
| 17 | + const packageJson = JSON.parse(fs.readFileSync(path.join(projectRoot, "package.json"), "utf8")) |
| 18 | + return { |
| 19 | + ...packageJson.dependencies, |
| 20 | + ...packageJson.devDependencies, |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +// Function to get all exports from a package |
| 25 | +function getPackageExports(packageName) { |
| 26 | + try { |
| 27 | + const packagePath = require.resolve(packageName) |
| 28 | + const pkg = require(packagePath) |
| 29 | + return Object.keys(pkg) |
| 30 | + } catch (error) { |
| 31 | + console.warn(`Could not analyze exports for package: ${packageName}`) |
| 32 | + return [] |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// Function to analyze imports in a file |
| 37 | +function analyzeFileImports(filePath, projectRoot) { |
| 38 | + const content = fs.readFileSync(path.join(projectRoot, filePath), "utf8") |
| 39 | + const imports = new Set() |
| 40 | + |
| 41 | + try { |
| 42 | + const ast = parser.parse(content, { |
| 43 | + sourceType: "module", |
| 44 | + plugins: ["jsx", "typescript", "decorators-legacy"], |
| 45 | + }) |
| 46 | + |
| 47 | + traverse(ast, { |
| 48 | + ImportDeclaration(path) { |
| 49 | + const source = path.node.source.value |
| 50 | + if (!source.startsWith(".") && !source.startsWith("/")) { |
| 51 | + const packageName = source.split("/")[0] |
| 52 | + path.node.specifiers.forEach((specifier) => { |
| 53 | + if (specifier.type === "ImportSpecifier") { |
| 54 | + imports.add(`${packageName}:${specifier.imported.name}`) |
| 55 | + } |
| 56 | + }) |
| 57 | + } |
| 58 | + }, |
| 59 | + }) |
| 60 | + } catch (error) { |
| 61 | + console.warn(`Error parsing file ${filePath}:`, error.message) |
| 62 | + } |
| 63 | + |
| 64 | + return imports |
| 65 | +} |
| 66 | + |
| 67 | +// Main function to analyze the project |
| 68 | +async function analyzeProject(projectRoot) { |
| 69 | + const files = getProjectFiles(projectRoot) |
| 70 | + const dependencies = getDependencies(projectRoot) |
| 71 | + const packageImports = new Map() |
| 72 | + const unusedExports = new Map() |
| 73 | + |
| 74 | + // Initialize package exports |
| 75 | + for (const pkg of Object.keys(dependencies)) { |
| 76 | + const exports = getPackageExports(pkg) |
| 77 | + if (exports.length > 0) { |
| 78 | + unusedExports.set(pkg, new Set(exports)) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // Analyze each file |
| 83 | + for (const file of files) { |
| 84 | + const imports = analyzeFileImports(file, projectRoot) |
| 85 | + |
| 86 | + for (const imp of imports) { |
| 87 | + const [pkg, exportName] = imp.split(":") |
| 88 | + if (unusedExports.has(pkg)) { |
| 89 | + unusedExports.get(pkg).delete(exportName) |
| 90 | + } |
| 91 | + |
| 92 | + if (!packageImports.has(pkg)) { |
| 93 | + packageImports.set(pkg, new Set()) |
| 94 | + } |
| 95 | + packageImports.get(pkg).add(exportName) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + // Find packages with unused exports |
| 100 | + const packagesWithUnusedExports = [] |
| 101 | + for (const [pkg, unused] of unusedExports) { |
| 102 | + if (unused.size > 0) { |
| 103 | + packagesWithUnusedExports.push({ |
| 104 | + package: pkg, |
| 105 | + unusedCount: unused.size, |
| 106 | + totalExports: getPackageExports(pkg).length, |
| 107 | + unusedExports: Array.from(unused), |
| 108 | + }) |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + return packagesWithUnusedExports |
| 113 | +} |
| 114 | + |
| 115 | +// Run the analysis |
| 116 | +const projectRoot = process.cwd() |
| 117 | +analyzeProject(projectRoot) |
| 118 | + .then((results) => { |
| 119 | + console.log("\nPackages with unused exports:") |
| 120 | + console.log("============================") |
| 121 | + |
| 122 | + const optimizePackageImports = results |
| 123 | + .filter((pkg) => pkg.unusedCount > 5) // Threshold for significant unused exports |
| 124 | + .map((pkg) => pkg.package) |
| 125 | + |
| 126 | + results.forEach((pkg) => { |
| 127 | + console.log(`\n${pkg.package}:`) |
| 128 | + console.log(`- ${pkg.unusedCount} unused exports out of ${pkg.totalExports} total exports`) |
| 129 | + console.log( |
| 130 | + "- Unused exports:", |
| 131 | + pkg.unusedExports.slice(0, 5).join(", ") + |
| 132 | + (pkg.unusedExports.length > 5 ? ` and ${pkg.unusedExports.length - 5} more...` : ""), |
| 133 | + ) |
| 134 | + }) |
| 135 | + |
| 136 | + console.log("\nRecommended optimizePackageImports configuration:") |
| 137 | + console.log("=============================================") |
| 138 | + console.log("Add this to your next.config.js:") |
| 139 | + console.log("optimizePackageImports: [") |
| 140 | + optimizePackageImports.forEach((pkg) => console.log(` '${pkg}',`)) |
| 141 | + console.log("]") |
| 142 | + }) |
| 143 | + .catch((error) => { |
| 144 | + console.error("Error analyzing project:", error) |
| 145 | + }) |
0 commit comments