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

chore: optimize bundle size #88

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

halvaradop
Copy link
Owner

@halvaradop halvaradop commented Jan 24, 2025

Description

This pull request optimizes the bundle size of the packages significantly.

Originally, the sizes of the components were around 120KB and 124KB. By updating the tsconfig to externalize react and react-dom dependencies and enabling the treeshaking option in the config, the size of the packages was reduced to around 25KB-28KB, and the core package from 166KB to 57KB—a significant reduction of about 75%. Additionally, I added a new script in the package.json files of the packages to remove unnecessary .d.cts files when the packages are built.

.d.cts Files

These files were generated because the tsupConfig declared the CJS format, causing the .d.cts files to be created. However, these files are not required since the original format of the packages/components is ESM modules, which generate the .d.ts files supporting both ESM and CommonJS modules. Therefore, the creation of these files is unnecessary unless the user project is using CommonJS modules.

The current exports field of the package.json looks like this:

"exports": {
    ".": {
        "types": "./dist/index.d.ts",
        "import": "./dist/index.js",
        "require": "./dist/index.cjs"
    }
}

Bash Scripts

Compare Sizes

I've used the following bash script to compare the sizes of the components:

#!/bin/bash

path="packages"

for dir in "$path"/*/; do
    echo "Current directory: $dir"
    cd "$dir" || exit
    pnpm pack
    tar -xf *.tgz
    previousSize=$(du -sh package | cut -f1)
    echo "Previous size: $previousSize"
    rm -rf package *.tgz
    cd - || exit
done

Clean

I've used the following bash script to remove the build folder and unnecessary files:

#!/bin/bash

path="packages"

for dir in "$path"/*/; do
    echo "Current directory: $dir"
    cd "$dir" || exit
    rm -rf package *.tgz .turbo dist
    cd ../.. || exit
done

Table of Sizes

Package Current After Externalizing After Removing .cts files
button 120K 28K 24K
checkbox 120K 24K 20K
core 166K 57K 42K
dialog 124K 28K 24K
form 124K 28K 24K
input 124K 28K 24K
label 124K 25K 21K
radio 124K 28K 24K
radio-group 124K 28K 24K
submit 124K 28K 24K
template 12K 12K 12K

Checklist

  • Added documentation.
  • The changes do not generate any warnings.
  • I have performed a self-review of my own code
  • All tests have been added and pass successfully

Notes

- Add `react` and `react-dom` as peer dependencies
- Include `react` and `react-dom` in external dependencies in `tsupConfig`
- Add `clean:cts` script in `package.json`
@halvaradop halvaradop marked this pull request as draft March 19, 2025 16:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In review
Development

Successfully merging this pull request may close these issues.

1 participant