Skip to content

Commit fb10489

Browse files
committed
feat(macros): support :slotted for defineStyle
1 parent 0afc03f commit fb10489

File tree

5 files changed

+69
-14
lines changed

5 files changed

+69
-14
lines changed

packages/macros/src/core/define-component/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { importHelperFn, type MagicStringAST } from '@vue-macros/common'
22
import { walkIdentifiers } from '@vue/compiler-sfc'
3-
import { isFunctionalNode, type FunctionalNode, type RootMapValue } from '..'
3+
import { isFunctionalNode, type FunctionalNode, type Macros } from '..'
44
import { getDefaultValue, restructure } from '../restructure'
55
import { transformAwait } from './await'
66
import { transformReturn } from './return'
@@ -9,7 +9,7 @@ import type { Node, ObjectExpression } from '@babel/types'
99
export function transformDefineComponent(
1010
root: FunctionalNode,
1111
propsName: string,
12-
macros: RootMapValue,
12+
macros: Macros,
1313
s: MagicStringAST,
1414
autoReturnFunction = false,
1515
): void {
@@ -143,7 +143,7 @@ function getWalkedIds(root: FunctionalNode, propsName: string) {
143143

144144
function transformDefineModel(
145145
s: MagicStringAST,
146-
defineModel: RootMapValue['defineModel'],
146+
defineModel: Macros['defineModel'],
147147
props: Record<string, string | null>,
148148
) {
149149
for (const { expression, isRequired } of defineModel || []) {

packages/macros/src/core/define-style.ts

+33-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { walkAST, type MagicStringAST } from '@vue-macros/common'
22
import hash from 'hash-sum'
33
import { helperPrefix } from './helper'
4-
import { isFunctionalNode, type DefineStyle, type FunctionalNode } from '.'
4+
import {
5+
isFunctionalNode,
6+
type DefineStyle,
7+
type FunctionalNode,
8+
type Macros,
9+
} from '.'
510
import type { Node } from '@babel/types'
611

712
export function transformDefineStyle(
@@ -10,6 +15,7 @@ export function transformDefineStyle(
1015
root: FunctionalNode | undefined,
1116
s: MagicStringAST,
1217
importMap: Map<string, string>,
18+
{ defineSlots }: Macros,
1319
): void {
1420
const { expression, lang, isCssModules } = defineStyle
1521
if (expression.arguments[0]?.type !== 'TemplateLiteral') return
@@ -62,14 +68,37 @@ export function transformDefineStyle(
6268
}
6369
}
6470

65-
if (scoped && returnExpression) {
66-
walkAST<Node>(returnExpression, {
71+
if (scoped && root) {
72+
const slotNames = defineSlots?.id
73+
? defineSlots.id.type === 'Identifier'
74+
? defineSlots.id.name
75+
: defineSlots.id.type === 'ObjectPattern'
76+
? defineSlots.id.properties.map((prop) =>
77+
s.sliceNode(
78+
prop.type === 'RestElement' ? prop.argument : prop.value,
79+
),
80+
)
81+
: []
82+
: []
83+
walkAST<Node>(root, {
6784
enter(node) {
6885
if (
6986
node.type === 'JSXElement' &&
7087
s.sliceNode(node.openingElement.name) !== 'template'
7188
) {
72-
s.appendRight(node.openingElement.name.end!, ` data-v-${scopeId}=""`)
89+
let subfix = ''
90+
if (slotNames.length) {
91+
const name = s.sliceNode(
92+
node.openingElement.name.type === 'JSXMemberExpression'
93+
? node.openingElement.name.object
94+
: node.openingElement.name,
95+
)
96+
subfix = slotNames.includes(name) ? '-s' : ''
97+
}
98+
s.appendRight(
99+
node.openingElement.name.end!,
100+
` data-v-${scopeId}${subfix}=""`,
101+
)
73102
}
74103
},
75104
})

packages/macros/src/core/index.ts

+23-6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import type {
1818
CallExpression,
1919
FunctionDeclaration,
2020
FunctionExpression,
21+
LVal,
2122
Node,
2223
Program,
2324
} from '@babel/types'
@@ -35,13 +36,16 @@ export type DefineStyle = {
3536
lang: string
3637
}
3738

38-
export type RootMapValue = {
39+
export type Macros = {
3940
defineComponent?: CallExpression
4041
defineModel?: {
4142
expression: CallExpression
4243
isRequired: boolean
4344
}[]
44-
defineSlots?: CallExpression
45+
defineSlots?: {
46+
expression: CallExpression
47+
id?: LVal
48+
}
4549
defineExpose?: CallExpression
4650
defineStyle?: DefineStyle[]
4751
}
@@ -59,7 +63,14 @@ export function transformJsxMacros(
5963
let defineStyleIndex = 0
6064
for (const [root, macros] of rootMap) {
6165
macros.defineStyle?.forEach((defineStyle) => {
62-
transformDefineStyle(defineStyle, defineStyleIndex++, root, s, importMap)
66+
transformDefineStyle(
67+
defineStyle,
68+
defineStyleIndex++,
69+
root,
70+
s,
71+
importMap,
72+
macros,
73+
)
6374
})
6475

6576
if (root === undefined) continue
@@ -109,7 +120,7 @@ export function transformJsxMacros(
109120
})
110121
}
111122
if (macros.defineSlots) {
112-
transformDefineSlots(macros.defineSlots, s)
123+
transformDefineSlots(macros.defineSlots.expression, s)
113124
}
114125
if (macros.defineExpose) {
115126
transformDefineExpose(macros.defineExpose, s, options.version)
@@ -121,7 +132,7 @@ export function transformJsxMacros(
121132

122133
function getRootMap(ast: Program, s: MagicStringAST, options: OptionsResolved) {
123134
const parents: (Node | undefined | null)[] = []
124-
const rootMap = new Map<FunctionalNode | undefined, RootMapValue>()
135+
const rootMap = new Map<FunctionalNode | undefined, Macros>()
125136
walkAST<Node>(ast, {
126137
enter(node, parent) {
127138
parents.unshift(parent)
@@ -174,7 +185,13 @@ function getRootMap(ast: Program, s: MagicStringAST, options: OptionsResolved) {
174185
lang,
175186
})
176187
} else if (options.defineSlots.alias.includes(macroName)) {
177-
rootMap.get(root)!.defineSlots = macroExpression
188+
rootMap.get(root)!.defineSlots = {
189+
expression: macroExpression,
190+
id:
191+
node.type === 'VariableDeclaration'
192+
? node.declarations[0].id
193+
: undefined,
194+
}
178195
} else if (options.defineExpose.alias.includes(macroName)) {
179196
rootMap.get(root)!.defineExpose = macroExpression
180197
}

packages/macros/tests/__snapshots__/fixtures.spec.ts.snap

+6-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,8 @@ export default function (__MACROS_props) {
250250
`;
251251
252252
exports[`fixtures > ./fixtures/define-style.tsx 1`] = `
253-
"import "vue-jsx-vapor/macros/define-style/0?scopeId=4e9d5cd0&scoped=true&lang.css";import style1 from "vue-jsx-vapor/macros/define-style/1?scopeId=426a859d&scoped=true&lang.module.scss";import "vue-jsx-vapor/macros/define-style/2?scopeId=63a7910c&scoped=false&lang.scss";import { defineComponent, ref } from 'vue'
253+
"import "vue-jsx-vapor/macros/define-style/0?scopeId=4e9d5cd0&scoped=true&lang.css";import style1 from "vue-jsx-vapor/macros/define-style/1?scopeId=426a859d&scoped=true&lang.module.scss";
254+
import { useSlots as __MACROS_useSlots } from "vue";import "vue-jsx-vapor/macros/define-style/2?scopeId=63a7910c&scoped=false&lang.scss";import { defineComponent, ref } from 'vue'
254255
255256
export const Comp = (__MACROS_props) => {
256257
const color = ref('red')
@@ -261,12 +262,16 @@ export const Comp = (__MACROS_props) => {
261262
export default defineComponent((__MACROS_props) => {
262263
const color = ref('red')
263264
const styles = style1
265+
const { default: Default, ...slots } = Object.assign({}, __MACROS_useSlots())
264266
return () => (
265267
<>
266268
<div {...{style:{'--426a859d-color-value': color.value}}} data-v-426a859d="" class={styles.bar}>foo</div>
267269
<div {...{style:{'--426a859d-color-value': color.value}}} data-v-426a859d="" class="bar">
268270
<span data-v-426a859d="">bar</span>
269271
</div>
272+
273+
<Default {...{style:{'--426a859d-color-value': color.value}}} data-v-426a859d-s="" />
274+
<slots.title {...{style:{'--426a859d-color-value': color.value}}} data-v-426a859d-s="" />
270275
</>
271276
)
272277
})

packages/macros/tests/fixtures/define-style.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,16 @@ export default defineComponent(() => {
2020
}
2121
}
2222
`)
23+
const { default: Default, ...slots } = defineSlots()
2324
return () => (
2425
<>
2526
<div class={styles.bar}>foo</div>
2627
<div class="bar">
2728
<span>bar</span>
2829
</div>
30+
31+
<Default />
32+
<slots.title />
2933
</>
3034
)
3135
})

0 commit comments

Comments
 (0)