diff --git a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap index d553fb04b3b..e9b5c3fa883 100644 --- a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap +++ b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap @@ -1280,7 +1280,7 @@ export default { function c() {} class d {} -return { aa, bb, cc, dd, a, b, c, d, xx, x } +return { get aa() { return aa }, bb, cc, dd, get a() { return a }, b, c, d, xx, x } } }" diff --git a/packages/compiler-sfc/__tests__/__snapshots__/compileScriptRefTransform.spec.ts.snap b/packages/compiler-sfc/__tests__/__snapshots__/compileScriptRefTransform.spec.ts.snap index b7c81a08c86..8df7f2897a6 100644 --- a/packages/compiler-sfc/__tests__/__snapshots__/compileScriptRefTransform.spec.ts.snap +++ b/packages/compiler-sfc/__tests__/__snapshots__/compileScriptRefTransform.spec.ts.snap @@ -15,7 +15,7 @@ export default { let c = () => {} let d -return { foo, a, b, c, d, ref, shallowRef } +return { foo, a, b, get c() { return c }, get d() { return d }, ref, shallowRef } } }" @@ -36,7 +36,7 @@ export default { let c = () => {} let d -return { foo, a, b, c, d } +return { foo, a, b, get c() { return c }, get d() { return d } } } }" diff --git a/packages/compiler-sfc/__tests__/__snapshots__/cssVars.spec.ts.snap b/packages/compiler-sfc/__tests__/__snapshots__/cssVars.spec.ts.snap index 4df59cc3428..e070087f43b 100644 --- a/packages/compiler-sfc/__tests__/__snapshots__/cssVars.spec.ts.snap +++ b/packages/compiler-sfc/__tests__/__snapshots__/cssVars.spec.ts.snap @@ -84,7 +84,7 @@ _useCssVars(_ctx => ({ let b = 200 let foo = 300 -return { a, b, foo } +return { get a() { return a }, get b() { return b }, get foo() { return foo } } } }" diff --git a/packages/compiler-sfc/__tests__/compileScript.spec.ts b/packages/compiler-sfc/__tests__/compileScript.spec.ts index b0a6c115e52..ea4daa0d71a 100644 --- a/packages/compiler-sfc/__tests__/compileScript.spec.ts +++ b/packages/compiler-sfc/__tests__/compileScript.spec.ts @@ -20,7 +20,9 @@ describe('SFC compile `) - expect(content).toMatch('return { aa, bb, cc, dd, a, b, c, d, xx, x }') + expect(content).toMatch( + 'return { get aa() { return aa }, bb, cc, dd, get a() { return a }, b, c, d, xx, x }' + ) expect(bindings).toStrictEqual({ x: BindingTypes.SETUP_MAYBE_REF, a: BindingTypes.SETUP_LET, diff --git a/packages/compiler-sfc/__tests__/compileScriptRefTransform.spec.ts b/packages/compiler-sfc/__tests__/compileScriptRefTransform.spec.ts index 88d62f2b478..6d601379749 100644 --- a/packages/compiler-sfc/__tests__/compileScriptRefTransform.spec.ts +++ b/packages/compiler-sfc/__tests__/compileScriptRefTransform.spec.ts @@ -32,7 +32,9 @@ describe('sfc ref transform', () => { // normal declarations left untouched expect(content).toMatch(`let c = () => {}`) expect(content).toMatch(`let d`) - expect(content).toMatch(`return { foo, a, b, c, d, ref, shallowRef }`) + expect(content).toMatch( + `return { foo, a, b, get c() { return c }, get d() { return d }, ref, shallowRef }` + ) assertCode(content) expect(bindings).toStrictEqual({ foo: BindingTypes.SETUP_REF, diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index e944b764725..74e3dbd7349 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -1484,7 +1484,15 @@ export function compileScript( allBindings[key] = true } } - returned = `{ ${Object.keys(allBindings).join(', ')} }` + returned = `{ ` + for (const key in allBindings) { + if (bindingMetadata[key] === BindingTypes.SETUP_LET) { + returned += `get ${key}() { return ${key} }, ` + } else { + returned += `${key}, ` + } + } + returned = returned.replace(/, $/, '') + ` }` } else { // inline mode if (sfc.template && !sfc.template.src) {