feat: defineExpose() support

This commit is contained in:
Evan You 2022-06-16 09:53:45 +08:00
parent bd8409bc98
commit 3c2707b62e
7 changed files with 101 additions and 161 deletions

View File

@ -1183,7 +1183,7 @@ export function compileScript(
s.prependLeft(startOffset, `\nlet __temp${any}, __restore${any}\n`) s.prependLeft(startOffset, `\nlet __temp${any}, __restore${any}\n`)
} }
const destructureElements = [`expose`] const destructureElements = hasDefineExposeCall ? [`expose`] : []
if (emitIdentifier) { if (emitIdentifier) {
destructureElements.push( destructureElements.push(
emitIdentifier === `emit` ? `emit` : `emit: ${emitIdentifier}` emitIdentifier === `emit` ? `emit` : `emit: ${emitIdentifier}`
@ -1256,9 +1256,6 @@ export function compileScript(
runtimeOptions += genRuntimeEmits(typeDeclaredEmits) runtimeOptions += genRuntimeEmits(typeDeclaredEmits)
} }
// <script setup> components are closed by default. If the user did not
// explicitly call `defineExpose`, call expose() with no args.
const exposeCall = hasDefineExposeCall ? `` : ` expose();\n`
// wrap setup code with function. // wrap setup code with function.
if (isTS) { if (isTS) {
// for TS, make sure the exported type is still valid type with // for TS, make sure the exported type is still valid type with
@ -1273,7 +1270,7 @@ export function compileScript(
`defineComponent` `defineComponent`
)}({${def}${runtimeOptions}\n ${ )}({${def}${runtimeOptions}\n ${
hasAwait ? `async ` : `` hasAwait ? `async ` : ``
}setup(${args}) {\n${exposeCall}` }setup(${args}) {\n`
) )
s.appendRight(endOffset, `})`) s.appendRight(endOffset, `})`)
} else { } else {
@ -1283,14 +1280,14 @@ export function compileScript(
s.prependLeft( s.prependLeft(
startOffset, startOffset,
`\nexport default /*#__PURE__*/Object.assign(${DEFAULT_VAR}, {${runtimeOptions}\n ` + `\nexport default /*#__PURE__*/Object.assign(${DEFAULT_VAR}, {${runtimeOptions}\n ` +
`${hasAwait ? `async ` : ``}setup(${args}) {\n${exposeCall}` `${hasAwait ? `async ` : ``}setup(${args}) {\n`
) )
s.appendRight(endOffset, `})`) s.appendRight(endOffset, `})`)
} else { } else {
s.prependLeft( s.prependLeft(
startOffset, startOffset,
`\nexport default {${runtimeOptions}\n ` + `\nexport default {${runtimeOptions}\n ` +
`${hasAwait ? `async ` : ``}setup(${args}) {\n${exposeCall}` `${hasAwait ? `async ` : ``}setup(${args}) {\n`
) )
s.appendRight(endOffset, `}`) s.appendRight(endOffset, `}`)
} }

View File

@ -3,8 +3,7 @@
exports[`SFC analyze <script> bindings > auto name inference > basic 1`] = ` exports[`SFC analyze <script> bindings > auto name inference > basic 1`] = `
"export default { "export default {
__name: 'FooBar', __name: 'FooBar',
setup(__props, { expose }) { setup(__props) {
expose();
const a = 1 const a = 1
return { a } return { a }
} }
@ -19,8 +18,7 @@ exports[`SFC analyze <script> bindings > auto name inference > do not overwrite
}) })
export default /*#__PURE__*/Object.assign(__default__, { export default /*#__PURE__*/Object.assign(__default__, {
setup(__props, { expose }) { setup(__props) {
expose();
const a = 1 const a = 1
return { a, defineComponent } return { a, defineComponent }
} }
@ -34,8 +32,7 @@ exports[`SFC analyze <script> bindings > auto name inference > do not overwrite
} }
export default /*#__PURE__*/Object.assign(__default__, { export default /*#__PURE__*/Object.assign(__default__, {
setup(__props, { expose }) { setup(__props) {
expose();
const a = 1 const a = 1
return { a } return { a }
} }
@ -48,8 +45,7 @@ exports[`SFC compile <script setup> > <script> after <script setup> the script c
import { x } from './x' import { x } from './x'
export default { export default {
setup(__props, { expose }) { setup(__props) {
expose();
return { n, x } return { n, x }
@ -66,8 +62,7 @@ exports[`SFC compile <script setup> > <script> and <script setup> co-usage > scr
const __default__ = {} const __default__ = {}
export default /*#__PURE__*/Object.assign(__default__, { export default /*#__PURE__*/Object.assign(__default__, {
setup(__props, { expose }) { setup(__props) {
expose();
x() x()
@ -84,8 +79,7 @@ exports[`SFC compile <script setup> > <script> and <script setup> co-usage > scr
import { x } from './x' import { x } from './x'
export default /*#__PURE__*/Object.assign(__default__, { export default /*#__PURE__*/Object.assign(__default__, {
setup(__props, { expose }) { setup(__props) {
expose();
x() x()
@ -106,8 +100,7 @@ import { x } from './x'
export default /*#__PURE__*/_defineComponent({ export default /*#__PURE__*/_defineComponent({
...__default__, ...__default__,
setup(__props, { expose }) { setup(__props) {
expose();
x() x()
@ -127,8 +120,7 @@ const __default__ = def
import { x } from './x' import { x } from './x'
export default /*#__PURE__*/Object.assign(__default__, { export default /*#__PURE__*/Object.assign(__default__, {
setup(__props, { expose }) { setup(__props) {
expose();
x() x()
@ -147,8 +139,7 @@ exports[`SFC compile <script setup> > <script> and <script setup> co-usage > spa
} }
export default /*#__PURE__*/Object.assign(__default__, { export default /*#__PURE__*/Object.assign(__default__, {
setup(__props, { expose }) { setup(__props) {
expose();
x() x()
@ -167,8 +158,7 @@ exports[`SFC compile <script setup> > <script> and <script setup> co-usage > spa
} }
export default /*#__PURE__*/Object.assign(__default__, { export default /*#__PURE__*/Object.assign(__default__, {
setup(__props, { expose }) { setup(__props) {
expose();
x() x()
@ -182,8 +172,7 @@ exports[`SFC compile <script setup> > async/await detection > expression stateme
"import { withAsyncContext as _withAsyncContext } from 'vue' "import { withAsyncContext as _withAsyncContext } from 'vue'
export default { export default {
async setup(__props, { expose }) { async setup(__props) {
expose();
let __temp, __restore let __temp, __restore
;( ;(
@ -201,8 +190,7 @@ exports[`SFC compile <script setup> > async/await detection > multiple \`if for\
"import { withAsyncContext as _withAsyncContext } from 'vue' "import { withAsyncContext as _withAsyncContext } from 'vue'
export default { export default {
async setup(__props, { expose }) { async setup(__props) {
expose();
let __temp, __restore let __temp, __restore
if (ok) { if (ok) {
@ -236,8 +224,7 @@ exports[`SFC compile <script setup> > async/await detection > multiple \`if whil
"import { withAsyncContext as _withAsyncContext } from 'vue' "import { withAsyncContext as _withAsyncContext } from 'vue'
export default { export default {
async setup(__props, { expose }) { async setup(__props) {
expose();
let __temp, __restore let __temp, __restore
if (ok) { if (ok) {
@ -291,8 +278,7 @@ exports[`SFC compile <script setup> > async/await detection > multiple \`if\` ne
"import { withAsyncContext as _withAsyncContext } from 'vue' "import { withAsyncContext as _withAsyncContext } from 'vue'
export default { export default {
async setup(__props, { expose }) { async setup(__props) {
expose();
let __temp, __restore let __temp, __restore
if (ok) { if (ok) {
@ -368,8 +354,7 @@ exports[`SFC compile <script setup> > async/await detection > nested await 1`] =
"import { withAsyncContext as _withAsyncContext } from 'vue' "import { withAsyncContext as _withAsyncContext } from 'vue'
export default { export default {
async setup(__props, { expose }) { async setup(__props) {
expose();
let __temp, __restore let __temp, __restore
;( ;(
@ -392,8 +377,7 @@ exports[`SFC compile <script setup> > async/await detection > nested await 2`] =
"import { withAsyncContext as _withAsyncContext } from 'vue' "import { withAsyncContext as _withAsyncContext } from 'vue'
export default { export default {
async setup(__props, { expose }) { async setup(__props) {
expose();
let __temp, __restore let __temp, __restore
;( ;(
@ -416,8 +400,7 @@ exports[`SFC compile <script setup> > async/await detection > nested await 3`] =
"import { withAsyncContext as _withAsyncContext } from 'vue' "import { withAsyncContext as _withAsyncContext } from 'vue'
export default { export default {
async setup(__props, { expose }) { async setup(__props) {
expose();
let __temp, __restore let __temp, __restore
;( ;(
@ -445,8 +428,7 @@ exports[`SFC compile <script setup> > async/await detection > nested leading awa
"import { withAsyncContext as _withAsyncContext } from 'vue' "import { withAsyncContext as _withAsyncContext } from 'vue'
export default { export default {
async setup(__props, { expose }) { async setup(__props) {
expose();
let __temp, __restore let __temp, __restore
foo() foo()
@ -471,8 +453,7 @@ exports[`SFC compile <script setup> > async/await detection > nested statements
"import { withAsyncContext as _withAsyncContext } from 'vue' "import { withAsyncContext as _withAsyncContext } from 'vue'
export default { export default {
async setup(__props, { expose }) { async setup(__props) {
expose();
let __temp, __restore let __temp, __restore
if (ok) { ( if (ok) { (
@ -494,8 +475,7 @@ exports[`SFC compile <script setup> > async/await detection > ref 1`] = `
"import { withAsyncContext as _withAsyncContext } from 'vue' "import { withAsyncContext as _withAsyncContext } from 'vue'
export default { export default {
async setup(__props, { expose }) { async setup(__props) {
expose();
let __temp, __restore let __temp, __restore
let a = $ref(1 + (( let a = $ref(1 + ((
@ -512,8 +492,7 @@ return { a }
exports[`SFC compile <script setup> > async/await detection > should ignore await inside functions 1`] = ` exports[`SFC compile <script setup> > async/await detection > should ignore await inside functions 1`] = `
"export default { "export default {
setup(__props, { expose }) { setup(__props) {
expose();
async function foo() { await bar } async function foo() { await bar }
return { foo } return { foo }
} }
@ -523,8 +502,7 @@ return { foo }
exports[`SFC compile <script setup> > async/await detection > should ignore await inside functions 2`] = ` exports[`SFC compile <script setup> > async/await detection > should ignore await inside functions 2`] = `
"export default { "export default {
setup(__props, { expose }) { setup(__props) {
expose();
const foo = async () => { await bar } const foo = async () => { await bar }
return { foo } return { foo }
} }
@ -534,8 +512,7 @@ return { foo }
exports[`SFC compile <script setup> > async/await detection > should ignore await inside functions 3`] = ` exports[`SFC compile <script setup> > async/await detection > should ignore await inside functions 3`] = `
"export default { "export default {
setup(__props, { expose }) { setup(__props) {
expose();
const obj = { async method() { await bar }} const obj = { async method() { await bar }}
return { obj } return { obj }
} }
@ -545,8 +522,7 @@ return { obj }
exports[`SFC compile <script setup> > async/await detection > should ignore await inside functions 4`] = ` exports[`SFC compile <script setup> > async/await detection > should ignore await inside functions 4`] = `
"export default { "export default {
setup(__props, { expose }) { setup(__props) {
expose();
const cls = class Foo { async method() { await bar }} const cls = class Foo { async method() { await bar }}
return { cls } return { cls }
} }
@ -558,8 +534,7 @@ exports[`SFC compile <script setup> > async/await detection > single line condit
"import { withAsyncContext as _withAsyncContext } from 'vue' "import { withAsyncContext as _withAsyncContext } from 'vue'
export default { export default {
async setup(__props, { expose }) { async setup(__props) {
expose();
let __temp, __restore let __temp, __restore
if (false) ( if (false) (
@ -577,8 +552,7 @@ exports[`SFC compile <script setup> > async/await detection > variable 1`] = `
"import { withAsyncContext as _withAsyncContext } from 'vue' "import { withAsyncContext as _withAsyncContext } from 'vue'
export default { export default {
async setup(__props, { expose }) { async setup(__props) {
expose();
let __temp, __restore let __temp, __restore
const a = 1 + (( const a = 1 + ((
@ -595,8 +569,7 @@ return { a }
exports[`SFC compile <script setup> > binding analysis for destructure 1`] = ` exports[`SFC compile <script setup> > binding analysis for destructure 1`] = `
"export default { "export default {
setup(__props, { expose }) { setup(__props) {
expose();
const { foo, b: bar, ['x' + 'y']: baz, x: { y, zz: { z }}} = {} const { foo, b: bar, ['x' + 'y']: baz, x: { y, zz: { z }}} = {}
@ -609,8 +582,7 @@ return { foo, bar, baz, y, z }
exports[`SFC compile <script setup> > defineEmits() 1`] = ` exports[`SFC compile <script setup> > defineEmits() 1`] = `
"export default { "export default {
emits: ['foo', 'bar'], emits: ['foo', 'bar'],
setup(__props, { expose, emit: myEmit }) { setup(__props, { emit: myEmit }) {
expose();
@ -637,8 +609,7 @@ exports[`SFC compile <script setup> > defineProps w/ external definition 1`] = `
export default { export default {
props: propsModel, props: propsModel,
setup(__props, { expose }) { setup(__props) {
expose();
const props = __props const props = __props
@ -655,8 +626,7 @@ exports[`SFC compile <script setup> > defineProps w/ leading code 1`] = `
export default { export default {
props: {}, props: {},
setup(__props, { expose }) { setup(__props) {
expose();
const props = __props const props = __props
@ -672,8 +642,7 @@ exports[`SFC compile <script setup> > defineProps() 1`] = `
props: { props: {
foo: String foo: String
}, },
setup(__props, { expose }) { setup(__props) {
expose();
const props = __props const props = __props
@ -690,8 +659,7 @@ exports[`SFC compile <script setup> > defineProps/defineEmits in multi-variable
"export default { "export default {
props: ['item'], props: ['item'],
emits: ['a'], emits: ['a'],
setup(__props, { expose, emit }) { setup(__props, { emit }) {
expose();
const props = __props const props = __props
@ -707,8 +675,7 @@ exports[`SFC compile <script setup> > defineProps/defineEmits in multi-variable
"export default { "export default {
props: ['item'], props: ['item'],
emits: ['a'], emits: ['a'],
setup(__props, { expose, emit }) { setup(__props, { emit }) {
expose();
const props = __props const props = __props
@ -725,8 +692,7 @@ exports[`SFC compile <script setup> > dev mode import usage check > TS annotatio
import { Foo, Baz, Qux, Fred } from './x' import { Foo, Baz, Qux, Fred } from './x'
export default /*#__PURE__*/_defineComponent({ export default /*#__PURE__*/_defineComponent({
setup(__props, { expose }) { setup(__props) {
expose();
const a = 1 const a = 1
function b() {} function b() {}
@ -742,8 +708,7 @@ exports[`SFC compile <script setup> > dev mode import usage check > attribute ex
import { bar, baz } from './x' import { bar, baz } from './x'
export default /*#__PURE__*/_defineComponent({ export default /*#__PURE__*/_defineComponent({
setup(__props, { expose }) { setup(__props) {
expose();
const cond = true const cond = true
@ -758,8 +723,7 @@ exports[`SFC compile <script setup> > dev mode import usage check > components 1
import { FooBar, FooBaz, FooQux, foo } from './x' import { FooBar, FooBaz, FooQux, foo } from './x'
export default /*#__PURE__*/_defineComponent({ export default /*#__PURE__*/_defineComponent({
setup(__props, { expose }) { setup(__props) {
expose();
const fooBar: FooBar = 1 const fooBar: FooBar = 1
@ -774,8 +738,7 @@ exports[`SFC compile <script setup> > dev mode import usage check > directive 1`
import { vMyDir } from './x' import { vMyDir } from './x'
export default /*#__PURE__*/_defineComponent({ export default /*#__PURE__*/_defineComponent({
setup(__props, { expose }) { setup(__props) {
expose();
return { vMyDir } return { vMyDir }
@ -789,8 +752,7 @@ exports[`SFC compile <script setup> > dev mode import usage check > js template
import { VAR, VAR2, VAR3 } from './x' import { VAR, VAR2, VAR3 } from './x'
export default /*#__PURE__*/_defineComponent({ export default /*#__PURE__*/_defineComponent({
setup(__props, { expose }) { setup(__props) {
expose();
return { VAR, VAR3 } return { VAR, VAR3 }
@ -804,8 +766,7 @@ exports[`SFC compile <script setup> > dev mode import usage check > last tag 1`]
import { FooBaz, Last } from './x' import { FooBaz, Last } from './x'
export default /*#__PURE__*/_defineComponent({ export default /*#__PURE__*/_defineComponent({
setup(__props, { expose }) { setup(__props) {
expose();
return { FooBaz, Last } return { FooBaz, Last }
@ -819,8 +780,7 @@ exports[`SFC compile <script setup> > dev mode import usage check > vue interpol
import { x, y, z, x$y } from './x' import { x, y, z, x$y } from './x'
export default /*#__PURE__*/_defineComponent({ export default /*#__PURE__*/_defineComponent({
setup(__props, { expose }) { setup(__props) {
expose();
return { x, z, x$y } return { x, z, x$y }
@ -841,8 +801,7 @@ export default {
emits: { emits: {
foo: () => bar > 1 foo: () => bar > 1
}, },
setup(__props, { expose }) { setup(__props) {
expose();
@ -863,8 +822,7 @@ exports[`SFC compile <script setup> > errors > should allow defineProps/Emit() r
emits: { emits: {
foo: bar => bar > 1 foo: bar => bar > 1
}, },
setup(__props, { expose }) { setup(__props) {
expose();
const bar = 1 const bar = 1
@ -880,8 +838,7 @@ exports[`SFC compile <script setup> > imports > import dedupe between <script> a
"import { x } from './x' "import { x } from './x'
export default { export default {
setup(__props, { expose }) { setup(__props) {
expose();
x() x()
@ -897,8 +854,7 @@ exports[`SFC compile <script setup> > imports > should allow defineProps/Emit at
export default { export default {
props: ['foo'], props: ['foo'],
emits: ['bar'], emits: ['bar'],
setup(__props, { expose }) { setup(__props) {
expose();
@ -915,8 +871,7 @@ exports[`SFC compile <script setup> > imports > should extract comment for impor
import b from 'b' import b from 'b'
export default { export default {
setup(__props, { expose }) { setup(__props) {
expose();
return { a, b } return { a, b }
@ -930,8 +885,7 @@ exports[`SFC compile <script setup> > imports > should hoist and expose imports
import 'foo/css' import 'foo/css'
export default { export default {
setup(__props, { expose }) { setup(__props) {
expose();
return { ref } return { ref }
@ -950,8 +904,7 @@ exports[`SFC compile <script setup> > should expose top level declarations 1`] =
import { x } from './x' import { x } from './x'
export default { export default {
setup(__props, { expose }) { setup(__props) {
expose();
let a = 1 let a = 1
const b = 2 const b = 2
@ -969,8 +922,7 @@ exports[`SFC compile <script setup> > with TypeScript > const Enum 1`] = `
const enum Foo { A = 123 } const enum Foo { A = 123 }
export default /*#__PURE__*/_defineComponent({ export default /*#__PURE__*/_defineComponent({
setup(__props, { expose }) { setup(__props) {
expose();
return { Foo } return { Foo }
@ -985,8 +937,7 @@ export interface Emits { (e: 'foo' | 'bar'): void }
export default /*#__PURE__*/_defineComponent({ export default /*#__PURE__*/_defineComponent({
emits: [\\"foo\\", \\"bar\\"], emits: [\\"foo\\", \\"bar\\"],
setup(__props, { expose, emit }: { emit: ({ (e: 'foo' | 'bar'): void }), expose: any, slots: any, attrs: any }) { setup(__props, { emit }: { emit: ({ (e: 'foo' | 'bar'): void }), expose: any, slots: any, attrs: any }) {
expose();
@ -1002,8 +953,7 @@ export type Emits = { (e: 'foo' | 'bar'): void }
export default /*#__PURE__*/_defineComponent({ export default /*#__PURE__*/_defineComponent({
emits: [\\"foo\\", \\"bar\\"], emits: [\\"foo\\", \\"bar\\"],
setup(__props, { expose, emit }: { emit: ({ (e: 'foo' | 'bar'): void }), expose: any, slots: any, attrs: any }) { setup(__props, { emit }: { emit: ({ (e: 'foo' | 'bar'): void }), expose: any, slots: any, attrs: any }) {
expose();
@ -1019,8 +969,7 @@ interface Emits { (e: 'foo' | 'bar'): void }
export default /*#__PURE__*/_defineComponent({ export default /*#__PURE__*/_defineComponent({
emits: [\\"foo\\", \\"bar\\"], emits: [\\"foo\\", \\"bar\\"],
setup(__props, { expose, emit }: { emit: ({ (e: 'foo' | 'bar'): void }), expose: any, slots: any, attrs: any }) { setup(__props, { emit }: { emit: ({ (e: 'foo' | 'bar'): void }), expose: any, slots: any, attrs: any }) {
expose();
@ -1036,8 +985,7 @@ export type Emits = (e: 'foo' | 'bar') => void
export default /*#__PURE__*/_defineComponent({ export default /*#__PURE__*/_defineComponent({
emits: [\\"foo\\", \\"bar\\"], emits: [\\"foo\\", \\"bar\\"],
setup(__props, { expose, emit }: { emit: ((e: 'foo' | 'bar') => void), expose: any, slots: any, attrs: any }) { setup(__props, { emit }: { emit: ((e: 'foo' | 'bar') => void), expose: any, slots: any, attrs: any }) {
expose();
@ -1053,8 +1001,7 @@ type Emits = (e: 'foo' | 'bar') => void
export default /*#__PURE__*/_defineComponent({ export default /*#__PURE__*/_defineComponent({
emits: [\\"foo\\", \\"bar\\"], emits: [\\"foo\\", \\"bar\\"],
setup(__props, { expose, emit }: { emit: ((e: 'foo' | 'bar') => void), expose: any, slots: any, attrs: any }) { setup(__props, { emit }: { emit: ((e: 'foo' | 'bar') => void), expose: any, slots: any, attrs: any }) {
expose();
@ -1070,8 +1017,7 @@ type Emits = { (e: 'foo' | 'bar'): void }
export default /*#__PURE__*/_defineComponent({ export default /*#__PURE__*/_defineComponent({
emits: [\\"foo\\", \\"bar\\"], emits: [\\"foo\\", \\"bar\\"],
setup(__props, { expose, emit }: { emit: ({ (e: 'foo' | 'bar'): void }), expose: any, slots: any, attrs: any }) { setup(__props, { emit }: { emit: ({ (e: 'foo' | 'bar'): void }), expose: any, slots: any, attrs: any }) {
expose();
@ -1086,8 +1032,7 @@ exports[`SFC compile <script setup> > with TypeScript > defineEmits w/ type (typ
export default /*#__PURE__*/_defineComponent({ export default /*#__PURE__*/_defineComponent({
emits: [\\"foo\\", \\"bar\\", \\"baz\\"], emits: [\\"foo\\", \\"bar\\", \\"baz\\"],
setup(__props, { expose, emit }: { emit: ({(e: 'foo' | 'bar'): void; (e: 'baz', id: number): void;}), expose: any, slots: any, attrs: any }) { setup(__props, { emit }: { emit: ({(e: 'foo' | 'bar'): void; (e: 'baz', id: number): void;}), expose: any, slots: any, attrs: any }) {
expose();
@ -1102,8 +1047,7 @@ exports[`SFC compile <script setup> > with TypeScript > defineEmits w/ type 1`]
export default /*#__PURE__*/_defineComponent({ export default /*#__PURE__*/_defineComponent({
emits: [\\"foo\\", \\"bar\\"], emits: [\\"foo\\", \\"bar\\"],
setup(__props, { expose, emit }: { emit: ((e: 'foo' | 'bar') => void), expose: any, slots: any, attrs: any }) { setup(__props, { emit }: { emit: ((e: 'foo' | 'bar') => void), expose: any, slots: any, attrs: any }) {
expose();
@ -1121,8 +1065,7 @@ export default /*#__PURE__*/_defineComponent({
props: { props: {
x: { type: Number, required: false } x: { type: Number, required: false }
}, },
setup(__props: any, { expose }) { setup(__props: any) {
expose();
@ -1141,8 +1084,7 @@ export default /*#__PURE__*/_defineComponent({
props: { props: {
x: { type: Number, required: false } x: { type: Number, required: false }
}, },
setup(__props: any, { expose }) { setup(__props: any) {
expose();
@ -1160,8 +1102,7 @@ export default /*#__PURE__*/_defineComponent({
props: { props: {
x: { type: Number, required: false } x: { type: Number, required: false }
}, },
setup(__props: any, { expose }) { setup(__props: any) {
expose();
@ -1179,8 +1120,7 @@ export default /*#__PURE__*/_defineComponent({
props: { props: {
x: { type: Number, required: false } x: { type: Number, required: false }
}, },
setup(__props: any, { expose }) { setup(__props: any) {
expose();
@ -1226,8 +1166,7 @@ export default /*#__PURE__*/_defineComponent({
intersection: { type: Object, required: true }, intersection: { type: Object, required: true },
foo: { type: [Function, null], required: true } foo: { type: [Function, null], required: true }
}, },
setup(__props: any, { expose }) { setup(__props: any) {
expose();
@ -1245,8 +1184,7 @@ export default /*#__PURE__*/_defineComponent({
props: { props: {
x: { type: Number, required: false } x: { type: Number, required: false }
}, },
setup(__props: any, { expose }) { setup(__props: any) {
expose();
@ -1262,8 +1200,7 @@ exports[`SFC compile <script setup> > with TypeScript > defineProps/Emit w/ runt
export default /*#__PURE__*/_defineComponent({ export default /*#__PURE__*/_defineComponent({
props: { foo: String }, props: { foo: String },
emits: ['a', 'b'], emits: ['a', 'b'],
setup(__props, { expose, emit }) { setup(__props, { emit }) {
expose();
const props = __props const props = __props
@ -1282,8 +1219,7 @@ export interface Foo {}
type Bar = {} type Bar = {}
export default /*#__PURE__*/_defineComponent({ export default /*#__PURE__*/_defineComponent({
setup(__props, { expose }) { setup(__props) {
expose();
return { } return { }
@ -1298,8 +1234,7 @@ import type { Foo } from './main.ts'
import { type Bar, Baz } from './main.ts' import { type Bar, Baz } from './main.ts'
export default /*#__PURE__*/_defineComponent({ export default /*#__PURE__*/_defineComponent({
setup(__props, { expose }) { setup(__props) {
expose();
return { Baz } return { Baz }
@ -1313,8 +1248,7 @@ exports[`SFC compile <script setup> > with TypeScript > runtime Enum 1`] = `
enum Foo { A = 123 } enum Foo { A = 123 }
export default /*#__PURE__*/_defineComponent({ export default /*#__PURE__*/_defineComponent({
setup(__props, { expose }) { setup(__props) {
expose();
return { Foo } return { Foo }
@ -1332,8 +1266,7 @@ enum Foo { A = 123 }
enum B { B = \\"B\\" } enum B { B = \\"B\\" }
export default /*#__PURE__*/_defineComponent({ export default /*#__PURE__*/_defineComponent({
setup(__props, { expose }) { setup(__props) {
expose();
return { D, C, B, Foo } return { D, C, B, Foo }
@ -1352,8 +1285,7 @@ export default /*#__PURE__*/_defineComponent({
bar: { type: Number, required: false }, bar: { type: Number, required: false },
baz: { type: Boolean, required: true } baz: { type: Boolean, required: true }
}, { ...defaults }), }, { ...defaults }),
setup(__props: any, { expose }) { setup(__props: any) {
expose();
const props = __props as { const props = __props as {
foo?: string foo?: string
@ -1379,8 +1311,7 @@ export default /*#__PURE__*/_defineComponent({
baz: { type: Boolean, required: true }, baz: { type: Boolean, required: true },
qux: { type: Function, required: false, default() { return 1 } } qux: { type: Function, required: false, default() { return 1 } }
}, },
setup(__props: any, { expose }) { setup(__props: any) {
expose();
const props = __props as { foo: string, bar?: number, baz: boolean, qux(): number } const props = __props as { foo: string, bar?: number, baz: boolean, qux(): number }

View File

@ -102,7 +102,7 @@ const bar = 1
// should remove defineOptions import and call // should remove defineOptions import and call
expect(content).not.toMatch('defineProps') expect(content).not.toMatch('defineProps')
// should generate correct setup signature // should generate correct setup signature
expect(content).toMatch(`setup(__props, { expose }) {`) expect(content).toMatch(`setup(__props) {`)
// should assign user identifier to it // should assign user identifier to it
expect(content).toMatch(`const props = __props`) expect(content).toMatch(`const props = __props`)
// should include context options in default export // should include context options in default export
@ -149,7 +149,7 @@ const myEmit = defineEmits(['foo', 'bar'])
// should remove defineOptions import and call // should remove defineOptions import and call
expect(content).not.toMatch('defineEmits') expect(content).not.toMatch('defineEmits')
// should generate correct setup signature // should generate correct setup signature
expect(content).toMatch(`setup(__props, { expose, emit: myEmit }) {`) expect(content).toMatch(`setup(__props, { emit: myEmit }) {`)
// should include context options in default export // should include context options in default export
expect(content).toMatch(`export default { expect(content).toMatch(`export default {
emits: ['foo', 'bar'],`) emits: ['foo', 'bar'],`)
@ -769,7 +769,7 @@ const emit = defineEmits(['a', 'b'])
expect(content).toMatch(`export default /*#__PURE__*/_defineComponent({ expect(content).toMatch(`export default /*#__PURE__*/_defineComponent({
props: { foo: String }, props: { foo: String },
emits: ['a', 'b'], emits: ['a', 'b'],
setup(__props, { expose, emit }) {`) setup(__props, { emit }) {`)
}) })
test('defineProps w/ type', () => { test('defineProps w/ type', () => {

View File

@ -13,8 +13,8 @@ export interface Config {
productionTip: boolean productionTip: boolean
performance: boolean performance: boolean
devtools: boolean devtools: boolean
errorHandler?: (err: Error, vm: Component, info: string) => void errorHandler?: (err: Error, vm: Component | null, info: string) => void
warnHandler?: (msg: string, vm: Component, trace: string) => void warnHandler?: (msg: string, vm: Component | null, trace: string) => void
ignoredElements: Array<string | RegExp> ignoredElements: Array<string | RegExp>
keyCodes: { [key: string]: number | Array<number> } keyCodes: { [key: string]: number | Array<number> }

View File

@ -3,7 +3,7 @@ import { noop, isArray, isFunction } from 'shared/util'
import type { Component } from 'types/component' import type { Component } from 'types/component'
import { currentInstance } from 'v3/currentInstance' import { currentInstance } from 'v3/currentInstance'
export let warn = noop export let warn: (msg: string, vm?: Component | null) => void = noop
export let tip = noop export let tip = noop
export let generateComponentTrace: (vm: Component) => string // work around flow check export let generateComponentTrace: (vm: Component) => string // work around flow check
export let formatComponentName: (vm: Component, includeFile?: false) => string export let formatComponentName: (vm: Component, includeFile?: false) => string

View File

@ -90,7 +90,7 @@ export type ComponentOptions = {
comments?: boolean comments?: boolean
inheritAttrs?: boolean inheritAttrs?: boolean
// Class API // Legacy API
abstract?: any abstract?: any
// private // private

View File

@ -12,6 +12,7 @@ export interface SetupContext {
attrs: Record<string, any> attrs: Record<string, any>
slots: Record<string, () => VNode[]> slots: Record<string, () => VNode[]>
emit: (event: string, ...args: any[]) => any emit: (event: string, ...args: any[]) => any
expose: (exposed: Record<string, any>) => void
} }
export function initSetup(vm: Component) { export function initSetup(vm: Component) {
@ -47,7 +48,7 @@ export function initSetup(vm: Component) {
if (!setupResult.__sfc) { if (!setupResult.__sfc) {
for (const key in setupResult) { for (const key in setupResult) {
if (!isReserved(key)) { if (!isReserved(key)) {
proxySetupProperty(vm, setupResult, key) proxyWithRefUnwrap(vm, setupResult, key)
} else if (__DEV__) { } else if (__DEV__) {
warn(`Avoid using variables that start with _ or $ in setup().`) warn(`Avoid using variables that start with _ or $ in setup().`)
} }
@ -56,7 +57,7 @@ export function initSetup(vm: Component) {
// exposed for compiled render fn // exposed for compiled render fn
const proxy = (vm._setupProxy = {}) const proxy = (vm._setupProxy = {})
for (const key in setupResult) { for (const key in setupResult) {
proxySetupProperty(proxy, setupResult, key) proxyWithRefUnwrap(proxy, setupResult, key)
} }
} }
} else if (__DEV__ && setupResult !== undefined) { } else if (__DEV__ && setupResult !== undefined) {
@ -69,22 +70,23 @@ export function initSetup(vm: Component) {
} }
} }
function proxySetupProperty( export function proxyWithRefUnwrap(
target: any, target: any,
setupResult: Record<string, any>, source: Record<string, any>,
key: string key: string
) { ) {
let raw = setupResult[key] let raw = source[key]
Object.defineProperty(target, key, { Object.defineProperty(target, key, {
enumerable: true, enumerable: true,
configurable: true, configurable: true,
get: () => (isRef(raw) ? raw.value : raw), get: () => (isRef(raw) ? raw.value : raw),
set: newVal => set: newVal =>
isRef(raw) ? (raw.value = newVal) : (raw = setupResult[key] = newVal) isRef(raw) ? (raw.value = newVal) : (raw = source[key] = newVal)
}) })
} }
function createSetupContext(vm: Component) { function createSetupContext(vm: Component): SetupContext {
let exposeCalled = false
return { return {
get attrs() { get attrs() {
return initAttrsProxy(vm) return initAttrsProxy(vm)
@ -93,8 +95,18 @@ function createSetupContext(vm: Component) {
return initSlotsProxy(vm) return initSlotsProxy(vm)
}, },
emit: bind(vm.$emit, vm) as any, emit: bind(vm.$emit, vm) as any,
expose() { expose(exposed?: Record<string, any>) {
// TODO if (__DEV__) {
if (exposeCalled) {
warn(`expose() should be called only once per setup().`, vm)
}
exposeCalled = true
}
if (exposed) {
Object.keys(exposed).forEach(key =>
proxyWithRefUnwrap(vm, exposed, key)
)
}
} }
} }
} }