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

defineCustomElement does not support async component #4261

Closed
gnuletik opened this issue Aug 5, 2021 · 2 comments
Closed

defineCustomElement does not support async component #4261

gnuletik opened this issue Aug 5, 2021 · 2 comments
Labels
✨ feature request New feature or request

Comments

@gnuletik
Copy link

gnuletik commented Aug 5, 2021

What problem does this feature solve?

While the new defineCustomElement feature is really nice, it does not supports async components.
This feature is useful when you want to define multiple web components in one file without loading all of them (they will be loaded when the custom elements are used).

The following attempt mount the component but custom elements attributes are not passed to the Vue component.

customElements.define(
  'my-element',
  defineCustomElement(defineAsyncComponent(() => import('MyComponent.vue')))
)

Async components are currently supported by web-component-wrapper for Vue 2.x (https://github.com/vuejs/vue-web-component-wrapper)

const CustomElement = wrap(Vue, () => import(`MyComponent.vue`))
window.customElements.define('my-element', CustomElement)

What does the proposed API look like?

The defineCustomElement function could receive a function returning a Promise to a Vue Component like this :

customElements.define(
  'my-element',
  defineCustomElement(() => import('MyComponent.vue'))
)

When the <my-element></my-element> element is inserted somewhere in the DOM, the MyComponent.vue file should be be downloaded by the browser (if not already) and mounted.

@posva posva added the ✨ feature request New feature or request label Aug 5, 2021
@yyx990803
Copy link
Member

A note: when importing SFCs as custom elements, the tooling needs to process the SFC styles as inlined CSS strings for shadow DOM injection.

This use case makes me realize that the current way SFC toolings handle custom elements (which imports the SFC directly as the custom element constructor) is flawed, since it makes it impossible to define a custom element with defineAsyncComponent + SFC. So I'll have to rework the behavior in @vitejs/plugin-vue and vue-loader.

yyx990803 added a commit to vitejs/vite that referenced this issue Aug 7, 2021
custom element mode no longer exports the element constructor
this change is necessary to allow defining lazy elements
See vuejs/core#4261
yyx990803 added a commit to vuejs/vue-loader that referenced this issue Aug 7, 2021
@gnuletik
Copy link
Author

Awesome ! Thanks for the super fast fix @yyx990803 and congrats for the 3.2 release !

aleclarson pushed a commit to aleclarson/vite that referenced this issue Nov 8, 2021
custom element mode no longer exports the element constructor
this change is necessary to allow defining lazy elements
See vuejs/core#4261
patak-dev pushed a commit to vitejs/vite-plugin-vue that referenced this issue Dec 2, 2022
custom element mode no longer exports the element constructor
this change is necessary to allow defining lazy elements
See vuejs/core#4261
@github-actions github-actions bot locked and limited conversation to collaborators Oct 14, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
✨ feature request New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants