Skip to content

Reduce JavaScript size by importing custom element definitions directly#6357

Merged
aduth merged 1 commit intomainfrom
aduth-custom-elements-exports
May 16, 2022
Merged

Reduce JavaScript size by importing custom element definitions directly#6357
aduth merged 1 commit intomainfrom
aduth-custom-elements-exports

Conversation

@aduth
Copy link
Contributor

@aduth aduth commented May 16, 2022

Why: So that React is not included in bundles which are expecting to use only the native custom element implementation.

There isn't expected to be a net reduction in bundle size (i.e. no celebration warranted), but instead resolves a regression in the form of inflated bundle sizes, caused by the introduction of a pattern of workspace packages which export both custom element and React component implementations of a UI component.

Ideally, this could also be addressed using Webpack tree shaking features. In particular, the custom element definition is expected to be the only side-effecty file of the package. Thus, it would be expected that defining something like "sideEffects": ["./print-button-element.ts"] in package.json would suffice to exclude React from the default import, but this does not work in practice. I'm still unclear how to configure it correctly (expecting built file names? full relative file paths? original import string without file extension?). In the meantime, the proposed changes should suffice as an alternative.

Performance Impact:

Measured against http://localhost:3000 as the full transferred size of JavaScript after running NODE_ENV=production yarn build && rails s. Notably, there is no compression reflected in these sizes.

Before After Diff%
182kB 41.2kB -77.4%

**Why**: So that React is not included in bundles which are expecting to use only the native custom element implementation.

changelog: Improvements, Performance, Reduce download size of compiled JavaScript
Copy link
Contributor

@mitchellhenke mitchellhenke left a comment

Choose a reason for hiding this comment

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

👍🏼

Copy link
Contributor

@zachmargolis zachmargolis left a comment

Choose a reason for hiding this comment

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

LGTM

@aduth aduth merged commit 37e8d8b into main May 16, 2022
@aduth aduth deleted the aduth-custom-elements-exports branch May 16, 2022 18:37
@aduth aduth mentioned this pull request May 17, 2022
zachmargolis pushed a commit that referenced this pull request May 17, 2022
**Why**: So that React is not included in bundles which are expecting to use only the native custom element implementation.

changelog: Improvements, Performance, Reduce download size of compiled JavaScript
(cherry picked from commit 37e8d8b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants