Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/twenty-houses-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@vue-macros/volar": minor
---

introduce setup-sfc

19 changes: 14 additions & 5 deletions docs/macros/setup-sfc.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ If you're using `setupSFC`, then `defineRender` cannot be disabled.
| Vue 3 | :white_check_mark: |
| Nuxt 3 | :white_check_mark: |
| Vue 2 | :white_check_mark: |
| TypeScript / Volar | :x: |
| TypeScript / Volar | :white_check_mark: |

## Setup

Expand Down Expand Up @@ -85,7 +85,16 @@ export default () => (
)
```

## Known Issues

- The source map does not correspond properly in JSX/TSX files.
- TypeScript support is not yet completed.
## Volar Configuration

```jsonc {5}
// tsconfig.json
{
"vueCompilerOptions": {
"plugins": [
"@vue-macros/volar/setup-sfc",
// ...more feature
],
},
}
```
19 changes: 14 additions & 5 deletions docs/zh-CN/macros/setup-sfc.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
| Vue 3 | :white_check_mark: |
| Nuxt 3 | :white_check_mark: |
| Vue 2 | :white_check_mark: |
| TypeScript / Volar | :x: |
| TypeScript / Volar | :white_check_mark: |

## 安装

Expand Down Expand Up @@ -85,7 +85,16 @@ export default () => (
)
```

## 已知的问题

- Source map 在 JSX/TSX 文件中不能正确的映射。
- TypeScript 支持尚未完成。
## Volar 配置

```jsonc {5}
// tsconfig.json
{
"vueCompilerOptions": {
"plugins": [
"@vue-macros/volar/setup-sfc",
// ...更多功能
],
},
}
```
40 changes: 40 additions & 0 deletions packages/volar/src/setup-sfc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { REGEX_SETUP_SFC } from '@vue-macros/common'
import { parse, type VueLanguagePlugin } from '@vue/language-core'
import type { SFCParseResult } from 'vue/compiler-sfc'

const plugin: VueLanguagePlugin = () => {
function isValidFile(fileName: string) {
return REGEX_SETUP_SFC.test(fileName)
}

return {
version: 2.1,
order: -1,
isValidFile,
parseSFC2(fileName, _, content) {
if (!isValidFile(fileName)) return

const lang = fileName.split(/\.[cm]?/).at(-1)
const prefix = `<script setup lang="${lang}">\n`
return patchSFC(parse(`${prefix}${content}\n</script>`), prefix.length)
},
}
}

export default plugin

function patchSFC(sfc: SFCParseResult, offset: number): SFCParseResult {
const {
descriptor: { scriptSetup },
} = sfc

if (scriptSetup) {
scriptSetup.loc.start.column -= offset
scriptSetup.loc.start.offset -= offset
scriptSetup.loc.end.offset -= offset
if (scriptSetup.loc.end.line === scriptSetup.loc.start.line) {
scriptSetup.loc.end.column -= offset
}
}
return sfc
}
15 changes: 15 additions & 0 deletions playground/vue3/src/examples/setup-sfc/comp.setup.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { expectTypeOf } from 'expect-type'

const { foo } = defineProps<{
foo: number
}>()

/**
* ## Vue JSX is the future
*/
export default (
<div>
<div v-if={foo === 1}>{expectTypeOf<1>(foo)}</div>
<div v-else-if={foo === 2}>{expectTypeOf<2>(foo)}</div>
</div>
)
11 changes: 2 additions & 9 deletions playground/vue3/src/examples/setup-sfc/index.setup.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
import { ref } from 'vue'
import Comp from './comp.setup'

const msg = ref('Hello World')

export default () => (
<div>
<input type="text" v-model={msg.value} />
<div>{msg.value}</div>
</div>
)
export default <Comp foo={1} />
7 changes: 6 additions & 1 deletion playground/vue3/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@vue-macros/volar/setup-jsdoc",
"@vue-macros/volar/boolean-prop",
"@vue-macros/volar/template-ref",
"@vue-macros/volar/setup-sfc",
"@vue-macros/volar/script-lang"
],
"experimentalDefinePropProposal": "kevinEdition",
Expand All @@ -54,7 +55,11 @@
"include": ["**/export-props/**"]
},
"exportRender": {
"include": ["**/export-render/**"]
"include": [
"**/export-render/**",
"**/setup-sfc/**",
"**/full.setup.tsx/**"
]
}
}
},
Expand Down