Skip to content

Commit cae43f0

Browse files
committed
feat: add defineSlots macro
1 parent ba9c2ae commit cae43f0

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

Diff for: packages/compiler-sfc/src/compileScript.ts

+26-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ const DEFINE_EMITS = 'defineEmits'
6868
const DEFINE_EXPOSE = 'defineExpose'
6969
const WITH_DEFAULTS = 'withDefaults'
7070
const DEFINE_OPTIONS = 'defineOptions'
71+
const DEFINT_SLOTS = 'defineSlots'
7172

7273
// constants
7374
const DEFAULT_VAR = `__default__`
@@ -299,6 +300,7 @@ export function compileScript(
299300
let hasDefaultExportName = false
300301
let hasDefaultExportRender = false
301302
let hasDefineOptionsCall = false
303+
let hasDefineSlotsCall = false
302304
let propsRuntimeDecl: Node | undefined
303305
let propsRuntimeDefaults: ObjectExpression | undefined
304306
let propsDestructureDecl: Node | undefined
@@ -593,6 +595,26 @@ export function compileScript(
593595
return true
594596
}
595597

598+
function processDefineSlots(node: Node, declId?: LVal): boolean {
599+
if (!isCallOf(node, DEFINT_SLOTS)) {
600+
return false
601+
}
602+
if (hasDefineSlotsCall) {
603+
error(`duplicate ${DEFINT_SLOTS}() call`, node)
604+
}
605+
hasDefineSlotsCall = true
606+
607+
if (declId) {
608+
s.overwrite(
609+
startOffset + node.start!,
610+
startOffset + node.end!,
611+
`${helper('useSlots')}()`
612+
)
613+
}
614+
615+
return true
616+
}
617+
596618
function getAstBody(): Statement[] {
597619
return scriptAst
598620
? [...scriptSetupAst.body, ...scriptAst.body]
@@ -1288,7 +1310,8 @@ export function compileScript(
12881310
processDefineProps(expr) ||
12891311
processDefineEmits(expr) ||
12901312
processDefineOptions(expr) ||
1291-
processWithDefaults(expr)
1313+
processWithDefaults(expr) ||
1314+
processDefineSlots(expr)
12921315
) {
12931316
s.remove(node.start! + startOffset, node.end! + startOffset)
12941317
} else if (processDefineExpose(expr)) {
@@ -1323,6 +1346,8 @@ export function compileScript(
13231346
processDefineProps(init, decl.id) ||
13241347
processWithDefaults(init, decl.id, node.kind)
13251348
const isDefineEmits = processDefineEmits(init, decl.id)
1349+
processDefineSlots(init, decl.id)
1350+
13261351
if (isDefineProps || isDefineEmits) {
13271352
if (left === 1) {
13281353
s.remove(node.start! + startOffset, node.end! + startOffset)

Diff for: packages/runtime-core/src/apiSetupHelpers.ts

+12
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
ExtractPropTypes
2020
} from './componentProps'
2121
import { warn } from './warning'
22+
import { VNode } from './vnode'
2223

2324
// dev only
2425
const warnRuntimeUsage = (method: string) =>
@@ -176,6 +177,17 @@ export function defineOptions<
176177
}
177178
}
178179

180+
export function defineSlots<
181+
T extends Record<string, any>
182+
>(): // @ts-expect-error
183+
{
184+
[K in keyof T]: (scope: T[K]) => VNode[] | undefined
185+
} {
186+
if (__DEV__) {
187+
warnRuntimeUsage(`defineSlots`)
188+
}
189+
}
190+
179191
type NotUndefined<T> = T extends undefined ? never : T
180192

181193
type InferDefaults<T> = {

Diff for: packages/runtime-core/types/scriptSetupHelpers.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ type _defineProps = typeof defineProps
44
type _defineEmits = typeof defineEmits
55
type _defineExpose = typeof defineExpose
66
type _defineOptions = typeof defineOptions
7+
type _defineSlots = typeof defineSlots
78
type _withDefaults = typeof withDefaults
89

910
declare global {
1011
const defineProps: _defineProps
1112
const defineEmits: _defineEmits
1213
const defineExpose: _defineExpose
1314
const defineOptions: _defineOptions
15+
const defineSlots: _defineSlots
1416
const withDefaults: _withDefaults
1517
}

0 commit comments

Comments
 (0)