-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Using customElements.define() is not possible in v4 #8681
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
Comments
Assuming you compile with custom element mode, then |
I just tested that @dummdidumm and unfortunately that doesn't seem to be a complete workaround since HMR doesn't seem to work with that at all. Is there something else I need to do to facilitate HMR in Vite? 🤔 // HMR not working
import ExampleElement from './lib/ExampleElement.svelte';
customElements.define('example-element', ExampleElement.element); vs. // HMR works
import ExampleElement from './lib/ExampleElement.svelte';
import { create_custom_element } from 'svelte/internal';
customElements.define('example-element',
create_custom_element(
ExampleElement,
{},
[],
[],
false,
)
); |
Setup a repro of the HMR issue in the git clone https://github.com/patricknelson/svelte-v4-custom-elements-define.git
cd svelte-v4-custom-elements-define
npm i
git checkout hmr-bug
npm run dev |
oh, this is nice "side effect" of the changes to custom elements. hmr for these didn't work at all in svelte-3 you would have to ensure that Not sure if svelte-hmr could do something special to improve this even more. cc @rixo |
Yeah, would definitely love to get HMR along with custom elements. My primary use case is in a fairly large and old codebase that cannot be converted to a Svelte (or SvelteKit) app. That said, svelte-tag works flawlessly with combining both custom elements and HMR. It has some other issues though (mainly with slots and handling attributes with capital letters) so I figured it might be best to work toward improving Svelte itself as a longer term solution. |
Putting this (i.e. HMR compatible declaration of Svelte v4 custom elements) on my roadmap to support in v2 of my new package // Totally hypothetical...
svelteRetag({
tag: 'example-element',
shadow: 'none',
props: {
greetPerson: { reflect: true, attribute: 'greet-person' },
},
}); All this is assuming of course HMR support isn't added by the time Svelte v4 comes out (since ideally
Apologies for this comment getting slightly out of hand, but I'm so excited for Svelte 4's upcoming Custom Elements that I want to do it now but also not lose functionality if I can help it (particularly simple bug-free HMR)! 😅 |
It appears this is now documented at https://svelte.dev/docs/custom-elements-api: import MyElement from './MyElement.svelte';
customElements.define('my-element', MyElement.element);
// In Svelte 3, do this instead:
// customElements.define('my-element', MyElement); I'll close this out and instead reopen a new ticket to address the HMR support as a separate feature request. @dominikg: Should that ticket go here or into |
svelte-hmr. |
Describe the bug
For v4, improvements were made in how custom elements were compiled in #8457:
So, since components are no longer compiled directly to an instance of
HTMLElement
, the traditional v3 approach of manually defining your custom element will no longer work:Unfortunately, a public API doesn't exist for handling this. To workaround it right now, you need to reach into Svelte's internal API:
Possible Solutions
In v4, we still need generate a class descending from
HTMLElement
. Maybe we could get a new wrapper function that would either:<svelte:options customElement={...} />
when called<svelte:options customElement={...} />
in the component itself for us (closer parity to v3).<svelte:options customElement={...} />
. Explicitly passed options to this wrapper would probably take precedence over the ones inferred from the component file.The goal of this would be to simplify documentation and refactoring as much as possible. That should hopefully make it far easier to reason about when reading docs or when needing to refactor/separate the options out when manually calling
customElements.define()
.For example:
main.js
ExampleElement.svelte
Reproduction
Code: https://github.com/patricknelson/svelte-v4-custom-elements-define
Init repo
git clone https://github.com/patricknelson/svelte-v4-custom-elements-define.git cd svelte-v4-custom-elements-define npm i
Reproduce bug
Test workaround
Logs
No response
System Info
Severity
blocking an upgrade
The text was updated successfully, but these errors were encountered: