@@ -45,13 +45,22 @@ interface GeneratedNoticeConfigEntry {
4545}
4646
4747interface PackageJson {
48+ readonly bin ?: unknown ;
49+ readonly browser ?: unknown ;
4850 readonly dependencies ?: Readonly < Record < string , string > > ;
51+ readonly exports ?: unknown ;
4952 readonly homepage ?: unknown ;
5053 readonly license ?: unknown ;
5154 readonly licenses ?: unknown ;
55+ readonly main ?: unknown ;
56+ readonly module ?: unknown ;
5257 readonly name ?: unknown ;
5358 readonly optionalDependencies ?: Readonly < Record < string , string > > ;
5459 readonly repository ?: unknown ;
60+ readonly sass ?: unknown ;
61+ readonly style ?: unknown ;
62+ readonly types ?: unknown ;
63+ readonly typings ?: unknown ;
5564 readonly version ?: unknown ;
5665}
5766
@@ -510,6 +519,73 @@ function dependencyNames(packageJson: PackageJson): ReadonlyArray<string> {
510519 ] . sort ( ( left , right ) => left . localeCompare ( right ) ) ;
511520}
512521
522+ interface ExportTarget {
523+ readonly path : string ;
524+ readonly underTypesCondition : boolean ;
525+ }
526+
527+ function exportTargets ( value : unknown , underTypesCondition = false ) : ReadonlyArray < ExportTarget > {
528+ if ( typeof value === "string" ) return [ { path : value , underTypesCondition } ] ;
529+ if ( Array . isArray ( value ) )
530+ return value . flatMap ( ( item ) => exportTargets ( item , underTypesCondition ) ) ;
531+ if ( value === null || typeof value !== "object" ) return [ ] ;
532+ return Object . entries ( value ) . flatMap ( ( [ key , item ] ) =>
533+ exportTargets ( item , underTypesCondition || key . startsWith ( "types" ) || key === "typings" ) ,
534+ ) ;
535+ }
536+
537+ /** Declarations, or the package's own manifest that many packages also export. Neither is code. */
538+ function isTypeDeclaration ( target : ExportTarget ) : boolean {
539+ return (
540+ target . underTypesCondition ||
541+ / \. d \. [ c m ] ? t s $ / u. test ( target . path ) ||
542+ target . path === "./package.json"
543+ ) ;
544+ }
545+
546+ function isDeclared ( value : unknown ) : boolean {
547+ if ( typeof value === "string" ) return value . trim ( ) !== "" ;
548+ return value !== null && typeof value === "object" && Object . keys ( value ) . length > 0 ;
549+ }
550+
551+ async function fileExists ( path : string ) : Promise < boolean > {
552+ try {
553+ await NodeFSP . access ( path ) ;
554+ return true ;
555+ } catch {
556+ return false ;
557+ }
558+ }
559+
560+ /**
561+ * A package that only describes types (DefinitelyTyped, csstype, undici-types): it declares types
562+ * and has no JS entry, so nothing of it ships. A types condition or a name with "types" in it is not
563+ * enough on its own: `@shikijs/types` exports real code next to its declarations.
564+ */
565+ async function isTypeOnlyPackage ( packageJson : PackageJson , packageRoot : string ) : Promise < boolean > {
566+ const targets = exportTargets ( packageJson . exports ) ;
567+ const declaresTypes =
568+ isDeclared ( packageJson . types ) ||
569+ isDeclared ( packageJson . typings ) ||
570+ targets . some ( isTypeDeclaration ) ;
571+ if ( ! declaresTypes ) return false ;
572+ const entries = [
573+ packageJson . main ,
574+ packageJson . module ,
575+ packageJson . browser ,
576+ packageJson . bin ,
577+ packageJson . style ,
578+ packageJson . sass ,
579+ ] ;
580+ if ( entries . some ( isDeclared ) || targets . some ( ( target ) => ! isTypeDeclaration ( target ) ) ) {
581+ return false ;
582+ }
583+ // Without `main` or `exports`, Node still loads the package's index.js.
584+ return packageJson . main !== undefined || packageJson . exports !== undefined
585+ ? true
586+ : ! ( await fileExists ( NodePath . join ( packageRoot , "index.js" ) ) ) ;
587+ }
588+
513589async function collectProductionDependencyPackages (
514590 packageManifests : ReadonlyArray < ThirdPartyLicensePackageManifest > ,
515591) : Promise < PackageCollection > {
@@ -530,7 +606,11 @@ async function collectProductionDependencyPackages(
530606 const dependencyPackageJsonPath = NodePath . join ( resolved . packageRoot , "package.json" ) ;
531607 const name =
532608 typeof resolved . packageJson . name === "string" ? resolved . packageJson . name : dependencyName ;
533- if ( ! name . startsWith ( FIRST_PARTY_PACKAGE_PREFIX ) ) {
609+ // Type-only packages ship nothing, but what they depend on may, so the walk goes on below.
610+ if (
611+ ! name . startsWith ( FIRST_PARTY_PACKAGE_PREFIX ) &&
612+ ! ( await isTypeOnlyPackage ( resolved . packageJson , resolved . packageRoot ) )
613+ ) {
534614 const identity = packageIdentity ( resolved . packageJson , resolved . packageRoot ) ;
535615 const existing = collection . byIdentity . get ( identity ) ;
536616 if ( existing ) {
0 commit comments