Skip to content

Commit 35faae9

Browse files
authoredMar 26, 2023
refactor: use findNearestPackageData in more places (#12577)
1 parent 1cc99f8 commit 35faae9

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed
 

‎packages/vite/src/node/build.ts

+19-12
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import {
3232
copyDir,
3333
emptyDir,
3434
joinUrlSegments,
35-
lookupFile,
3635
normalizePath,
3736
requireResolveFromRootWithFallback,
3837
} from './utils'
@@ -52,8 +51,8 @@ import {
5251
initDepsOptimizer,
5352
} from './optimizer'
5453
import { loadFallbackPlugin } from './plugins/loadFallback'
55-
import type { PackageData } from './packages'
56-
import { watchPackageDataPlugin } from './packages'
54+
import { findNearestPackageData, watchPackageDataPlugin } from './packages'
55+
import type { PackageCache } from './packages'
5756
import { ensureWatchPlugin } from './plugins/ensureWatch'
5857
import { ESBUILD_MODULES_TARGET, VERSION } from './constants'
5958
import { resolveChokidarOptions } from './watch'
@@ -578,7 +577,11 @@ export async function build(
578577
const format = output.format || (cjsSsrBuild ? 'cjs' : 'es')
579578
const jsExt =
580579
ssrNodeBuild || libOptions
581-
? resolveOutputJsExtension(format, getPkgJson(config.root)?.type)
580+
? resolveOutputJsExtension(
581+
format,
582+
findNearestPackageData(config.root, config.packageCache)?.data
583+
.type,
584+
)
582585
: 'js'
583586
return {
584587
dir: outDir,
@@ -595,7 +598,14 @@ export async function build(
595598
? `[name].${jsExt}`
596599
: libOptions
597600
? ({ name }) =>
598-
resolveLibFilename(libOptions, format, name, config.root, jsExt)
601+
resolveLibFilename(
602+
libOptions,
603+
format,
604+
name,
605+
config.root,
606+
jsExt,
607+
config.packageCache,
608+
)
599609
: path.posix.join(options.assetsDir, `[name]-[hash].${jsExt}`),
600610
chunkFileNames: libOptions
601611
? `[name]-[hash].${jsExt}`
@@ -742,10 +752,6 @@ function prepareOutDir(
742752
}
743753
}
744754

745-
function getPkgJson(root: string): PackageData['data'] {
746-
return JSON.parse(lookupFile(root, ['package.json']) || `{}`)
747-
}
748-
749755
function getPkgName(name: string) {
750756
return name?.[0] === '@' ? name.split('/')[1] : name
751757
}
@@ -769,15 +775,16 @@ export function resolveLibFilename(
769775
entryName: string,
770776
root: string,
771777
extension?: JsExt,
778+
packageCache?: PackageCache,
772779
): string {
773780
if (typeof libOptions.fileName === 'function') {
774781
return libOptions.fileName(format, entryName)
775782
}
776783

777-
const packageJson = getPkgJson(root)
784+
const packageJson = findNearestPackageData(root, packageCache)?.data
778785
const name =
779786
libOptions.fileName ||
780-
(typeof libOptions.entry === 'string'
787+
(packageJson && typeof libOptions.entry === 'string'
781788
? getPkgName(packageJson.name)
782789
: entryName)
783790

@@ -786,7 +793,7 @@ export function resolveLibFilename(
786793
'Name in package.json is required if option "build.lib.fileName" is not provided.',
787794
)
788795

789-
extension ??= resolveOutputJsExtension(format, packageJson.type)
796+
extension ??= resolveOutputJsExtension(format, packageJson?.type)
790797

791798
if (format === 'cjs' || format === 'es') {
792799
return `${name}.${extension}`

‎packages/vite/src/node/config.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ import type { JsonOptions } from './plugins/json'
6262
import type { PluginContainer } from './server/pluginContainer'
6363
import { createPluginContainer } from './server/pluginContainer'
6464
import type { PackageCache } from './packages'
65+
import { findNearestPackageData } from './packages'
6566
import { loadEnv, resolveEnvPrefix } from './env'
6667
import type { ResolvedSSROptions, SSROptions } from './ssr'
6768
import { resolveSSROptions } from './ssr'
@@ -390,6 +391,7 @@ export async function resolveConfig(
390391
let configFileDependencies: string[] = []
391392
let mode = inlineConfig.mode || defaultMode
392393
const isNodeEnvSet = !!process.env.NODE_ENV
394+
const packageCache: PackageCache = new Map()
393395

394396
// some dependencies e.g. @vue/compiler-* relies on NODE_ENV for getting
395397
// production-specific behavior, so set it early on
@@ -539,12 +541,12 @@ export async function resolveConfig(
539541
)
540542

541543
// resolve cache directory
542-
const pkgPath = lookupFile(resolvedRoot, [`package.json`], { pathOnly: true })
544+
const pkgDir = findNearestPackageData(resolvedRoot, packageCache)?.dir
543545
const cacheDir = normalizePath(
544546
config.cacheDir
545547
? path.resolve(resolvedRoot, config.cacheDir)
546-
: pkgPath
547-
? path.join(path.dirname(pkgPath), `node_modules/.vite`)
548+
: pkgDir
549+
? path.join(pkgDir, `node_modules/.vite`)
548550
: path.join(resolvedRoot, `.vite`),
549551
)
550552

@@ -681,7 +683,7 @@ export async function resolveConfig(
681683
return DEFAULT_ASSETS_RE.test(file) || assetsFilter(file)
682684
},
683685
logger,
684-
packageCache: new Map(),
686+
packageCache,
685687
createResolver,
686688
optimizeDeps: {
687689
disabled: 'build',

0 commit comments

Comments
 (0)
Please sign in to comment.