Skip to content

Commit

Permalink
fix: use the closest package.json as root when workspace not found fo… (
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu authored and fi3ework committed May 22, 2021
1 parent c0e72b0 commit 9257646
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion packages/vite/src/node/server/searchRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,30 @@ function hasRootFile(root: string): boolean {
return ROOT_FILES.some((file) => fs.existsSync(join(root, file)))
}

function hasPackageJSON(root: string) {
const path = join(root, 'package.json')
return fs.existsSync(path)
}

/**
* Search up for the nearest `package.json`
*/
export function searchForPackageRoot(current: string, root = current): string {
if (hasPackageJSON(current)) return current

const dir = dirname(current)
// reach the fs root
if (!dir || dir === current) return root

return searchForPackageRoot(dir, root)
}

/**
* Search up for the nearest workspace root
*/
export function searchForWorkspaceRoot(
current: string,
root = current
root = searchForPackageRoot(current)
): string {
if (hasRootFile(current)) return current
if (hasWorkspacePackageJSON(current)) return current
Expand Down

0 comments on commit 9257646

Please sign in to comment.