-
-
Notifications
You must be signed in to change notification settings - Fork 108
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
HMR breaks on CustomElements (WebComponents) #270
Comments
Hi, web-components are registered once and you have to reload the browser page to load a newer version, you can't redefine them later, so HMR for the web-components themselves isn't possible. You can however use a feature of vite-plugin-svelte to selectively compile .svelte files a web-components svelte.config.js function isWebComponentSvelte(code) {
const svelteOptionsIdx = code.indexOf('<svelte:options ')
if(svelteOptionsIdx < 0) {
return false
}
const tagOptionIdx = code.indexOf('tag=', svelteOptionsIdx)
const svelteOptionsEndIdx = code.indexOf('>',svelteOptionsIdx);
return tagOptionIdx > svelteOptionsIdx && tagOptionIdx < svelteOptionsEndIdx
}
export default {
vitePlugin: {
experimental: {
dynamicCompileOptions({code}) {
if(isWebComponentSvelte(code)) {
return {
customElement: true
}
}
}
}
}
} -- edited the above code block to vite-plugin-svelte 1.0 config That way, only .svelte files that contain You should also make sure you only import svelte web components once eg |
Thank you so much @dominikg! Very appreciated :) |
@dominikg I'm trying to do something similar in svelte 4. Made the change from
Any idea how to sort these out? I can open a different issue if that's more appropriate. |
please use https://svelte.dev/chat help channels to ask these questions. for bugs new issues with minimal reproduction. |
Describe the bug
I am using the "CustomElement" feature of svelte. (My backend is a legacy asp.net MVC app)
I have setup a CustomElement called "rb-about" and the page renders the component/element correctly.
Im connecting to HMR and proxying the Backend (see vite.config below).
In the page i have included the following scripts as per vite instructions: https://vitejs.dev/guide/backend-integration.html#backend-integration and I can see in the console vite has "connected":
[vite] connecting... client.ts:22:8
[vite] connected.
I then make a change in the svelte component and save.
Expected Result
HMR updates the custom component in the browser
Actual Result
The custom component doesn't update and the console logs the following error:
DOMException: CustomElementRegistry.define: 'rb-about' has already been defined as a custom element
Reproduction
I just dont know how to spin up a repo with a disconnected/seperate backend (as my current backend is an existing asp.net mvc app).
Logs
System Info
Severity
annoyance
The text was updated successfully, but these errors were encountered: