Skip to content
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

Cannot use custom element correctly with sveltekit #4502

Closed
winston0410 opened this issue Apr 3, 2022 · 5 comments
Closed

Cannot use custom element correctly with sveltekit #4502

winston0410 opened this issue Apr 3, 2022 · 5 comments
Labels
bug Something isn't working
Milestone

Comments

@winston0410
Copy link

Describe the bug

I got this error, when I set the following compiler options:

const config = {
	compilerOptions: {
		customElement: true
	},
	preprocess: preprocess(),

	kit: {
		adapter: adapter()
	}
};

The received error:

.svelte-kit/generated/root.svelte:1:0 No custom element 'tag' option was specified. To automatically register a custom element, specify a name with a hyphen in it, e.g. <svelte:options tag="my-thing"/>. To hide this warning, use <svelte:options tag={null}/>

.svelte-kit/runtime/components/layout.svelte:1:0 No custom element 'tag' option was specified. To automatically register a custom element, specify a name with a hyphen in it, e.g. <svelte:options tag="my-thing"/>. To hide this warning, use <svelte:options tag={null}/>

Reproduction

git clone https://github.com/winston0410/sveltekit-custom-element
cd ./sveltekit-custom-element
npm i
npm run dev

Logs

No response

System Info

System:
    OS: macOS 11.4
    CPU: (8) arm64 Apple M1
    Memory: 106.36 MB / 8.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 17.8.0 - /opt/homebrew/bin/node
    Yarn: 1.22.17 - /opt/homebrew/bin/yarn
    npm: 8.5.5 - /opt/homebrew/bin/npm
  Browsers:
    Brave Browser: 99.1.36.119
    Safari: 14.1.1
  npmPackages:
    @sveltejs/adapter-auto: next => 1.0.0-next.34 
    @sveltejs/kit: next => 1.0.0-next.304 
    svelte: ^3.44.0 => 3.46.6

Severity

blocking all usage of SvelteKit

Additional Information

No response

@Rich-Harris Rich-Harris added the bug Something isn't working label Apr 4, 2022
@Rich-Harris Rich-Harris added this to the post-1.0 milestone Apr 4, 2022
@cc-ww
Copy link

cc-ww commented Oct 13, 2022

Is there another way to aviod this to build a library?

@dominikg
Copy link
Member

I don't think we can support defining pages as webcomponents (see original reproduction adding it to src/routes/index.svelte which is the equivalent of +page.svelte today)

But it is possible to compile some .svelte files as web-components with vite-plugin-svelte's dynamicCompileOptions, see this comment sveltejs/vite-plugin-svelte#270 (comment)

You'd still have to solve registering them once if you want to use them in your own sveltekit application and HMR won't work.
Packaging them as an npm package for consumption in other apps should work too.

@cc-ww
Copy link

cc-ww commented Oct 14, 2022

thank you , i am sorry to occupy up your time! I am fresh to svelte. Can this support to build a library to be used in angular ,vue or react. hope to have a demo about this :)

@cc-ww
Copy link

cc-ww commented Oct 14, 2022

@dominikg thank you for the demo. It works for me.

dummdidumm added a commit to sveltejs/svelte that referenced this issue May 2, 2023
This is an overhaul of custom elements in Svelte. Instead of compiling to a custom element class, the Svelte component class is mostly preserved as-is. Instead a wrapper is introduced which wraps a Svelte component constructor and returns a HTML element constructor. This has a couple of advantages:

- component can be used both as a custom element as well as a regular component. This allows creating one wrapper custom element and using regular Svelte components inside. Fixes #3594, fixes #3128, fixes #4274, fixes #5486, fixes #3422, fixes #2969, helps with sveltejs/kit#4502
- all components are compiled with injected styles (inlined through Javascript), fixes #4274
- the wrapper instantiates the component in `connectedCallback` and disconnects it in `disconnectedCallback` (but only after one tick, because this could be a element move). Mount/destroy works as expected inside, fixes #5989, fixes #8191
- the wrapper forwards `addEventListener` calls to `component.$on`, which allows to listen to custom events, fixes #3119, closes #4142 
- some things are hard to auto-configure, like attribute hyphen preferences or whether or not setting a property should reflect back to the attribute. This is why `<svelte:options customElement={..}>` can also take an object to modify such aspects. This option allows to specify whether setting a prop should be reflected back to the attribute (default `false`), what to use when converting the property to the attribute value and vice versa (through `type`, default `String`, or when `export let prop = false` then `Boolean`), and what the corresponding attribute for the property is (`attribute`, default lowercased prop name). These options are heavily inspired by lit: https://lit.dev/docs/components/properties. Closes #7638, fixes #5705
- adds a `shadowdom` option to control whether or not encapsulate the custom element. Closes #4330, closes #1748 

Breaking changes:
- Wrapped Svelte component now stays as a regular Svelte component (invokeing it like before with `new Component({ target: ..})` won't create a custom element). Its custom element constructor is now a static property named `element` on the class (`Component.element`) and should be regularly invoked through setting it in the html.
- The timing of mount/destroy/update is different. Mount/destroy/updating a prop all happen after a tick, so `shadowRoot.innerHTML` won't immediately reflect the change (Lit does this too). If you rely on it, you need to await a promise
@eltigerchino
Copy link
Member

Closing this as it seems to work fine with the latest Svelte and SvelteKit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants