Skip to content
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

feat(icon): review icons bundle #477

Merged
merged 38 commits into from
Sep 17, 2024
Merged

feat(icon): review icons bundle #477

merged 38 commits into from
Sep 17, 2024

Conversation

epessina
Copy link
Contributor

@epessina epessina commented Jun 3, 2024

Description

This PR aims at changing how icons are served and bundled.

The main problem with how icons are handled now is the bundle size: the <Icon /> component works by accepting the name of the icon which is resolved from a dynamic map that contains all the SVGs of three different icon packs. This approach results in all the SVGs being added to the library bundle, which subsequent bundlers cannot tree shake, given that at runtime any of the values of the aforementioned map could be used (in the bundle of applications using it, the library weights at least 6 MB regardless of the number of used components).

To solve this issue, this PR fundamentally changes how the <Icon /> component works, making it a wrapper that accepts the SVG to render as a React function component. This way we can provide a standard interface while leaving the possibility to load any SVG asset without adding bulk to the bundle size.
It follows an example of the usage of the new component.

import { Icon } from "@mia-platform-internal/console-design-system-react"
import MySvg from "./assets/my-svg.svg?react"

const App = () => <Icon color="#ce2020" size={48} component={MySvg} />

To still provide the user with a set of "recommended" icons, a new @mia-platform-internal/console-design-system-react/icons sub-package has been added to the final NPM package. The package contains the icons from the same three packages that were shipped before (Ant Design, Phosphor, and Feather) as well as a set of Mia-Platform-specific custom icons.

To make dynamic import of single icons possible, each icon in the package has its own file (more precisely a set of three files: the esm index, the cjs index and the declarations file).

Therefore, the icon components can be accessed from the package as named imports:

import { PiAddressBook } from "@mia-platform-internal/console-design-system-react/icons/pi/PiAddressBook"
import { FiHome } from "@mia-platform-internal/console-design-system-react/icons/fi/FiHome"
import { AiFillAlert } from "@mia-platform-internal/console-design-system-react/icons/ai/AiFillAlert"
import { MiMiaPlatform } from "@mia-platform-internal/console-design-system-react/icons/mi/MiMiaPlatform"

or as dynamic imports:

import('@mia-platform-internal/console-design-system-react/icons/mi/MiMiaPlatformColored')
  .then(({ MiMiaPlatformColored }) => { /* do something */ })

The icons sub-package can be automatically constructed by launching the build-icons script.

On top of this, the PR upgrades the vite-plugin-svgr dev dependency to the latest major (version 4.2.0).

Note that this PR is breaking since it changes the interface of the <Icon /> component. Migrating, however, is simple enough:

import { Icon } from "@mia-platform-internal/console-design-system-react"
+ import { PiAddressBook } from "@mia-platform-internal/console-design-system-react/icons/pi/PiAddressBook"

- const App = () =>  <Icon name="PiAddressBook" />
+ const App = () =>  <Icon component={PiAddressBook} />

Checklist

  • commit message and branch name follow conventions
  • tests are included
  • changes are accessible and documented from components stories
  • typings are updated or integrated accordingly with your changes
  • all added files include Apache 2.0 license
  • you are not committing extraneous files or sensible data
  • the browser console does not have any logged errors
  • necessary labels have been applied to this pull request (enhancement, bug, ecc.)

@epessina epessina added Icon component Icon component and hooks related activities breaking labels Jun 3, 2024
@epessina epessina force-pushed the feat/icons-bundle branch from 5fd6326 to 63c3035 Compare June 4, 2024 15:11
@epessina epessina self-assigned this Jun 4, 2024
@epessina epessina marked this pull request as ready for review June 5, 2024 06:45
@epessina epessina requested a review from a team as a code owner June 5, 2024 06:45
@epessina epessina mentioned this pull request Jun 18, 2024
9 tasks
@epessina epessina force-pushed the feat/icons-bundle branch from 8ae7a76 to da486e9 Compare July 1, 2024 12:07
Copy link
Contributor

@danielemarostica danielemarostica left a comment

Choose a reason for hiding this comment

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

LGTM

fredmaggiowski
fredmaggiowski previously approved these changes Jul 1, 2024
Copy link
Member

@fredmaggiowski fredmaggiowski left a comment

Choose a reason for hiding this comment

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

LGTM

@epessina epessina dismissed stale reviews from danielemarostica and fredmaggiowski via d992366 July 2, 2024 06:57
Danielecina
Danielecina previously approved these changes Jul 2, 2024
davidebianchi
davidebianchi previously approved these changes Jul 2, 2024
Copy link
Member

@davidebianchi davidebianchi left a comment

Choose a reason for hiding this comment

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

LGTM

@Danielecina
Copy link
Member

Danielecina commented Jul 4, 2024

I think you should wait before merging this PR. We would be testing a solution to async import specific icon

@epessina epessina dismissed stale reviews from davidebianchi and Danielecina via 312257b July 13, 2024 14:22
@epessina epessina force-pushed the feat/icons-bundle branch from d992366 to 312257b Compare July 13, 2024 14:22
@epessina epessina force-pushed the feat/icons-bundle branch from 312257b to 87ce731 Compare July 22, 2024 07:01
@epessina epessina force-pushed the feat/icons-bundle branch from c53d26f to 0fe0004 Compare July 23, 2024 12:12
@danibix95
Copy link

Hi! Is this PR ready to be merged?

@fredmaggiowski fredmaggiowski merged commit a35d8f5 into main Sep 17, 2024
6 checks passed
@fredmaggiowski fredmaggiowski deleted the feat/icons-bundle branch September 17, 2024 10:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking Icon component Icon component and hooks related activities
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants