Skip to content

Commit 083c9c8

Browse files
committed
refactor: adjust custom element mode behavior
custom element mode no longer exports the element constructor this change is necessary to allow defining lazy elements See vuejs/core#4261
1 parent 78e5474 commit 083c9c8

File tree

2 files changed

+13
-18
lines changed

2 files changed

+13
-18
lines changed

packages/plugin-vue/README.md

+11-7
Original file line numberDiff line numberDiff line change
@@ -82,23 +82,27 @@ export default {
8282

8383
## Using Vue SFCs as Custom Elements
8484

85-
> Requires `vue@^3.2.0`
85+
> Requires `vue@^3.2.0` & `@vitejs/plugin-vue@^1.4.0`
8686
87-
By default, files ending in `*.ce.vue` will be processed as native Custom Elements when imported (created with `defineCustomElement` from Vue core):
87+
Vue 3.2 introduces the `defineCustomElement` method, which works with SFCs. By default, `<style>` tags inside SFCs are extracted and merged into CSS files during build. However when shipping a library of custom elements, it may be desirable to inline the styles as JavaScript strings and inject them into the custom elements' shadow root instead.
88+
89+
Starting in 1.4.0, files ending with `*.ce.vue` will be compiled in "custom elements" mode: its `<style>` tags are compiled into inlined CSS strings and attached to the component as its `styles` property:
8890

8991
```js
92+
import { defineCustomElement } from 'vue'
9093
import Example from './Example.ce.vue'
9194

92-
// register
93-
customElements.define('my-example', Example)
95+
console.log(Example.styles) // ['/* css content */']
9496

95-
// can also be instantiated
96-
const myExample = new Example()
97+
// register
98+
customElements.define('my-example', defineCustomElement(Example))
9799
```
98100

101+
Note in custom elements mode there is no need to use `<style scoped>` since the CSS is already scoped inside the shadow DOM.
102+
99103
The `customElement` plugin option can be used to configure the behavior:
100104

101-
- `{ customElement: true }` will import all `*.vue` files as Custom Elements.
105+
- `{ customElement: true }` will import all `*.vue` files in custom element mode.
102106
- Use a string or regex pattern to change how files should be loaded as Custom Elements (this check is applied after `include` and `exclude` matches).
103107

104108
## License

packages/plugin-vue/src/main.ts

+2-11
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,7 @@ export async function transformMain(
136136
output.push(`export const _rerender_only = true`)
137137
}
138138
output.push(
139-
`import.meta.hot.accept(({ default: ${
140-
asCustomElement ? `{ def: updated }` : `updated`
141-
}, _rerender_only }) => {`,
139+
`import.meta.hot.accept(({ default: updated, _rerender_only }) => {`,
142140
` if (_rerender_only) {`,
143141
` __VUE_HMR_RUNTIME__.rerender(updated.__hmrId, updated.render)`,
144142
` } else {`,
@@ -191,14 +189,7 @@ export async function transformMain(
191189
resolvedMap.sourcesContent = templateMap.sourcesContent
192190
}
193191

194-
if (asCustomElement) {
195-
output.push(
196-
`import { defineCustomElement as __ce } from 'vue'`,
197-
`export default __ce(_sfc_main)`
198-
)
199-
} else {
200-
output.push(`export default _sfc_main`)
201-
}
192+
output.push(`export default _sfc_main`)
202193

203194
return {
204195
code: output.join('\n'),

0 commit comments

Comments
 (0)