@@ -32,7 +32,6 @@ import {
32
32
copyDir ,
33
33
emptyDir ,
34
34
joinUrlSegments ,
35
- lookupFile ,
36
35
normalizePath ,
37
36
requireResolveFromRootWithFallback ,
38
37
} from './utils'
@@ -52,8 +51,8 @@ import {
52
51
initDepsOptimizer ,
53
52
} from './optimizer'
54
53
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'
57
56
import { ensureWatchPlugin } from './plugins/ensureWatch'
58
57
import { ESBUILD_MODULES_TARGET , VERSION } from './constants'
59
58
import { resolveChokidarOptions } from './watch'
@@ -578,7 +577,11 @@ export async function build(
578
577
const format = output . format || ( cjsSsrBuild ? 'cjs' : 'es' )
579
578
const jsExt =
580
579
ssrNodeBuild || libOptions
581
- ? resolveOutputJsExtension ( format , getPkgJson ( config . root ) ?. type )
580
+ ? resolveOutputJsExtension (
581
+ format ,
582
+ findNearestPackageData ( config . root , config . packageCache ) ?. data
583
+ . type ,
584
+ )
582
585
: 'js'
583
586
return {
584
587
dir : outDir ,
@@ -595,7 +598,14 @@ export async function build(
595
598
? `[name].${ jsExt } `
596
599
: libOptions
597
600
? ( { 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
+ )
599
609
: path . posix . join ( options . assetsDir , `[name]-[hash].${ jsExt } ` ) ,
600
610
chunkFileNames : libOptions
601
611
? `[name]-[hash].${ jsExt } `
@@ -742,10 +752,6 @@ function prepareOutDir(
742
752
}
743
753
}
744
754
745
- function getPkgJson ( root : string ) : PackageData [ 'data' ] {
746
- return JSON . parse ( lookupFile ( root , [ 'package.json' ] ) || `{}` )
747
- }
748
-
749
755
function getPkgName ( name : string ) {
750
756
return name ?. [ 0 ] === '@' ? name . split ( '/' ) [ 1 ] : name
751
757
}
@@ -769,15 +775,16 @@ export function resolveLibFilename(
769
775
entryName : string ,
770
776
root : string ,
771
777
extension ?: JsExt ,
778
+ packageCache ?: PackageCache ,
772
779
) : string {
773
780
if ( typeof libOptions . fileName === 'function' ) {
774
781
return libOptions . fileName ( format , entryName )
775
782
}
776
783
777
- const packageJson = getPkgJson ( root )
784
+ const packageJson = findNearestPackageData ( root , packageCache ) ?. data
778
785
const name =
779
786
libOptions . fileName ||
780
- ( typeof libOptions . entry === 'string'
787
+ ( packageJson && typeof libOptions . entry === 'string'
781
788
? getPkgName ( packageJson . name )
782
789
: entryName )
783
790
@@ -786,7 +793,7 @@ export function resolveLibFilename(
786
793
'Name in package.json is required if option "build.lib.fileName" is not provided.' ,
787
794
)
788
795
789
- extension ??= resolveOutputJsExtension ( format , packageJson . type )
796
+ extension ??= resolveOutputJsExtension ( format , packageJson ? .type )
790
797
791
798
if ( format === 'cjs' || format === 'es' ) {
792
799
return `${ name } .${ extension } `
0 commit comments