Skip to content

Commit

Permalink
fix(sfc): ensure consistent dev/prod behavior for non-reactive variab…
Browse files Browse the repository at this point in the history
…les declared in `<script setup>`

fix #5655
  • Loading branch information
yyx990803 committed Nov 10, 2022
1 parent f73925d commit 5a3d45a
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 7 deletions.
Expand Up @@ -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 }
}

}"
Expand Down
Expand Up @@ -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 }
}
}"
Expand All @@ -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 } }
}
}"
Expand Down
Expand Up @@ -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 } }
}
}"
Expand Down
4 changes: 3 additions & 1 deletion packages/compiler-sfc/__tests__/compileScript.spec.ts
Expand Up @@ -20,7 +20,9 @@ describe('SFC compile <script setup>', () => {
class dd {}
</script>
`)
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,
Expand Down
Expand Up @@ -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,
Expand Down
10 changes: 9 additions & 1 deletion packages/compiler-sfc/src/compileScript.ts
Expand Up @@ -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) {
Expand Down

0 comments on commit 5a3d45a

Please sign in to comment.