Appearance
Modular Export Single Entry For Component Library
Status: Approved
Decision Date: 27/10/2022
Scope: codey-components
Context
- Current situation:
- The components are split into folders (e.g.
./src/components/button
) to represent a module. This is done to have a modular structure for tree shaking. - Every module has a
package-build.json
used to create a sub packagepackage.json
file after bundling. - The build script takes the entry point from the
package-build.json
which can be a Vue file or index. - This is currently done to enable the export of multiple formats of the components (js, cjs, sfc (vue)).
- The components are split into folders (e.g.
- import after installing the components pacakge outside our monorepo are possible using
import XerButton from "@xerius/codey-components/button"
. This gives a nice modular feel. - Importing a component inside our monorepo requires an additional entry point as the
package.json
ofcodey-components
does not contain any. This is because at build time every component module gets one of its own.- An additional
index.js
needs to be created for internal use - Every component needs to be re-exported here to use within the repo
- This creates a different import/export mechanism for internal/external use
- An additional
Decision
Because of the mixed import structure I decided to simplify the module structure like what is now done with the codey-composables
:
- Every component still remains in his module folder.
- Every module folder has an
index.js
. - A src
index.js
is created to re-export all components - The
package.json
ofcodey-composables
has the globalindex.js
set asmain
&exports
. This allows the same import structure for internal and external use. - On build time, every module is build separately using the
index.js
. - The src
index.js
is copied to thedist
. - This also allows to import all components in one statement:
import * as CodeyComponents from '@xerius/codey-components'
Consequences
- Only
esm
output is supported with this approach, as anotherindex.csj
would need to be created. As our components are always used in combination withvue3
this won't be a great issue. - Removing the sfc from the dist will not allow SSR to utilize these directly. which can have a performance cost.
Alternatives
- Sticking with the current situation and keep track of 2 separate export & import methods.