From 58139eedbb5d9ab820226ebdd2a0c37b2078fec6 Mon Sep 17 00:00:00 2001 From: sapphi-red Date: Mon, 3 Apr 2023 16:28:11 +0900 Subject: [PATCH 1/2] refactor: use `resolvePackageData` in `requireResolveFromRootWithFallback` --- packages/vite/src/node/utils.ts | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index 3f52cda0e53565..b56bab18afb0d7 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -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' @@ -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 '.' /** @@ -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.`) + } - // 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 { From cf9c5e1eea99c1c0a798cf60023ffef028bd60c1 Mon Sep 17 00:00:00 2001 From: sapphi-red Date: Wed, 5 Apr 2023 17:27:40 +0900 Subject: [PATCH 2/2] fix: add `error.code` --- packages/vite/src/node/utils.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index b56bab18afb0d7..69555d069fbc73 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -975,7 +975,9 @@ export const requireResolveFromRootWithFallback = ( // https://github.com/nodejs/node/issues/44663 const found = resolvePackageData(id, root) || resolvePackageData(id, _dirname) if (!found) { - throw new Error(`${JSON.stringify(id)} not found.`) + const error = new Error(`${JSON.stringify(id)} not found.`) + ;(error as any).code = 'MODULE_NOT_FOUND' + throw error } // actually resolve