Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.

Commit c73b288

Browse files
authored
feat: support defineExpose (#40)
1 parent b6783aa commit c73b288

File tree

4 files changed

+60
-13
lines changed

4 files changed

+60
-13
lines changed

src/core/macros.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export function applyMacros(nodes: Statement[]) {
4040
| TSInterfaceBody
4141
| undefined
4242
let emitsTypeDeclRaw: Node | undefined
43+
let exposeDecl: CallExpression['arguments'][number] | undefined
4344

4445
// props/emits declared via types
4546
const typeDeclaredProps: Record<string, PropTypeData> = {}
@@ -190,9 +191,18 @@ export function applyMacros(nodes: Statement[]) {
190191
}
191192

192193
function processDefineExpose(node: Node): boolean {
193-
if (isCallOf(node, DEFINE_EXPOSE))
194-
error(`Vue 2 does not support ${DEFINE_EXPOSE}()`, node)
195-
return false
194+
if (!isCallOf(node, DEFINE_EXPOSE))
195+
return false
196+
197+
if (exposeDecl)
198+
error(`duplicate ${DEFINE_EXPOSE}() call`, node)
199+
200+
if (node.arguments.length !== 1)
201+
error(`${DEFINE_EXPOSE}() requires one argument`, node)
202+
203+
exposeDecl = node.arguments[0]
204+
205+
return true
196206
}
197207

198208
function genRuntimeProps(props: Record<string, PropTypeData>) {
@@ -285,6 +295,7 @@ export function applyMacros(nodes: Statement[]) {
285295
return {
286296
nodes,
287297
props: getProps(),
298+
expose: exposeDecl,
288299
}
289300
}
290301

src/core/transformScriptSetup.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { getIdentifierDeclarations } from './identifiers'
1010
export function transformScriptSetup(sfc: ParsedSFC, options?: ScriptSetupTransformOptions) {
1111
const { scriptSetup, script, template } = sfc
1212

13-
const { nodes: body, props } = applyMacros(scriptSetup.ast.body)
13+
const { nodes: body, props, expose } = applyMacros(scriptSetup.ast.body)
1414

1515
const [hoisted, setupBody] = partition(
1616
body,
@@ -25,9 +25,14 @@ export function transformScriptSetup(sfc: ParsedSFC, options?: ScriptSetupTransf
2525
getIdentifierDeclarations(setupBody, declarations)
2626

2727
// filter out identifiers that are used in `<template>`
28-
const returns = Array.from(declarations)
28+
const returns: t.ObjectExpression['properties'] = Array.from(declarations)
2929
.filter(Boolean)
3030
.filter(i => template.identifiers.has(i))
31+
.map((i) => {
32+
const id = t.identifier(i)
33+
return t.objectProperty(id, id, false, true)
34+
})
35+
3136
const components = Array.from(declarations)
3237
.filter(Boolean)
3338
.filter(i => template.components.has(i)
@@ -92,14 +97,13 @@ export function transformScriptSetup(sfc: ParsedSFC, options?: ScriptSetupTransf
9297
// `__sfc_main.setup = () => {}`
9398
if (body.length) {
9499
hasBody = true
95-
const returnStatement = t.returnStatement(
96-
t.objectExpression(
97-
returns.map((i) => {
98-
const id = t.identifier(i)
99-
return t.objectProperty(id, id, false, true)
100-
}),
101-
),
102-
)
100+
const returnExpr = expose
101+
? t.callExpression(
102+
t.memberExpression(t.identifier('Object'), t.identifier('assign')),
103+
[t.objectExpression(returns), expose],
104+
)
105+
: t.objectExpression(returns)
106+
const returnStatement = t.returnStatement(returnExpr)
103107

104108
ast.body.push(
105109
t.expressionStatement(

test/__snapshots__/transform.test.ts.snap

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,29 @@ export default __sfc_main;
215215
"
216216
`;
217217
218+
exports[`transform fixtures test/fixtures/MacrosDefineExpose.vue 1`] = `
219+
"<template>
220+
<div>{{a}}</div>
221+
</template>
222+
223+
<script lang=\\"js\\">
224+
const __sfc_main = {};
225+
226+
__sfc_main.setup = (__props, __ctx) => {
227+
const a = ref(1);
228+
const b = 1;
229+
return Object.assign({
230+
a
231+
}, {
232+
b
233+
});
234+
};
235+
236+
export default __sfc_main;
237+
</script>
238+
"
239+
`;
240+
218241
exports[`transform fixtures test/fixtures/MacrosPure.vue 1`] = `
219242
"<template>
220243
<div>{{ msg }}</div>

test/fixtures/MacrosDefineExpose.vue

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<template>
2+
<div>{{a}}</div>
3+
</template>
4+
5+
<script setup lang="js">
6+
const a = ref(1);
7+
const b = 1;
8+
defineExpose({b});
9+
</script>

0 commit comments

Comments
 (0)