Skip to content

Conversation

@jonrohan
Copy link
Member

@jonrohan jonrohan commented Sep 12, 2025

This PR adds automatic generation of a components.json file during the build process for the @primer/styled-react package. This provides consumers with a machine-readable list of all exported components, utilities, and types.

Problem

Previously, there was no programmatic way for consumers to discover what components and utilities are available in the @primer/styled-react package without manually inspecting the source code or documentation.

Solution

Added a build-time script that automatically parses the main index.tsx file and generates a structured JSON file containing:

  • All React components (44 items)
  • Utility functions (6 items)
  • TypeScript types (3 items)

Usage

import componentsData from '@primer/styled-react/components.json' with {type: 'json'}

console.log(componentsData.components) // Array of component names
console.log(componentsData.utilities)  // Array of utility names
console.log(componentsData.types)      // Array of type names

Changelog

New

  • Added script/generate-components-json build script to automatically extract and categorize exports
  • Added components.json export path to package.json exports map
  • Added automatic generation of /dist/components.json during build process
  • Added documentation section in README.md explaining the components.json feature

Changed

  • Modified script/build to include components.json generation step
  • Updated README.md with usage instructions for the new components.json file

Removed

  • N/A

Rollout strategy

  • Patch release
  • Minor release
  • Major release; if selected, include a written rollout or migration plan
  • None; if selected, include a brief description as to why

Rationale: This is a new feature that adds functionality without breaking existing APIs. The components.json file is additive and provides new capabilities for package consumers.

Testing & Reviewing

To test this PR:

  1. Build the package: Run npm run build in packages/styled-react/
  2. Verify generation: Check that dist/components.json is created with correct structure
  3. Test imports: Verify the file can be imported using @primer/styled-react/components.json
  4. Validate content: Confirm the generated JSON contains all expected components (44), utilities (6), and types (3)

Key files to review:

  • packages/styled-react/script/generate-components-json - The extraction and generation logic
  • packages/styled-react/script/build - Integration into build process
  • packages/styled-react/package.json - Export path configuration
  • packages/styled-react/README.md - Updated documentation

Example generated structure:

{
  "components": ["ActionList", "Avatar", "Box", ...],
  "utilities": ["merge", "sx", "theme", ...],
  "types": ["BetterSystemStyleObject", "BoxProps", "SxProp"],
}

Merge checklist

  • Added/updated tests - Build process validates the generation works correctly
  • Added/updated documentation - README.md updated with usage instructions
  • Added/updated previews (Storybook) - N/A for build tooling
  • Changes are SSR compatible - Static JSON generation, no runtime impact
  • Tested in Chrome - N/A for build tooling
  • Tested in Firefox - N/A for build tooling
  • Tested in Safari - N/A for build tooling
  • Tested in Edge - N/A for build tooling
  • (GitHub staff only) Integration tests pass at github/github - N/A

@jonrohan jonrohan requested a review from joshblack September 12, 2025 20:38
@jonrohan jonrohan requested a review from a team as a code owner September 12, 2025 20:38
Copilot AI review requested due to automatic review settings September 12, 2025 20:38
@changeset-bot
Copy link

changeset-bot bot commented Sep 12, 2025

🦋 Changeset detected

Latest commit: 2a84655

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@primer/styled-react Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added the staff Author is a staff member label Sep 12, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds automatic generation of a components.json file for the @primer/styled-react package, providing consumers with a machine-readable list of all exported components, utilities, and types. The feature extracts exports from the main index file during build time and categorizes them into components, utilities, and types with package metadata.

Key changes:

  • Added build-time script to parse exports and generate structured JSON
  • Integrated JSON generation into the build process
  • Added export path configuration for the components.json file

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.

File Description
packages/styled-react/script/generate-components-json New Node.js script that parses the main index.tsx file and categorizes exports into components, utilities, and types
packages/styled-react/script/build Updated build script to include components.json generation step after rollup compilation
packages/styled-react/package.json Added export path for "./components.json" to make the generated file importable by consumers
packages/styled-react/README.md Added documentation section explaining the components.json feature with usage examples

const types = [...typeExports] // All type exports go into types

// Known utility patterns
const utilityPatterns = ['merge', 'sx', 'theme', 'themeGet', 'useColorSchemeVar', 'useTheme']
Copy link

Copilot AI Sep 12, 2025

Choose a reason for hiding this comment

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

