SFC Custom blocks #10577
Unanswered
InvisibleGit
asked this question in
Help/Questions
SFC Custom blocks
#10577
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi all,
SFC Custom blocks feature is probably on the more advanced scale of using vue. For example if we want to customize SFC for our particular need we might want to add additional blocks to make DX nicer.
Yet the documentation and examples found online are years old and don't seem to be of much use. For example the docs in the guide mention 3 projects which use custom blocks, where 2 are abandoned (Gridsome and vite-plugin-vue-gql), and the third one (i18n) was rewritten in unimport.js.
Even if we persue i18n as example of how it's implemented we hit a wall...
First inside Vue Guide -> Tooling -> SFC Custom Block Integrations (https://vuejs.org/guide/scaling-up/tooling.html#sfc-custom-block-integrations) we have a hint of how to awkwardly sets i18n data on a component (https://github.com/vitejs/vite-plugin-vue/tree/main/packages/plugin-vue#example-for-transforming-custom-blocks) and use it outside of the SFC component with
Demo.i18n.*
. We're left clueless how to use custom block inside the same SFC file where it's defined...At least we're pushed in the right direction to get to
vite-plugin-vue
repo. We findCustomBlock.vue
andCustomBlockPlugin.vue
undervite-plugin-vue/playground/vue/
. AgainCustomBlockPlugin.ts
hosits the data toComp.i18n
and thenCustomBlock.ts
definesuseI18n
method which uses contraversialgetCurrentInstance()
to get the data and use it inside the component.The crux of the problem is how
vite-plugin-vue
compiles the custom blocks in the functiongenCustomBlockCode()
invitejs/vite-plugin-vue/packages/plugin-vue/src/main.ts
. It runs the custom blockcode
with_sfc_main
as a parameter. This is how above mentioned setting of data on a components is doable. But this is well after the data portion of the component, meaning we can't simply use the data in our custom blocks as an exported variable or an object.Granted
vite-plugin-vue-gql
andi18n
do exactly that, but how they do it is much more complex then what is given as a clue insidevite-plugin-vue
's playground.So... is there an example how to do this or something similar? Can we make contents of the custom blocks available under <script setup> block somehow? Or as part of the component data?
The use case for example would be to create meta data for a blog, and inside the same SFC update title of the page with it. Or to extract this meta data for generation of a RSS/Atom feed.
Thank you for your time...
Beta Was this translation helpful? Give feedback.
All reactions