Skip to content

Commit a9209ea

Browse files
authored
feat(define-slots): init (#116)
1 parent dcab0e5 commit a9209ea

36 files changed

+508
-8
lines changed

.changeset/popular-shoes-cry.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'@vue-macros/volar': minor
3+
'@vue-macros/common': patch
4+
'@vue-macros/define-slots': patch
5+
'unplugin-vue-macros': patch
6+
---
7+
8+
add defineSlots macro

docs/.vitepress/configs/navs.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ export const sidebar: DefaultTheme.Sidebar = [
3131
text: 'defineModel',
3232
link: '/macros/define-model',
3333
},
34+
{
35+
text: 'defineSlots',
36+
link: '/macros/define-slots',
37+
},
3438
{
3539
text: 'defineRender',
3640
link: '/macros/define-render',

docs/macros/define-model.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ emit('update:count', count + 1)
110110
// tsconfig.json
111111
{
112112
"vueCompilerOptions": {
113-
"target": 3,
113+
"target": 3, // or 2.7 for Vue 2
114114
"plugins": [
115115
"@vue-macros/volar/define-model"
116116
// ...more feature

docs/macros/define-slots.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# defineSlots
2+
3+
Declaring type of SFC slots in `<script setup>` using the `defineSlots`.
4+
5+
| Features | Supported |
6+
| :-----------: | :---------------------------------: |
7+
| Vue 3 | :white_check_mark: |
8+
| Vue 2 | :white_check_mark: |
9+
| Volar + Vue 3 | :white_check_mark: |
10+
| Volar + Vue 2 | :x: (Volar is not supported it yet) |
11+
12+
## Basic Usage
13+
14+
```vue
15+
<script setup lang="ts">
16+
defineSlots<{
17+
// slot name
18+
title: {
19+
// scoped slot
20+
foo: 'bar' | boolean
21+
}
22+
}>()
23+
</script>
24+
```
25+
26+
## Volar Configuration
27+
28+
```jsonc{6}
29+
// tsconfig.json
30+
{
31+
"vueCompilerOptions": {
32+
"target": 3, // or 2.7 is not supported by Volar.
33+
"plugins": [
34+
"@vue-macros/volar/define-slots"
35+
// ...more feature
36+
],
37+
38+
}
39+
}
40+
```

packages/common/src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export const DEFINE_MODEL = 'defineModel'
55
export const DEFINE_MODEL_DOLLAR = '$defineModel'
66
export const DEFINE_SETUP_COMPONENT = 'defineSetupComponent'
77
export const DEFINE_RENDER = 'defineRender'
8+
export const DEFINE_SLOTS = 'defineSlots'
89

910
export const REPO_ISSUE_URL =
1011
'https://github.com/sxzz/unplugin-vue-macros/issues'

packages/common/src/vue.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { compileScript, parse } from '@vue/compiler-sfc'
2+
import type { Statement } from '@babel/types'
23
import type { MagicString } from './magic-string'
34
import type { SFCDescriptor, SFCScriptBlock } from '@vue/compiler-sfc'
45

@@ -10,7 +11,10 @@ export type _SFCScriptBlock = Omit<
1011
export type SFCCompiled = Omit<SFCDescriptor, 'script' | 'scriptSetup'> & {
1112
script?: _SFCScriptBlock | null
1213
scriptSetup?: _SFCScriptBlock | null
13-
scriptCompiled: SFCScriptBlock
14+
scriptCompiled: Omit<SFCScriptBlock, 'scriptAst' | 'scriptSetupAst'> & {
15+
scriptAst?: Statement[]
16+
scriptSetupAst?: Statement[]
17+
}
1418
lang: string | undefined
1519
}
1620

packages/define-slots/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# @vue-macros/define-slots [![npm](https://img.shields.io/npm/v/@vue-macros/define-slots.svg)](https://npmjs.com/package/@vue-macros/define-slots)
2+
3+
Please refer to [README.md](https://github.com/sxzz/unplugin-vue-macros#readme)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { defineSlots as _defineSlots } from './macros'
2+
3+
declare global {
4+
const defineSlots: typeof _defineSlots
5+
}
6+
7+
export {}

packages/define-slots/macros.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export declare const defineSlots: <
2+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
3+
T
4+
>() => void

packages/define-slots/package.json

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
{
2+
"name": "@vue-macros/define-slots",
3+
"version": "0.0.0",
4+
"packageManager": "pnpm@7.13.5",
5+
"description": "",
6+
"keywords": [
7+
"unplugin",
8+
"vue",
9+
"sfc",
10+
"setup",
11+
"macros",
12+
"script-setup",
13+
"define-slots"
14+
],
15+
"license": "MIT",
16+
"homepage": "https://github.com/sxzz/unplugin-vue-macros#readme",
17+
"bugs": {
18+
"url": "https://github.com/sxzz/unplugin-vue-macros/issues"
19+
},
20+
"repository": {
21+
"type": "git",
22+
"url": "git+https://github.com/sxzz/unplugin-vue-macros.git"
23+
},
24+
"author": "三咲智子 <sxzz@sxzz.moe>",
25+
"files": [
26+
"dist",
27+
"*.d.ts"
28+
],
29+
"main": "dist/index.js",
30+
"module": "dist/index.mjs",
31+
"types": "index.d.ts",
32+
"exports": {
33+
".": {
34+
"require": "./dist/index.js",
35+
"import": "./dist/index.mjs"
36+
},
37+
"./vite": {
38+
"require": "./dist/vite.js",
39+
"import": "./dist/vite.mjs"
40+
},
41+
"./webpack": {
42+
"require": "./dist/webpack.js",
43+
"import": "./dist/webpack.mjs"
44+
},
45+
"./rollup": {
46+
"require": "./dist/rollup.js",
47+
"import": "./dist/rollup.mjs"
48+
},
49+
"./esbuild": {
50+
"require": "./dist/esbuild.js",
51+
"import": "./dist/esbuild.mjs"
52+
},
53+
"./macros": "./macros.d.ts",
54+
"./macros-global": "./macros-global.d.ts",
55+
"./*": "./*"
56+
},
57+
"typesVersions": {
58+
"*": {
59+
"*": [
60+
"./dist/*",
61+
"./*"
62+
]
63+
}
64+
},
65+
"scripts": {
66+
"build": "tsup && tsx ../../scripts/postbuild.mts",
67+
"dev": "DEV=1 tsup"
68+
},
69+
"peerDependencies": {
70+
"vue": "^2.7.0 || ^3.0.0"
71+
},
72+
"dependencies": {
73+
"@rollup/pluginutils": "^4.2.1",
74+
"@vue-macros/common": "workspace:~",
75+
"unplugin": "^0.9.6"
76+
},
77+
"devDependencies": {
78+
"@babel/types": "^7.19.4",
79+
"fast-glob": "^3.2.12",
80+
"tsup": "^6.2.3",
81+
"tsx": "^3.10.1",
82+
"vite": "^3.1.8",
83+
"vue": "^3.2.41"
84+
},
85+
"engines": {
86+
"node": ">=14.19.0"
87+
}
88+
}

0 commit comments

Comments
 (0)