File tree Expand file tree Collapse file tree 3 files changed +42
-0
lines changed
playground/components/content Expand file tree Collapse file tree 3 files changed +42
-0
lines changed Original file line number Diff line number Diff line change 1+ <template >
2+ <div >
3+ Dummy component to test useSlots
4+ </div >
5+ </template >
6+
7+ <script setup lang="ts">
8+ const slots = useSlots ()
9+
10+ const defaultSlot = computed (() => slots .default ?.())
11+
12+ </script >
Original file line number Diff line number Diff line change 1+ <template >
2+ <div >
3+ Dummy component to test useSlots
4+ </div >
5+ </template >
6+
7+ <script setup lang="ts">
8+ const { title, default : dd } = useSlots ()
9+ </script >
Original file line number Diff line number Diff line change @@ -52,6 +52,27 @@ export default defineNuxtModule<ModuleOptions>({
5252 return `<slot ${ slotName === 'default' ? '' : `name="${ slotName } "` } />`
5353 }
5454 )
55+ // Handle `(const|let|var) slots = useSlots()`
56+ const name = code . match ( / ( c o n s t | l e t | v a r ) ( [ a - z A - Z ] [ a - z A - Z - _ 0 - 9 ] * ) = u s e S l o t s \( \) / ) ?. [ 2 ] || '$slots'
57+ const _slots = code . match ( new RegExp ( `${ name } \\.[a-zA-Z]+` , 'gm' ) )
58+ if ( _slots ) {
59+ const slots = _slots
60+ . map ( s => s . replace ( name + '.' , '' ) )
61+ . map ( s => `<slot name="${ s } " />` )
62+ code = code . replace ( / < t e m p l a t e > / , `<template>\n${ slots . join ( '\n' ) } \n` )
63+ }
64+
65+ // Handle `(const|let|var) { title, default: defaultSlot } = useSlots()`
66+ const slotNames = code . match ( / ( c o n s t | l e t | v a r ) { ( [ ^ } ] + ) } \s * = \s * u s e S l o t s \( \) / ) ?. [ 2 ]
67+ // \s*(([a-zA-Z][a-zA-Z-_0-9]*(\s*:\s*[a-zA-Z][a-zA-Z-_0-9]*)?\s*,?\s*)+)\s*
68+ if ( slotNames ) {
69+ const slots = slotNames
70+ . trim ( )
71+ . split ( ',' )
72+ . map ( s => s . trim ( ) . split ( ':' ) [ 0 ] . trim ( ) )
73+ . map ( s => `<slot name="${ s } " />` )
74+ code = code . replace ( / < t e m p l a t e > / , `<template>\n${ slots . join ( '\n' ) } \n` )
75+ }
5576
5677 return { component, code }
5778 }
You can’t perform that action at this time.
0 commit comments