The utility patterns are hardcoded in the script. Consider moving this list to a configuration file or making it discoverable through naming conventions to reduce maintenance overhead when utilities are added or removed.

Copilot uses AI. Check for mistakes.
const typeExports = []

// Extract regular exports (handle multi-line exports)
const exportRegex = /export\s*\{\s*([\s\S]*?)\s*\}/g
Copy link

Copilot AI Sep 12, 2025

Choose a reason for hiding this comment

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

The regex patterns for parsing exports may not handle all edge cases of TypeScript/JavaScript syntax. Consider using a proper AST parser like @babel/parser or typescript compiler API for more robust parsing.

Copilot uses AI. Check for mistakes.
}

// Extract type exports (handle multi-line exports)
const typeExportRegex = /export\s+type\s*\{\s*([\s\S]*?)\s*\}/g
Copy link

Copilot AI Sep 12, 2025

Choose a reason for hiding this comment

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

The regex patterns for parsing exports may not handle all edge cases of TypeScript/JavaScript syntax. Consider using a proper AST parser like @babel/parser or typescript compiler API for more robust parsing.

Copilot uses AI. Check for mistakes.
}

// Extract direct exports
const directExportRegex = /export\s+(?:const|let|var|function|class)\s+(\w+)/g
Copy link

Copilot AI Sep 12, 2025

Choose a reason for hiding this comment

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

The regex patterns for parsing exports may not handle all edge cases of TypeScript/JavaScript syntax. Consider using a proper AST parser like @babel/parser or typescript compiler API for more robust parsing.

Copilot uses AI. Check for mistakes.

async function generateComponentsJson() {
try {
const srcPath = path.join(__dirname, '..', 'src', 'index.tsx')
Copy link

Copilot AI Sep 12, 2025

Choose a reason for hiding this comment

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

The source file path is hardcoded. Consider making this configurable or deriving it from package.json to improve flexibility if the entry point changes.

Suggested change
const srcPath = path.join(__dirname, '..', 'src', 'index.tsx')
// Read package.json to get the entry point
const pkgJsonPath = path.join(__dirname, '..', 'package.json')
const pkgJsonContent = await fs.readFile(pkgJsonPath, 'utf-8')
const pkgJson = JSON.parse(pkgJsonContent)
// Use 'main' field, fallback to 'src/index.tsx' if not present
const entryRelative = pkgJson.main || 'src/index.tsx'
const srcPath = path.join(__dirname, '..', entryRelative)

Copilot uses AI. Check for mistakes.
@github-actions
Copy link
Contributor

github-actions bot commented Sep 12, 2025

size-limit report 📦

Path Size
packages/react/dist/browser.esm.js 89.42 KB (0%)
packages/react/dist/browser.umd.js 89.63 KB (0%)

@github-actions github-actions bot requested a deployment to storybook-preview-6848 September 12, 2025 20:43 Abandoned
@github-actions github-actions bot temporarily deployed to storybook-preview-6848 September 12, 2025 20:51 Inactive
@jonrohan jonrohan enabled auto-merge September 15, 2025 14:50
@jonrohan jonrohan disabled auto-merge September 15, 2025 14:50
Comment on lines 113 to 120
metadata: {
package: '@primer/styled-react',
description: 'Exported components and utilities from the Primer styled-react package',
totalComponents: categorized.components.length,
totalUtilities: categorized.utilities.length,
totalTypes: categorized.types.length,
generatedAt: new Date().toISOString().split('T')[0],
},
Copy link
Member

Choose a reason for hiding this comment

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

Do we need any of this for the rule? Wasn't sure if we needed this kind of metadata or not

}

// Extract type exports (handle multi-line exports)
const typeExportRegex = /export\s+type\s*\{\s*([\s\S]*?)\s*\}/g
Copy link
Member

Choose a reason for hiding this comment

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

Would it be possible to match what the copilot comment left and use AST for parsing instead of regex? Would help make things more reliable, I think, if we want to depend on this file

Co-authored-by: Josh Black <joshblack@users.noreply.github.com>
@jonrohan jonrohan requested a review from joshblack September 16, 2025 20:14
Merged via the queue into main with commit 156903c Sep 22, 2025
40 of 41 checks passed
@jonrohan jonrohan deleted the generate_components_json branch September 22, 2025 17:40
@primer primer bot mentioned this pull request Sep 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

staff Author is a staff member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants