diff --git a/packages/vite-node/package.json b/packages/vite-node/package.json index b93429c35..9b3c35199 100644 --- a/packages/vite-node/package.json +++ b/packages/vite-node/package.json @@ -87,7 +87,6 @@ "debug": "^4.3.4", "pathe": "^1.1.1", "picocolors": "^1.0.0", - "pkg-types": "^1.0.3", "vite": "^3.0.0 || ^4.0.0 || ^5.0.0-0" }, "devDependencies": { diff --git a/packages/vite-node/src/externalize.ts b/packages/vite-node/src/externalize.ts index 8efaf3d5d..2f23a3255 100644 --- a/packages/vite-node/src/externalize.ts +++ b/packages/vite-node/src/externalize.ts @@ -1,8 +1,7 @@ import { existsSync, promises as fsp } from 'node:fs' -import { readPackageJSON } from 'pkg-types' -import { extname, join } from 'pathe' +import { dirname, extname, join } from 'pathe' import type { DepsHandlingOptions } from './types' -import { isNodeBuiltin, slash } from './utils' +import { findNearestPackageData, isNodeBuiltin, slash } from './utils' import { KNOWN_ASSET_TYPES } from './constants' const BUILTIN_EXTENSIONS = new Set(['.mjs', '.cjs', '.node', '.wasm']) @@ -65,14 +64,14 @@ async function isValidNodeImport(id: string) { if (/\.(\w+-)?esm?(-\w+)?\.js$|\/(esm?)\//.test(id)) return false - const package_ = await readPackageJSON(id).catch(() => ({ - type: undefined, - })) + id = id.replace('file:///', '') + + const package_ = await findNearestPackageData(dirname(id)) if (package_.type === 'module') return true - const code = await fsp.readFile(id.replace('file:///', ''), 'utf8').catch(() => '') + const code = await fsp.readFile(id, 'utf8').catch(() => '') return !ESM_SYNTAX_RE.test(code) } diff --git a/packages/vite-node/src/utils.ts b/packages/vite-node/src/utils.ts index e6cdcaf74..cffe441da 100644 --- a/packages/vite-node/src/utils.ts +++ b/packages/vite-node/src/utils.ts @@ -1,7 +1,7 @@ import { fileURLToPath, pathToFileURL } from 'node:url' import { builtinModules } from 'node:module' -import { existsSync } from 'node:fs' -import { dirname, resolve } from 'pathe' +import { existsSync, promises as fsp } from 'node:fs' +import { dirname, join, resolve } from 'pathe' import type { Arrayable, Nullable } from './types' export const isWindows = process.platform === 'win32' @@ -235,3 +235,31 @@ export function createImportMetaEnvProxy() { }, }) } + +const packageCache = new Map() + +export async function findNearestPackageData(basedir: string): Promise<{ type?: 'module' | 'commonjs' }> { + const originalBasedir = basedir + while (basedir) { + const cached = getCachedData(packageCache, basedir, originalBasedir) + if (cached) + return cached + + const pkgPath = join(basedir, 'package.json') + if ((await fsp.stat(pkgPath).catch(() => {}))?.isFile()) { + const pkgData = JSON.parse(await fsp.readFile(pkgPath, 'utf8')) + + if (packageCache) + setCacheData(packageCache, pkgData, basedir, originalBasedir) + + return pkgData + } + + const nextBasedir = dirname(basedir) + if (nextBasedir === basedir) + break + basedir = nextBasedir + } + + return {} +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e06cedea9..69021d929 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1315,9 +1315,6 @@ importers: picocolors: specifier: ^1.0.0 version: 1.0.0 - pkg-types: - specifier: ^1.0.3 - version: 1.0.3 vite: specifier: ^5.0.0-beta.12 version: 5.0.0-beta.12(@types/node@18.16.19)(less@4.1.3) @@ -13134,6 +13131,7 @@ packages: resolution: {integrity: sha512-F0SAmZ8iUtS//m8DmCTA0jlh6TDKkHQyK6xc6V4KDTyZKA9dnvX9/3sRTVQrWm79glUAZbnmmNcdYwUIHWVybw==} engines: {node: '>=0.4.0'} hasBin: true + dev: true /acorn@8.8.2: resolution: {integrity: sha512-xjIYgE8HBrkpd/sJqOGNspf8uHG+NOHGOw6a/Urj8taM2EXfdNAH2oFcPeIFfsv3+kz/mJrS5VuMqbNLjCa2vw==} @@ -21394,6 +21392,7 @@ packages: /jsonc-parser@3.2.0: resolution: {integrity: sha512-gfFQZrcTc8CnKXp6Y4/CBT3fTc0OVuDofpre4aEeEpSBPV5X5v4+Vmx+8snU7RLPrNHPKSgLxGo9YuQzz20o+w==} + dev: true /jsonfile@4.0.0: resolution: {integrity: sha512-m6F1R3z8jjlf2imQHS2Qez5sjKWQzbuuhuJ/FKYFRZvPE3PuHcSMVZzfsLhGVOkfd20obL5SWEBew5ShlquNxg==} @@ -22452,6 +22451,7 @@ packages: pathe: 1.1.1 pkg-types: 1.0.3 ufo: 1.3.1 + dev: true /moment@2.29.4: resolution: {integrity: sha512-5LC9SOxjSc2HF6vO2CyuTDNivEdoz2IvyJJGj6X8DJ0eFyfszE0QiEd+iXmBvUP3WHxSjFH/vIsA0EN00cgr8w==} @@ -23806,6 +23806,7 @@ packages: jsonc-parser: 3.2.0 mlly: 1.4.2 pathe: 1.1.1 + dev: true /playwright-chromium@1.39.0: resolution: {integrity: sha512-0WVmvn9ppPbcyb2PQherIpzsvJlyjqziCZiAiexTEYSz8k6/+/3wljmFaMRMP1lcv2xKyHDn9yWd/lHb7IzYyA==} @@ -27637,6 +27638,7 @@ packages: /ufo@1.3.1: resolution: {integrity: sha512-uY/99gMLIOlJPwATcMVYfqDSxUR9//AUcgZMzwfSTJPDKzA1S8mX4VLqa+fiAtveraQUBCz4FFcwVZBGbwBXIw==} + dev: true /uglify-js@3.17.0: resolution: {integrity: sha512-aTeNPVmgIMPpm1cxXr2Q/nEbvkmV8yq66F3om7X3P/cvOXQ0TMQ64Wk63iyT1gPlmdmGzjGpyLh1f3y8MZWXGg==}