Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(resolve): make tryNodeResolve more robust #9170

Closed
wants to merge 12 commits into from
2 changes: 1 addition & 1 deletion packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
"postcss-import": "^15.0.0",
"postcss-load-config": "^4.0.1",
"postcss-modules": "^5.0.0",
"resolve.exports": "^1.1.0",
"resolve.exports": "npm:@alloc/resolve.exports@^1.1.0",
"sirv": "^2.0.2",
"source-map-js": "^1.0.2",
"source-map-support": "^0.5.21",
Expand Down
10 changes: 3 additions & 7 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,7 @@ import {
DEFAULT_MAIN_FIELDS,
ENV_ENTRY
} from './constants'
import type {
InternalResolveOptions,
InternalResolveOptionsWithOverrideConditions,
ResolveOptions
} from './plugins/resolve'
import type { InternalResolveOptions, ResolveOptions } from './plugins/resolve'
import { resolvePlugin, tryNodeResolve } from './plugins/resolve'
import type { LogLevel, Logger } from './logger'
import { createLogger } from './logger'
Expand Down Expand Up @@ -958,7 +954,7 @@ async function bundleConfigFile(
{
name: 'externalize-deps',
setup(build) {
const options: InternalResolveOptionsWithOverrideConditions = {
const options: InternalResolveOptions = {
root: path.dirname(fileName),
isBuild: true,
isProduction: true,
Expand All @@ -968,7 +964,7 @@ async function bundleConfigFile(
mainFields: [],
browserField: false,
conditions: [],
overrideConditions: ['node'],
overrideConditions: ['node', 'require'],
dedupe: [],
extensions: DEFAULT_EXTENSIONS,
preserveSymlinks: false
Expand Down
37 changes: 37 additions & 0 deletions packages/vite/src/node/packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,40 @@ export function watchPackageDataPlugin(config: ResolvedConfig): Plugin {
}
}
}

export function findPackageJson(dir: string): string | null {
// Stop looking at node_modules directory.
if (path.basename(dir) === 'node_modules') {
return null
}
const pkgPath = path.join(dir, 'package.json')
if (fs.existsSync(pkgPath)) {
return pkgPath
}
const parentDir = path.dirname(dir)
return parentDir !== dir ? findPackageJson(parentDir) : null
}

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

export function isWorkspaceRoot(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using searchForWorkspaceRoot, I added this function, which takes advantage of the packageCache. It also only checks the given directory, instead of an upward traversal.

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
}