Skip to content

Commit ff3e222

Browse files
committed
update sfc custom element usage
1 parent 8614424 commit ff3e222

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

src/guide/web-components.md

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -165,21 +165,26 @@ The [Provide / Inject API](/guide/component-provide-inject.html#provide-inject)
165165

166166
### SFC as Custom Element
167167

168-
The official build toolchains have built-in support for importing Vue Single File Components (SFCs) as custom elements. By default, files ending in `*.ce.vue` will be processed as native custom elements when imported (created with `defineCustomElement`):
168+
`defineCustomElement` also works with Vue Single File Components (SFCs). However, with the default tooling setup, the `<style>` inside the SFCs will still be extracted and merged into a single CSS file during production build. When using an SFC as a custom element, it is often desirable to inject the `<style>` tags into the custom element's shadow root instead.
169+
170+
The official SFC toolings support importing SFCs in "custom element mode" (requires `@vitejs/plugin-vue@^1.4.0` or `vue-loader@^16.5.0`). An SFC loaded in custom element mode inlines its `<style>` tags as strings of CSS and exposes them under the component's `styles` option. This will be picked up by `defineCustomElement` and injected into the element's shadow root when instantiated.
171+
172+
To opt-in to this mode, simply end your component file name with `.ce.vue`:
169173

170174
```js
175+
import { defineCustomElement } from 'vue'
171176
import Example from './Example.ce.vue'
172177

173-
// register
174-
customElements.define('my-example', Example)
178+
console.log(Example.styles) // ["/* inlined css */"]
175179

176-
// can also be instantiated
177-
const myExample = new Example()
178-
```
180+
// convert into custom element constructor
181+
const ExampleElement = defineCustomElement(Example)
179182

180-
When imported as custom elements, `<style>` inside the SFC will be inlined as JavaScript strings and inserted as a native `<style>` tag inside the custom element's shadow root.
183+
// register
184+
customElements.define('my-example', ExampleElement)
185+
```
181186

182-
If you wish to customize what files should be imported as custom elements (for example treating _all_ SFCs as custom elements), you can pass the `customElement` option to the respective build plugins:
187+
If you wish to customize what files should be imported in custom element mode (for example treating _all_ SFCs as custom elements), you can pass the `customElement` option to the respective build plugins:
183188

184189
- [@vitejs/plugin-vue](https://github.com/vitejs/vite/tree/main/packages/plugin-vue#using-vue-sfcs-as-custom-elements)
185190
- [vue-loader](https://github.com/vuejs/vue-loader/tree/next#v16-only-options)
@@ -193,8 +198,12 @@ If the custom elements will be used in an application that is also using Vue, yo
193198
It is recommended to export the individual element constructors to give your users the flexibility to import them on-demand and register them with desired tag names. You can also export a convenience function to automatically register all elements. Here's an example entry point of a Vue custom element library:
194199

195200
```js
196-
import MyFoo from './MyFoo.ce.vue'
197-
import MyBar from './MyBar.ce.bar'
201+
import { defineCustomElement } from 'vue'
202+
import Foo from './MyFoo.ce.vue'
203+
import Bar from './MyBar.ce.bar'
204+
205+
const MyFoo = defineCustomElement(Foo)
206+
const MyBar = defineCustomElement(Bar)
198207

199208
// export individual elements
200209
export { MyFoo, MyBar }
@@ -205,6 +214,8 @@ export function register() {
205214
}
206215
```
207216

217+
If you have many components, you can also leverage build tool features such as Vite's [glob import](https://vitejs.dev/guide/features.html#glob-import) or webpack's [`require.context`](https://webpack.js.org/guides/dependency-management/#requirecontext) to load all components from a directory.
218+
208219
## Web Components vs. Vue Components
209220

210221
Some developers believe that framework-proprietary component models should be avoided, and that exclusively using Custom Elements makes an application "future-proof". Here we will try to explain why we believe that this is an overly simplistic take on the problem.

0 commit comments

Comments
 (0)