👉 https://dev.to/lukasbombach/how-to-write-a-tree-shakable-component-library-4ied
This MonoRepo has 2 packages
appandui-library
The app is a Next.js app that consumes the ui-library in the index.tsx page while the ui-library provides bundled components.
You can run the app by switching to the app folder and run yarn dev or yarn start*
* both packages have postinstall script that runs yarn build in each project
cd packages/app
yarn devYou can also run StoryBook from within the ui-library
cd packages/ui-library
yarn storybookYou can change or add new components by running
cd packages/ui-library
yarn build -win another terminal you can run the app in parallel and it will auto-update with hot-module-reloading
cd packages/app
yarn devYou can very simple see that the app actually does tree-shake your code by opening this project in a code
editor and searching all files in packages/app/.next and
- searching for the string
I SHOULD BE HERE, which should be found. This string is part of theButtoncomponent which has been loaded into the app - then search for the string
I MUST NOT BE HEREThis string is part of theLinkcomponent which has not been loaded into the app even though it has been bundled in the ui library and should not be bundled in the app. - check out the file
packages/ui-library/lib/index.esm.jswhich is the bundled file of the ui-library which includes all components