Skip to content

Commit

Permalink
fix: stop looking in node_modules once a workspace root is found
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson committed Nov 14, 2022
1 parent 5115d77 commit fff33dc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
24 changes: 24 additions & 0 deletions packages/vite/src/node/packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,27 @@ export function findPackageJson(dir: string): string | null {
const parentDir = path.dirname(dir)
return parentDir !== dir ? findPackageJson(parentDir) : null
}

const workspaceRootFiles = ['lerna.json', 'pnpm-workspace.yaml', '.git']

export function isWorkspaceRoot(
dir: string,
preserveSymlinks?: boolean,
packageCache?: PackageCache
): boolean {
const files = fs.readdirSync(dir)
if (files.some((file) => workspaceRootFiles.includes(file))) {
return true // Found a lerna/pnpm workspace or git repository.
}
if (files.includes('package.json')) {
const workspacePkg = loadPackageData(
path.join(dir, 'package.json'),
preserveSymlinks,
packageCache
)
if (workspacePkg?.data.workspaces) {
return true // Found a npm/yarn workspace.
}
}
return false
}
17 changes: 16 additions & 1 deletion packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ import {
import { optimizedDepInfoFromFile, optimizedDepInfoFromId } from '../optimizer'
import type { DepsOptimizer } from '../optimizer'
import type { SSROptions } from '..'
import { findPackageJson, PackageCache, PackageData } from '../packages'
import {
findPackageJson,
isWorkspaceRoot,
PackageCache,
PackageData
} from '../packages'
import { loadPackageData, resolvePackageData } from '../packages'
import { isWorkerRequest } from './worker'

Expand Down Expand Up @@ -784,6 +789,16 @@ export function tryNodeResolve(
break
}
}

// Stop looking if we're at the workspace root directory.
if (
isWorkspaceRoot(
path.dirname(nodeModulesDir),
preserveSymlinks,
packageCache
)
)
break
}

if (!resolvedId) {
Expand Down

0 comments on commit fff33dc

Please sign in to comment.