-
Notifications
You must be signed in to change notification settings - Fork 59
Add ESM bundle support #1280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rezrah
wants to merge
13
commits into
main
Choose a base branch
from
rezrah/esm-lib
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add ESM bundle support #1280
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4d32d7c
add ESM support
rezrah 81363f1
address copilot feedback
rezrah 46aaa4c
fix icons
rezrah 2128a41
fix linter
rezrah 16649fe
fix octicons externals
rezrah 8c46ecb
fix typing erroprs
rezrah 1050da2
Merge branch 'main' into rezrah/esm-lib
rezrah 7e087df
fix cjs path for astro
rezrah 51bbc62
upgrade astro to v6 and switch to ESM
rezrah 7d481d6
update astro to support matrix strategy for cjs and esm
rezrah 26c41af
add stylesheets file to side effects
rezrah 9af2037
Merge branch 'main' of github.com:primer/brand into rezrah/esm-lib
rezrah 15aebdb
upgrade to vite 8
rezrah File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| --- | ||
| '@primer/react-brand': minor | ||
| --- | ||
|
|
||
| Added ESM library support. | ||
|
|
||
| A new `@primer/react-brand/esm` entry point is now available for modern bundlers (E.g. Vite, Next.js, etc). | ||
|
|
||
| Components imported from this path automatically include the minimum CSS needed. Separate global stylesheet imports are no longer required. | ||
|
|
||
| ```js | ||
| import {Hero, ThemeProvider} from '@primer/react-brand/esm' | ||
| ``` | ||
|
|
||
| ESM features are opt-in, and the previous UMD bundle (`@primer/react-brand`) and global CSS (`@primer/react-brand/lib/css/main.css`) continue to work as before. | ||
|
|
||
| We recommend switching to ESM as soon as possible, as it will eventually become the default in future. | ||
|
|
||
| 🔗 [Get started with ESM](https://primer.style/brand/introduction/getting-started/esm) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| name: Check bundle size | ||
|
|
||
| on: | ||
| pull_request: | ||
|
|
||
| jobs: | ||
| size: | ||
| if: ${{ github.repository == 'primer/brand' }} | ||
| name: Report bundle size | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Checkout PR branch | ||
| uses: actions/checkout@v4 | ||
| with: | ||
| fetch-depth: 0 | ||
|
|
||
| - name: Create main branch worktree | ||
| run: git worktree add /tmp/main ${{ github.event.pull_request.base.sha }} | ||
|
|
||
| - name: Set up Node | ||
| uses: actions/setup-node@v4 | ||
| with: | ||
| node-version: 24 | ||
|
|
||
| - name: Caching dependencies | ||
| uses: actions/cache@v4 | ||
| with: | ||
| path: ~/.npm | ||
| key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} | ||
| restore-keys: | | ||
| ${{ runner.os }}-node- | ||
|
|
||
| - name: Install and build (PR) | ||
| run: | | ||
| npm ci | ||
| npm run build:lib | ||
|
|
||
| - name: Measure bundle size (PR) | ||
| run: npx size-limit --json > /tmp/pr-sizes.json | ||
|
|
||
| - name: Install and build (main branch) | ||
| working-directory: /tmp/main | ||
| run: | | ||
| npm ci | ||
| cp ${{ github.workspace }}/.size-limit.js .size-limit.js | ||
| npm run build:lib || true | ||
|
|
||
rezrah marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| - name: Measure bundle size (main branch) | ||
| working-directory: /tmp/main | ||
| run: npx size-limit --json > /tmp/main-sizes.json 2>/dev/null || echo "[]" > /tmp/main-sizes.json | ||
|
|
||
| - name: Generate size report | ||
| id: size-report | ||
| run: | | ||
| REPORT=$(node packages/repo-configs/scripts/bundle-size-report.js /tmp/pr-sizes.json /tmp/main-sizes.json) | ||
| echo "comment-body<<EOF" >> "$GITHUB_OUTPUT" | ||
| echo "$REPORT" >> "$GITHUB_OUTPUT" | ||
| echo "EOF" >> "$GITHUB_OUTPUT" | ||
|
|
||
| - name: Add or update bundle size comment | ||
| uses: phulsechinmay/rewritable-pr-comment@v0.3.0 | ||
| with: | ||
| message: ${{ steps.size-report.outputs.comment-body }} | ||
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
| COMMENT_IDENTIFIER: 'bundle-size-report' | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| module.exports = [ | ||
| { | ||
| name: 'UMD — full bundle (JS)', | ||
| path: 'packages/react/lib/index.js', | ||
| }, | ||
| { | ||
| name: 'UMD — full bundle (CSS)', | ||
| path: 'packages/react/lib/css/main.css', | ||
| }, | ||
| { | ||
| name: 'ESM — full bundle (JS + CSS)', | ||
| path: 'packages/react/esm/**/*.{js,css}', | ||
| }, | ||
| { | ||
| name: 'ESM — tree-shaken simple (Button)', | ||
| path: 'packages/react/esm/index.esm.js', | ||
| import: '{ Button }', | ||
| modifyEsbuildConfig(config) { | ||
| config.external = ['react', 'react-dom'] | ||
| return config | ||
| }, | ||
| }, | ||
| { | ||
| name: 'ESM — tree-shaken complex (ActionMenu)', | ||
| path: 'packages/react/esm/index.esm.js', | ||
| import: '{ ActionMenu }', | ||
| modifyEsbuildConfig(config) { | ||
| config.external = ['react', 'react-dom'] | ||
| return config | ||
| }, | ||
| }, | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| --- | ||
| title: Getting started | ||
| description: Primer Brand is open-sourced on GitHub and available on npm. | ||
| menu-position: 1 | ||
| keywords: ['accessibility', 'introduction', 'simple'] | ||
| show-tabs: true | ||
| tab-label: ESM | ||
| --- | ||
|
|
||
| Using ESM, you will load only the React components and CSS you need. | ||
|
|
||
| We recommend using ESM if your project uses compatible bundlers or frameworks like Vite or Next.js. | ||
|
|
||
| ### 1. Install Primer Brand | ||
|
|
||
| ```shell | ||
| npm install @primer/react-brand | ||
| ``` | ||
|
|
||
| ### 2. Import components from the ESM folder | ||
|
|
||
| ```js | ||
| import {Hero, ThemeProvider} from '@primer/react-brand/esm' | ||
| ``` | ||
|
|
||
| Each component's styles are automatically included when you import the component. | ||
|
|
||
| <Note> | ||
| <p> | ||
| Do not combine this approach with the global <code>lib/css/main.css</code> import. | ||
| </p> | ||
|
|
||
| <p>Choose one approach or the other to avoid loading duplicate styles.</p> | ||
| </Note> | ||
|
|
||
| ### 3. Import the font assets | ||
|
|
||
| The Mona Sans typeface still needs to be loaded separately: | ||
|
|
||
| ```js | ||
| import '@primer/react-brand/fonts/fonts.css' | ||
| ``` | ||
|
|
||
| ### 4. Use the `ThemeProvider` | ||
|
|
||
| ```js | ||
| import {ThemeProvider} from '@primer/react-brand/esm' | ||
|
|
||
| function App() { | ||
| return ( | ||
| <ThemeProvider> | ||
| <div>...</div> | ||
| </ThemeProvider> | ||
| ) | ||
| } | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| --- | ||
| title: Introduction | ||
| title: Guides | ||
| description: Get started with Primer Brand | ||
| menu-position: 1 | ||
| menu-position: 2 | ||
| --- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| /// <reference types="next" /> | ||
| /// <reference types="next/image-types/global" /> | ||
| /// <reference path="./.next/types/routes.d.ts" /> | ||
| import "./.next/dev/types/routes.d.ts"; | ||
|
|
||
| // NOTE: This file should not be edited | ||
| // see https://nextjs.org/docs/app/api-reference/config/typescript for more information. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.