Split icons to separate files for ESM tree-shaking#8
Split icons to separate files for ESM tree-shaking#8t3rminus wants to merge 6 commits intoRaresAil:mainfrom
Conversation
[IMP] Split rollup into CJS (one file) and ESM (split files)
There was a problem hiding this comment.
Pull request overview
This PR restructures the build output to enable ESM tree-shaking by splitting individual icon components into separate files, reducing bundle sizes from 1.11 MB to 440.2 KB when only specific icons are imported.
Changes:
- Modified Rollup configuration to generate separate files for each icon in the ESM build
- Added post-build processing to conditionally re-add strokeWidth parameter only to icons that use it
- Updated package.json with sideEffects field to support tree-shaking optimization
Reviewed changes
Copilot reviewed 4 out of 6 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| rollup.config.js | Reconfigured ESM output to use multiple entry points (one per icon file) instead of a single bundle |
| post-build-icons.js | Added logic to selectively restore strokeWidth parameter to icon components that reference it |
| templates/svg.js | Removed strokeWidth from default function parameters (restored conditionally in post-build) |
| package.json | Added glob dependency, sideEffects configuration, and changed yarn to npm in build script |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
rollup.config.js
Outdated
| // file, so e.g. src/nested/foo.js becomes nested/foo | ||
| path.relative('src', file.slice(0, file.length - path.extname(file).length)), | ||
| // This expands the relative paths to absolute paths, so e.g. | ||
| // src/nested/foo becomes /project/src/nested/foo.js |
There was a problem hiding this comment.
The comment example references '.js' files but the actual glob pattern matches '.tsx' files. Update the comment to use '.tsx' in the example (e.g., 'src/nested/foo.tsx becomes nested/foo') for consistency.
| // file, so e.g. src/nested/foo.js becomes nested/foo | |
| path.relative('src', file.slice(0, file.length - path.extname(file).length)), | |
| // This expands the relative paths to absolute paths, so e.g. | |
| // src/nested/foo becomes /project/src/nested/foo.js | |
| // file, so e.g. src/nested/foo.tsx becomes nested/foo | |
| path.relative('src', file.slice(0, file.length - path.extname(file).length)), | |
| // This expands the relative paths to absolute paths, so e.g. | |
| // src/nested/foo becomes /project/src/nested/foo.tsx |
There was a problem hiding this comment.
The output file (src/nested/foo -> /project/src/nested/foo.js) is correctly js, as the compiled output file type will be js
The input line (src/nested/foo.js) has been resolved so it reads src/nested/foo.tsx
rollup.config.js
Outdated
| // file, so e.g. src/nested/foo.js becomes nested/foo | ||
| path.relative('src', file.slice(0, file.length - path.extname(file).length)), | ||
| // This expands the relative paths to absolute paths, so e.g. | ||
| // src/nested/foo becomes /project/src/nested/foo.js |
There was a problem hiding this comment.
The comment example shows the transformation to '.js' but the actual files being processed are '.tsx'. Update the comment to reflect '.tsx' in the example for accuracy.
| // file, so e.g. src/nested/foo.js becomes nested/foo | |
| path.relative('src', file.slice(0, file.length - path.extname(file).length)), | |
| // This expands the relative paths to absolute paths, so e.g. | |
| // src/nested/foo becomes /project/src/nested/foo.js | |
| // file, so e.g. src/nested/foo.tsx becomes nested/foo | |
| path.relative('src', file.slice(0, file.length - path.extname(file).length)), | |
| // This expands the relative paths to absolute paths, so e.g. | |
| // src/nested/foo becomes /project/src/nested/foo.tsx |
package.json
Outdated
| "scripts": { | ||
| "build:icons": "rimraf ./src/icons && svgr ./src/svg && node post-build-icons.js", | ||
| "build": "rimraf ./dist && yarn run build:icons && rollup -c --bundleConfigAsCjs", | ||
| "build": "rimraf ./dist && npm run build:icons && rollup -c --bundleConfigAsCjs", |
There was a problem hiding this comment.
The build script was changed from 'yarn run' to 'npm run', but other scripts in the repository may still use yarn. This creates inconsistency in the build tooling. Consider using a consistent package manager throughout all scripts or document the reason for this change.
| "build": "rimraf ./dist && npm run build:icons && rollup -c --bundleConfigAsCjs", | |
| "build": "rimraf ./dist && yarn run build:icons && rollup -c --bundleConfigAsCjs", |
This PR splits up the icons into separate files in the ESM version of the build to allow for efficient tree-shaking when creating application bundles. Unused icons will be pruned from the final bundle dramatically reducing file size and loading time.
Before:
./node_modules/@raresail/react-ionicons/dist/esm/index.js (1.11 MB)
After:
./static/chunks/*.js (Count: 289)
Total size: 440.2 KB