-
Notifications
You must be signed in to change notification settings - Fork 119
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
fix(angular, react, vue): use defineCustomElement import to improve treeshaking #208
Conversation
const decorator = function (cls: any) { | ||
const { tagName, customElement, inputs, methods } = opts; | ||
|
||
defineCustomElement(tagName, customElement); | ||
if (defineCustomElementFn !== undefined) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is another const function called defineCustomElements
defined above, so that's why I called this defineCustomElementFn
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good and makes sense. Thanks for the thorough write-up. 🚀
resolves #199
When using
autoDefineCustomElements
, Stencil automatically generates adefineCustomElement()
call in the body of a component file. This is a side effect, which causes bundlers to pull in the entire file when building. When importing components from a single entry point (I.e.import { IonButton } from '@ionic/vue';
), this causes bundlers to bundle every component with your application, regardless of whether or not the user is actually using the component in their application.For example, the entry point of @ionic/vue looks something like this:
index.esm.js
and the file for each component looks something like this (IonAvatar used as example):
When bundlers look at
index.esm.js
, they need to evaluate bothion-avatar.js
andion-badge.js
. CallingdefineCustomElement$1();
in each of the component files creates a side effect, resulting in bothion-avatar
andion-badge
being added to an application bundle, even ifion-avatar
is the only component being used.The wrappers have a secondary defect where if you turn off
autoDefineCustomElements
, child components are never defined because the wrappers only define the parent component.The wrappers found in this repo do the following to define the components:
This PR seeks to solve both the wrapper defect and the treeshaking issue by having the wrappers import
defineCustomElement
for each component and call it when the parent component is used.The one thing to note is that this PR would require the minimum Stencil version to be 2.10.0 as I believe that is the version where the individual
defineCustomElement
functions were generated.Note: This PR does not resolve parts of #198 which describes a potential side effect created by
proxyCustomElement
. My initial investigation found that this only impacts Webpack, so Rollup/ESBuild/others should not be impacted by #198.