diff --git a/packages/next/src/server/lib/recursive-readdir-sync.ts b/packages/next/src/server/lib/recursive-readdir-sync.ts index aadcf75dd18cf..07549a88bbc36 100644 --- a/packages/next/src/server/lib/recursive-readdir-sync.ts +++ b/packages/next/src/server/lib/recursive-readdir-sync.ts @@ -1,5 +1,5 @@ import fs from 'fs' -import { join } from 'path' +import { sep } from 'path' /** * Recursively read directory @@ -10,20 +10,22 @@ export function recursiveReadDirSync( dir: string, /** This doesn't have to be provided, it's used for the recursion */ arr: string[] = [], - /** Used to replace the initial path, only the relative path is left, it's faster than path.relative. */ - rootDir = dir + /** Used to remove the initial path suffix and leave only the relative, faster than path.relative. */ + rootDirLength = dir.length ): string[] { - const result = fs.readdirSync(dir, { withFileTypes: true }) - - result.forEach((part) => { - const absolutePath = join(dir, part.name) + // Use opendirSync for better memory usage + const result = fs.opendirSync(dir) + let part: fs.Dirent | null + while ((part = result.readSync())) { + const absolutePath = dir + sep + part.name if (part.isDirectory()) { - recursiveReadDirSync(absolutePath, arr, rootDir) - return + recursiveReadDirSync(absolutePath, arr, rootDirLength) + } else { + arr.push(absolutePath.slice(rootDirLength)) } - arr.push(absolutePath.replace(rootDir, '')) - }) + } + result.closeSync() return arr }