Skip to content

feat: publish clay as both CJS and ESM #6110

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

bryceosterhaus
Copy link
Member

@bryceosterhaus bryceosterhaus commented Jul 17, 2025

Now that DXP is ESM, we would benefit from also publishing clay as ESM. I am gonna leave this as a draft and publish a prerelease to test in dxp

Check a prerelease here, https://app.unpkg.com/@clayui/button@3.141.1-alpha.1

I tested button in DXP and saw that with ESM we go from 430 lines of code to 274 for @clayui/button import. So that is promising

properties: ['data-testid'],
},
],
'babel-plugin-fully-specified',
Copy link
Member Author

Choose a reason for hiding this comment

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

This is to update any import like from './language-helpers', which would resolve to ./language-helpers/index.js or ./language-helpers.js.

In ESM, you have to explicitly specify the file and extension, in CJS you don't have to.

Comment on lines -7 to -9
"engines": {
"node": ">=0.12.0",
"npm": ">=3.0.0"
Copy link
Member Author

Choose a reason for hiding this comment

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

No need for this, the numbers are wildly off too

Comment on lines +7 to +11
"main": "lib/cjs/index.js",
"module": "lib/esm/index.js",
"exports": {
"import": "./lib/esm/index.js",
"require": "./lib/cjs/index.js"
Copy link
Member Author

Choose a reason for hiding this comment

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

These are all standards with package.json now and allows for both ESM and CJS consumption

@bryceosterhaus bryceosterhaus marked this pull request as ready for review July 17, 2025 22:31
@bryceosterhaus
Copy link
Member Author

Just to note, these are the size differences(in bytes)

 'clay-alert': { esm: 1193, cjs: 1512 },
  'clay-autocomplete': { esm: 4767, cjs: 5291 },
  'clay-badge': { esm: 427, cjs: 638 },
  'clay-breadcrumb': { esm: 840, cjs: 1324 },
  'clay-button': { esm: 1090, cjs: 1462 },
  'clay-card': { esm: 3227, cjs: 4155 },
  'clay-charts': { esm: 155389, cjs: 156006 },
  'clay-color-picker': { esm: 10072, cjs: 10874 },
  'clay-core': { esm: 34704, cjs: 40042 },
  'clay-data-provider': { esm: 2662, cjs: 2801 },
  'clay-date-picker': { esm: 17676, cjs: 18336 },
  'clay-drop-down': { esm: 6478, cjs: 7857 },
  'clay-empty-state': { esm: 855, cjs: 1272 },
  'clay-form': { esm: 3626, cjs: 4596 },
  'clay-icon': { esm: 419, cjs: 631 },
  'clay-label': { esm: 703, cjs: 933 },
  'clay-layout': { esm: 1289, cjs: 1837 },
  'clay-link': { esm: 843, cjs: 1165 },
  'clay-list': { esm: 1803, cjs: 2391 },
  'clay-loading-indicator': { esm: 451, cjs: 660 },
  'clay-localized-input': { esm: 927, cjs: 1336 },
  'clay-management-toolbar': { esm: 854, cjs: 1314 },
  'clay-modal': { esm: 3894, cjs: 4626 },
  'clay-multi-select': { esm: 3571, cjs: 4135 },
  'clay-multi-step-nav': { esm: 1442, cjs: 2146 },
  'clay-nav': { esm: 427, cjs: 697 },
  'clay-navigation-bar': { esm: 1203, cjs: 1774 },
  'clay-pagination': { esm: 1589, cjs: 2015 },
  'clay-pagination-bar': { esm: 1406, cjs: 2048 },
  'clay-panel': { esm: 1233, cjs: 1648 },
  'clay-popover': { esm: 1442, cjs: 1986 },
  'clay-progress-bar': { esm: 531, cjs: 749 },
  'clay-provider': { esm: 903, cjs: 1552 },
  'clay-shared': { esm: 13781, cjs: 15242 },
  'clay-slider': { esm: 797, cjs: 1007 },
  'clay-sticker': { esm: 469, cjs: 738 },
  'clay-table': { esm: 1051, cjs: 1420 },
  'clay-tabs': { esm: 1653, cjs: 2227 },
  'clay-time-picker': { esm: 1888, cjs: 2356 },
  'clay-toolbar': { esm: 993, cjs: 1479 },
  'clay-tooltip': { esm: 7821, cjs: 8237 },
  'clay-upper-toolbar': { esm: 543, cjs: 756 }

@ethib137
Copy link
Member

Oh nice! That's great to see that esm is smaller.

Also, I noticed that in the deploy proview logs it looks like there's still a problem with at least one export:

11:05:14 PM: ERR! ModuleNotFoundError: Module not found: Error: Package path ./src/useResource is not exported from package

@bryceosterhaus
Copy link
Member Author

@ethib137 got it passing, has some issues with deeply imported items(/src/useResource)

@bryceosterhaus
Copy link
Member Author

New build, 3.141.2-alpha.0, https://app.unpkg.com/@clayui/button@3.141.2-alpha.0

@bryceosterhaus
Copy link
Member Author

bryceosterhaus commented Jul 18, 2025

I tested this in DXP using our build reports, very promising for bundle size by moving to ESM. Note, these are not the gzipped size

File Path Before After Diff (kb)
frontend-taglib-clay/liferay/exports/@clayui$core.js 234136 113046 -121.1
frontend-taglib-clay/liferay/exports/@clayui$date-picker.js 166815 131712 -35.1
frontend-taglib-clay/liferay/exports/@clayui$drop-down.js 52091 23450 -28.6
frontend-taglib-clay/liferay/exports/@clayui$shared.js 50491 26968 -23.5
frontend-taglib-clay/liferay/exports/@clayui$form.js 34067 14470 -19.6
frontend-taglib-clay/liferay/exports/@clayui$charts.js 478509 462052 -16.5
frontend-taglib-clay/liferay/exports/@clayui$modal.js 28055 12307 -15.7
frontend-taglib-clay/liferay/exports/@clayui$color-picker.js 44542 29439 -15.1
frontend-taglib-clay/liferay/exports/@clayui$card.js 28774 14707 -14.1
frontend-taglib-clay/liferay/exports/@clayui$autocomplete.js 23469 12501 -11.0
frontend-taglib-clay/liferay/exports/@clayui$layout.js 17062 7046 -10.0
frontend-taglib-clay/liferay/exports/@clayui$tabs.js 15140 5294 -9.8
frontend-taglib-clay/liferay/exports/@clayui$list.js 18565 9090 -9.5
frontend-taglib-clay/liferay/exports/@clayui$tooltip.js 17971 8646 -9.3
frontend-taglib-clay/liferay/exports/@clayui$multi-select.js 20205 10937 -9.3
frontend-taglib-clay/liferay/exports/@clayui$data-provider.js 15584 6878 -8.7
frontend-taglib-clay/liferay/exports/@clayui$pagination-bar.js 13388 5581 -7.8
frontend-taglib-clay/liferay/exports/@clayui$pagination.js 13314 5919 -7.4
frontend-taglib-clay/liferay/exports/@clayui$multi-step-nav.js 12518 5215 -7.3
frontend-taglib-clay/liferay/exports/@clayui$toolbar.js 11670 5283 -6.4
frontend-taglib-clay/liferay/exports/@clayui$navigation-bar.js 11442 5367 -6.1
frontend-taglib-clay/liferay/exports/@clayui$panel.js 12128 5522 -6.6
frontend-taglib-clay/liferay/exports/@clayui$provider.js 9863 4804 -5.1
frontend-taglib-clay/liferay/exports/@clayui$localized-input.js 8935 4285 -4.7
frontend-taglib-clay/liferay/exports/@clayui$table.js 8615 3969 -4.6
frontend-taglib-clay/liferay/exports/@clayui$popover.js 8298 4019 -4.3
frontend-taglib-clay/liferay/exports/@clayui$time-picker.js 10831 6496 -4.3
frontend-taglib-clay/liferay/exports/@clayui$management-toolbar.js 7827 3675 -4.2
frontend-taglib-clay/liferay/exports/@clayui$alert.js 8043 4469 -3.6
frontend-taglib-clay/liferay/exports/@clayui$breadcrumb.js 7445 3867 -3.6
frontend-taglib-clay/liferay/exports/@clayui$button.js 8031 4597 -3.4
frontend-taglib-clay/liferay/exports/@clayui$empty-state.js 5786 2710 -3.1
frontend-taglib-clay/liferay/exports/@clayui$badge.js 2552 1583 -1.0
frontend-taglib-clay/liferay/exports/@clayui$nav.js 3296 2345 -1.0
frontend-taglib-clay/liferay/exports/@clayui$upper-toolbar.js 3626 2477 -1.1
frontend-taglib-clay/liferay/exports/@clayui$label.js 5390 3155 -2.2
frontend-taglib-clay/liferay/exports/@clayui$sticker.js 3854 1781 -2.1
frontend-taglib-clay/liferay/exports/@clayui$slider.js 5056 2650 -2.4
frontend-taglib-clay/liferay/exports/@clayui$link.js 5077 2613 -2.5
frontend-taglib-clay/liferay/exports/@clayui$progress-bar.js 4446 2859 -1.6
frontend-taglib-clay/liferay/exports/@clayui$loading-indicator.js 3351 1662 -1.7
frontend-taglib-clay/liferay/exports/@clayui$icon.js 3136 2357 -0.8

@bryceosterhaus
Copy link
Member Author

I think this is worth releasing and testing a new official publish

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants