From 3aaf65ccfce69f3501d5b8c3c0719f7b3bb5c48c Mon Sep 17 00:00:00 2001 From: Ryan Zimmerman Date: Thu, 20 Aug 2026 11:57:05 -0400 Subject: [PATCH] Refactor getAncestorDirs to iterate instead of recurse --- index.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 7294893..3572b44 100755 --- a/index.js +++ b/index.js @@ -349,11 +349,15 @@ function error(err) { // Input: '/imports/components/button.css' // Output: ['/imports/components', '/imports', '/'] -function getAncestorDirs(fileOrDir) { - const { root } = path.parse(fileOrDir) - if (fileOrDir === root) { - return [] +function getAncestorDirs(file) { + const { root } = path.parse(file) + const ancestors = [] + let current = file + + while (current !== root) { + current = path.dirname(current) + ancestors.push(current) } - const parentDir = path.dirname(fileOrDir) - return [parentDir, ...getAncestorDirs(parentDir)] + + return ancestors }