-
Notifications
You must be signed in to change notification settings - Fork 842
Optimize @primer/octicons-react for codesplitting and tree-shaking #1245
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9c32a25
Initial plan
Copilot a5402f0
Plan: optimize octicons-react for codesplitting/treeshaking
Copilot 8c1a561
Emit per-icon modules, pre-transformed components, and ./* subpath ex…
Copilot eec5332
Add changeset for octicons-react codesplitting (minor)
Copilot de1096d
Remove stray npm lockfile and restore yarn.lock in octicons-react (#1…
Copilot c6db8e1
Apply suggestions from code review
mattcosta7 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,5 @@ | ||
| --- | ||
| "@primer/octicons-react": minor | ||
| --- | ||
|
|
||
| Optimize `@primer/octicons-react` for codesplitting and tree-shaking. Each icon is now emitted as its own module and exposed via a `./*` subpath export, so icons can be dynamically imported and code-split (e.g. `import('@primer/octicons-react/AlertIcon')`). The generated icons are now finished `React.forwardRef` components built on a shared `renderOcticon` runtime instead of runtime `createIconComponent` factory calls. Existing `import {AlertIcon}` and `import * as Octicons` usage continues to work unchanged. |
12 changes: 12 additions & 0 deletions
12
lib/octicons_react/__tests__/__fixtures__/dynamic-imports.mjs
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,12 @@ | ||
| // Fixture for the code-splitting test: static dynamic imports of per-icon | ||
| // subpaths so Rollup emits one chunk per icon. | ||
| export function load(name) { | ||
| switch (name) { | ||
| case 'AlertIcon': | ||
| return import('../../dist/icons/AlertIcon.mjs') | ||
| case 'RepoIcon': | ||
| return import('../../dist/icons/RepoIcon.mjs') | ||
| default: | ||
| return null | ||
| } | ||
| } |
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,50 @@ | ||
| const path = require('node:path') | ||
| const fs = require('node:fs') | ||
| const {rollup} = require('rollup') | ||
|
|
||
| const packageRoot = path.resolve(__dirname, '..') | ||
| const iconsDir = path.join(packageRoot, 'dist', 'icons') | ||
|
|
||
| test('emits one pre-transformed module per icon', () => { | ||
| const alert = fs.readFileSync(path.join(iconsDir, 'AlertIcon.mjs'), 'utf8') | ||
| // The generated icon is a finished forwardRef component using the shared | ||
| // renderOcticon runtime, not a runtime createIconComponent factory call. | ||
| expect(alert).toContain('React.forwardRef') | ||
| expect(alert).toContain('renderOcticon(') | ||
| expect(alert).not.toContain('createIconComponent') | ||
| // Per-icon declaration file is emitted alongside for subpath type resolution. | ||
| expect(fs.existsSync(path.join(iconsDir, 'AlertIcon.d.ts'))).toBe(true) | ||
| }) | ||
|
|
||
| test('the barrel is a pure re-export of the per-icon modules', () => { | ||
| const barrel = fs.readFileSync(path.join(packageRoot, 'dist', 'index.esm.mjs'), 'utf8') | ||
| expect(barrel).toContain("export { AlertIcon } from './icons/AlertIcon.mjs'") | ||
| // A pure re-export barrel contains no rendering logic of its own. | ||
| expect(barrel).not.toContain('forwardRef') | ||
| }) | ||
|
|
||
| test('package.json exports expose the "." barrel and a per-icon "./*" subpath', () => { | ||
| const pkg = require(path.join(packageRoot, 'package.json')) | ||
| expect(pkg.exports['.'].import).toBe('./dist/index.esm.mjs') | ||
| expect(pkg.exports['.'].require).toBe('./dist/index.umd.js') | ||
| expect(pkg.exports['./*'].import).toBe('./dist/icons/*.mjs') | ||
| expect(pkg.exports['./*'].types).toBe('./dist/icons/*.d.ts') | ||
| }) | ||
|
|
||
| test('dynamic subpath imports are code-split into separate chunks', async () => { | ||
| const bundle = await rollup({ | ||
| input: path.join(__dirname, '__fixtures__', 'dynamic-imports.mjs'), | ||
| external: ['react'] | ||
| }) | ||
| const {output} = await bundle.generate({format: 'esm'}) | ||
|
|
||
| // The entry plus one chunk per dynamically imported icon (and any shared | ||
| // runtime chunk). More than a single chunk proves the icons are code-split | ||
| // rather than bundled into the entry. | ||
| expect(output.length).toBeGreaterThan(1) | ||
|
|
||
| const entry = output.find(chunk => chunk.isEntry) | ||
| // The entry itself must not inline any icon path data. | ||
| expect(entry.code).not.toContain('octicon octicon-alert') | ||
| expect(entry.code).not.toContain('octicon octicon-repo') | ||
| }) |
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
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.
Uh oh!
There was an error while loading. Please reload this page.