Skip to content

Commit

Permalink
refactor: use resolvePackageData in `requireResolveFromRootWithFall…
Browse files Browse the repository at this point in the history
…back` (#12712)
  • Loading branch information
sapphi-red committed Apr 5, 2023
1 parent 8e30025 commit 1ea38e2
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 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 @@ -966,21 +967,25 @@ 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) {
const error = new Error(`${JSON.stringify(id)} not found.`)
;(error as any).code = 'MODULE_NOT_FOUND'
throw error
}

// 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

0 comments on commit 1ea38e2

Please sign in to comment.