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

refactor: use resolvePackageData in requireResolveFromRootWithFallback #12712

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 12 additions & 9 deletions packages/vite/src/node/utils.ts
Expand Up @@ -3,7 +3,7 @@ import os from 'node:os'
import path from 'node:path'
import { exec } from 'node:child_process'
import { createHash } from 'node:crypto'
import { URL, URLSearchParams } from 'node:url'
import { URL, URLSearchParams, fileURLToPath } from 'node:url'
import { builtinModules, createRequire } from 'node:module'
import { promises as dns } from 'node:dns'
import { performance } from 'node:perf_hooks'
Expand Down Expand Up @@ -34,6 +34,7 @@ import {
import type { DepOptimizationConfig } from './optimizer'
import type { ResolvedConfig } from './config'
import type { ResolvedServerUrls, ViteDevServer } from './server'
import { resolvePackageData } from './packages'
import type { CommonServerOptions } from '.'

/**
Expand Down Expand Up @@ -963,21 +964,23 @@ export function getHash(text: Buffer | string): string {
return createHash('sha256').update(text).digest('hex').substring(0, 8)
}

const _dirname = path.dirname(fileURLToPath(import.meta.url))

export const requireResolveFromRootWithFallback = (
root: string,
id: string,
): string => {
const paths = _require.resolve.paths?.(id) || []
// Search in the root directory first, and fallback to the default require paths.
paths.unshift(root)

// Use `resolve` package to check existence first, so if the package is not found,
// check existence first, so if the package is not found,
// it won't be cached by nodejs, since there isn't a way to invalidate them:
// https://github.com/nodejs/node/issues/44663
resolve.sync(id, { basedir: root, paths })
const found = resolvePackageData(id, root) || resolvePackageData(id, _dirname)
if (!found) {
throw new Error(`${JSON.stringify(id)} not found.`)
sapphi-red marked this conversation as resolved.
Show resolved Hide resolved
}

// Use `require.resolve` again as the `resolve` package doesn't support the `exports` field
return _require.resolve(id, { paths })
// actually resolve
// Search in the root directory first, and fallback to the default require paths.
return _require.resolve(id, { paths: [root, _dirname] })
}

export function emptyCssComments(raw: string): string {
Expand Down