Skip to content

feat(bundlecheck): Add package exports analysis and installer - #496

Merged
aversini merged 5 commits into
mainfrom
feat(bundlecheck)-Add-package-exports-analysis-and-installer
Feb 1, 2026
Merged

aversini merged 5 commits into
mainfrom
feat(bundlecheck)-Add-package-exports-analysis-and-installer

Conversation

@aversini

@aversini aversini commented Feb 1, 2026

Copy link
Copy Markdown
Collaborator

This pull request adds support for retrieving and displaying the named exports of npm packages in the bundlecheck tool. It introduces a new API method, updates the CLI and caching logic to include export counts, and adds comprehensive tests for the new functionality.

New API & Documentation:

  • Added getPackageExports function to the public API, allowing users to fetch all named exports (including types) and runtime exports from an npm package by analyzing its TypeScript declarations. Documentation and usage examples were added to README.md. [1] [2] [3] [4]

CLI and Display Improvements:

  • Updated the CLI output in bundlecheck.ts to display a summary of named exports, showing either the export names, the count, or a fallback message, depending on the available data. [1] [2]

Core Logic & Caching:

  • Integrated named export counting into the bundle analysis process and cache storage. The bundle result now includes a namedExportCount field, which is stored and retrieved from the cache database. Migrations ensure backward compatibility for existing caches. [1] [2] [3] [4] [5] [6] [7] [8] [9]

Testing:

  • Added extensive unit tests for the new exports-installer logic and the getPackageExports API, covering scenarios for package installation, registry handling, cleanup, and export mapping. [1] [2] [3]

Introduce named-export analysis for npm packages by parsing TypeScript declaration files and a temporary installer to fetch packages.

- Add src/exports.ts: parses .d.ts files (with re-exports), extracts named/runtime exports and returns counts.
- Add src/exports-installer.ts: safely installs a package into a temp dir (pnpm preferred, npm fallback), validates registry, returns parsed exports and resolved version, and exposes an alias for backward compatibility.
- Add getPackageExports API in src/index.ts that installs the package and returns structured PackageExports (including runtime vs type-only counts).
- Integrate export info into bundling flow: bundler now imports getNamedExports and includes namedExportCount in BundleResult; display shows export summary.
- Persist named export count in cache: add named_export_count column (with migration), include value in getCachedResult/setCachedResult.
- Update README to document getPackageExports usage, types and examples.

These changes enable consumers to inspect package named exports (and runtime-only exports) and persist that metadata for faster subsequent checks.
Add comprehensive unit tests for export discovery and the package installer. Two new test suites were added: exports.test.ts covers parsing TypeScript declaration files (functions, classes, consts, types, interfaces, enums, re-exports, aliasing, common paths, error handling, circular import protection, sorting and deduplication), and exports-installer.test.ts covers installing packages in a temp dir (pnpm/npm selection, registry handling/validation, package.json writing, reading installed version, cleanup on success/failure, and calling getNamedExports). Also update index.test.ts to mock and test getPackageExports behavior (resolving versions, passing registry, and mapping exports/runtimeExports) by mocking installPackage.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request adds a comprehensive package exports analysis feature to the bundlecheck tool, enabling users to programmatically retrieve and display the named exports of npm packages by analyzing their TypeScript declarations.

Changes:

  • Introduces a new public API method getPackageExports() for fetching named exports and runtime exports from npm packages
  • Adds CLI display enhancements to show export counts in bundle analysis results
  • Implements core parsing logic for TypeScript declaration files with recursive re-export handling
  • Updates cache schema to store named export counts with backward-compatible migration

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
packages/bundlecheck/src/index.ts Adds getPackageExports() public API function, new types (PackageExport, PackageExports), and integrates namedExportCount into BundleStats
packages/bundlecheck/src/exports.ts Implements TypeScript declaration file parsing with regex-based export extraction, recursive re-export following, and duplicate detection
packages/bundlecheck/src/exports-installer.ts Provides package installation utilities with temporary directory management, registry validation, and pnpm/npm fallback logic
packages/bundlecheck/src/bundler.ts Integrates export counting into bundle analysis workflow by calling getNamedExports() and adding namedExportCount to results
packages/bundlecheck/src/cache.ts Extends cache schema with named_export_count column and implements backward-compatible migration for existing databases
packages/bundlecheck/src/bundlecheck.ts Updates CLI output to display export information with intelligent formatting (shows names, count, or fallback message)
packages/bundlecheck/src/__tests__/exports.test.ts Comprehensive unit tests covering all export types, edge cases (circular imports, aliasing, type detection), and error handling
packages/bundlecheck/src/__tests__/exports-installer.test.ts Tests for package installation, registry handling, pnpm/npm detection, cleanup, and security validation
packages/bundlecheck/src/__tests__/index.test.ts API integration tests for getPackageExports() covering version resolution, registry options, and export mapping
packages/bundlecheck/README.md Adds documentation for getPackageExports() API with usage examples, type definitions, and updates to BundleStats type

Comment thread packages/bundlecheck/src/exports.ts
Improve and standardize comments and JSDoc across the bundlecheck package. Changes include wrapping long comment lines, adding punctuation, clarifying regex/pattern explanations, adding small JSDoc blocks (e.g., migrations, parse helpers), and tidying comment wording in files: bundlecheck.ts, bundler.ts, cache.ts, defaults.ts, exports-installer.ts, exports.ts, and index.ts. These are non-functional, documentation-only changes aimed at readability and maintainability.
@aversini
aversini merged commit 316fd03 into main Feb 1, 2026
6 checks passed
@aversini
aversini deleted the feat(bundlecheck)-Add-package-exports-analysis-and-installer branch February 1, 2026 16:56
@aversini aversini mentioned this pull request Feb 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants