You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am currently working on a PR for a nuxt module called server-block-nuxt by @Hebilicious which will get multiple server blocks inside of the SFC to work.
<server lang="ts">
importdbfrom"db";exportconst GET =defineEventHandler(() => {// ^? Cannot redeclare block-scoped variable 'GET'. ts(2451)returndb.query("hello");});
</server>
<server lang="ts">
exportconst GET =defineEventHandler(() => {// ^? Cannot redeclare block-scoped variable 'GET'. ts(2451)return { message: "world" };});
</server>
<script lang="ts" setup>
const { data } =awaituseFetch("/api/about");const GET ="GET";// ^? Cannot redeclare block-scoped variable 'GET'. ts(2451)db.query("hello")//^? does exist as a type but at runtime it isn't here
</script>
<template>
<div>
<p>{{ GET }}</p>
</div>
</template>
But if you look at the above example I am faced with a typescript error and the error is not just limited to custom blocks because it appears just the same in script setup.
Another problem is if you look at the top server block we import db from "db" but this import is also made available inside of the script setup only as a type as the server blocks are removed by a vite plugins transform option during run/build-time
There is already a VueLanguagePlugin required to get auto-imports and types to work inside the custom blocks if the desired functionality can be achieved through a plugin it is also a possibility but I couldn't find any documentation on plugin authoring.
This is the current plugin.
importtype{VueLanguagePlugin}from"@vue/language-core"constplugin: VueLanguagePlugin=()=>{return{name: "sfc-server-volar",version: 1,resolveEmbeddedFile(fileName,sfc,embeddedFile){if(embeddedFile.fileName.replace(fileName,"").match(/^\.(js|ts|jsx|tsx)$/)){for(constblockofsfc.customBlocks){constcontent=embeddedFile.contentif(block.type==="server"){content.push([block.content,// text to addblock.name,// source0,// content offset in source{// language capabilities to enable in this segmenthover: true,references: true,definition: true,diagnostic: true,rename: true,completion: true,semanticTokens: true}])}}}}}}exportdefaultplugin
My current plan is to manipulate the block.content inside of the plugin before pushing it to embededFile.content like so:
importdbfrom"db";// 2. hoist the imports out of the block{// 1. put the declarations inside of a block to isolate themconstGET=defineEventHandler(()=>{// ^? 3. delete export stringreturndb.query("hello");});constPOST=defineEventHandler(()=>{// ^? 3. delete export stringreturndb.query("goodbye");});}
This wouldn't however solve the issue of imports of one block being also avalible inside of the others but just the "Cannot redeclare block-scoped variable 'GET'. ts(2451)". Which I am actually fine with since there is nothing sensitive being leaked, we only lose the type safety to some degree.
Conclusion:
If the above proposed solution is an acceptable one I will go through with it for the time being.
And I would like to request an official way of enabling/disabling this kind of behaviour inside of the plugin api to address the problems that could not be solved by simply manipulating the content. If there is one and I am not aware of it I would like to learn about it.
The text was updated successfully, but these errors were encountered:
UfukUstali
changed the title
Is it possible to isolate custom blocks?
Is it possible to isolate custom blocks from the rest of the SFC?
Nov 30, 2023
I am currently working on a PR for a nuxt module called server-block-nuxt by @Hebilicious which will get multiple server blocks inside of the SFC to work.
But if you look at the above example I am faced with a typescript error and the error is not just limited to custom blocks because it appears just the same in script setup.
Another problem is if you look at the top server block we import db from "db" but this import is also made available inside of the script setup only as a type as the server blocks are removed by a vite plugins transform option during run/build-time
There is already a
VueLanguagePlugin
required to get auto-imports and types to work inside the custom blocks if the desired functionality can be achieved through a plugin it is also a possibility but I couldn't find any documentation on plugin authoring.This is the current plugin.
My current plan is to manipulate the
block.content
inside of the plugin before pushing it toembededFile.content
like so:Given:
Modified:
This wouldn't however solve the issue of imports of one block being also avalible inside of the others but just the "Cannot redeclare block-scoped variable 'GET'. ts(2451)". Which I am actually fine with since there is nothing sensitive being leaked, we only lose the type safety to some degree.
Conclusion:
If the above proposed solution is an acceptable one I will go through with it for the time being.
And I would like to request an official way of enabling/disabling this kind of behaviour inside of the plugin api to address the problems that could not be solved by simply manipulating the content. If there is one and I am not aware of it I would like to learn about it.
The text was updated successfully, but these errors were encountered: