Skip to content

feat(styled-react): Add an automatic generation of a components.json file to be used in the primer-react/use-styled-react-import eslint plugin - #6848

Merged
jonrohan merged 6 commits into
mainfrom
generate_components_json
Sep 22, 2025
Merged

feat(styled-react): Add an automatic generation of a components.json file to be used in the primer-react/use-styled-react-import eslint plugin#6848
jonrohan merged 6 commits into
mainfrom
generate_components_json

Conversation

@jonrohan

@jonrohanjonrohan commented Sep 12, 2025

Copy link
Copy Markdown
Member

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

importcomponentsDatafrom'@primer/styled-react/components.json'with{type: 'json'}console.log(componentsData.components)// Array of component namesconsole.log(componentsData.utilities)// Array of utility namesconsole.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 a team as a code ownerSeptember 12, 2025 20:38
CopilotAI review requested due to automatic review settings September 12, 2025 20:38
@changeset-bot

changeset-botBot commented Sep 12, 2025

Copy link
Copy Markdown

🦋 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
NameType
@primer/styled-reactMinor

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-actionsgithub-actionsBot added the staff Author is a staff member label Sep 12, 2025

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

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.

FileDescription
packages/styled-react/script/generate-components-jsonNew Node.js script that parses the main index.tsx file and categorizes exports into components, utilities, and types
packages/styled-react/script/buildUpdated build script to include components.json generation step after rollup compilation
packages/styled-react/package.jsonAdded export path for "./components.json" to make the generated file importable by consumers
packages/styled-react/README.mdAdded 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']

CopilotAISep 12, 2025

Copy link

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

CopilotAISep 12, 2025

Copy link

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

CopilotAISep 12, 2025

Copy link

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

CopilotAISep 12, 2025

Copy link

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')

CopilotAISep 12, 2025

Copy link

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

github-actionsBot commented Sep 12, 2025

Copy link
Copy Markdown
Contributor

size-limit report 📦

PathSize
packages/react/dist/browser.esm.js89.42 KB (0%)
packages/react/dist/browser.umd.js89.63 KB (0%)

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
Copy Markdown
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
Copy Markdown
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>
@github-actions
github-actionsBottemporarily deployed to storybook-preview-6848 September 22, 2025 17:32 Inactive
Merged via the queue into main with commit 156903cSep 22, 2025
40 of 41 checks passed
@jonrohan
jonrohan deleted the generate_components_json branch September 22, 2025 17:40
@primerprimerBot mentioned this pull request Sep 22, 2025
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

staffAuthor is a staff member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@jonrohan@joshblack