Skip to content

Commit 5a3d45a

Browse files
committed
fix(sfc): ensure consistent dev/prod behavior for non-reactive variables declared in <script setup>
fix #5655
1 parent f73925d commit 5a3d45a

6 files changed

+19
-7
lines changed

packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -1280,7 +1280,7 @@ export default {
12801280
function c() {}
12811281
class d {}
12821282

1283-
return { aa, bb, cc, dd, a, b, c, d, xx, x }
1283+
return { get aa() { return aa }, bb, cc, dd, get a() { return a }, b, c, d, xx, x }
12841284
}
12851285

12861286
}"

packages/compiler-sfc/__tests__/__snapshots__/compileScriptRefTransform.spec.ts.snap

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default {
1515
let c = () => {}
1616
let d
1717
18-
return { foo, a, b, c, d, ref, shallowRef }
18+
return { foo, a, b, get c() { return c }, get d() { return d }, ref, shallowRef }
1919
}
2020
2121
}"
@@ -36,7 +36,7 @@ export default {
3636
let c = () => {}
3737
let d
3838
39-
return { foo, a, b, c, d }
39+
return { foo, a, b, get c() { return c }, get d() { return d } }
4040
}
4141
4242
}"

packages/compiler-sfc/__tests__/__snapshots__/cssVars.spec.ts.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ _useCssVars(_ctx => ({
8484
let b = 200
8585
let foo = 300
8686
87-
return { a, b, foo }
87+
return { get a() { return a }, get b() { return b }, get foo() { return foo } }
8888
}
8989
9090
}"

packages/compiler-sfc/__tests__/compileScript.spec.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ describe('SFC compile <script setup>', () => {
2020
class dd {}
2121
</script>
2222
`)
23-
expect(content).toMatch('return { aa, bb, cc, dd, a, b, c, d, xx, x }')
23+
expect(content).toMatch(
24+
'return { get aa() { return aa }, bb, cc, dd, get a() { return a }, b, c, d, xx, x }'
25+
)
2426
expect(bindings).toStrictEqual({
2527
x: BindingTypes.SETUP_MAYBE_REF,
2628
a: BindingTypes.SETUP_LET,

packages/compiler-sfc/__tests__/compileScriptRefTransform.spec.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ describe('sfc ref transform', () => {
3232
// normal declarations left untouched
3333
expect(content).toMatch(`let c = () => {}`)
3434
expect(content).toMatch(`let d`)
35-
expect(content).toMatch(`return { foo, a, b, c, d, ref, shallowRef }`)
35+
expect(content).toMatch(
36+
`return { foo, a, b, get c() { return c }, get d() { return d }, ref, shallowRef }`
37+
)
3638
assertCode(content)
3739
expect(bindings).toStrictEqual({
3840
foo: BindingTypes.SETUP_REF,

packages/compiler-sfc/src/compileScript.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -1484,7 +1484,15 @@ export function compileScript(
14841484
allBindings[key] = true
14851485
}
14861486
}
1487-
returned = `{ ${Object.keys(allBindings).join(', ')} }`
1487+
returned = `{ `
1488+
for (const key in allBindings) {
1489+
if (bindingMetadata[key] === BindingTypes.SETUP_LET) {
1490+
returned += `get ${key}() { return ${key} }, `
1491+
} else {
1492+
returned += `${key}, `
1493+
}
1494+
}
1495+
returned = returned.replace(/, $/, '') + ` }`
14881496
} else {
14891497
// inline mode
14901498
if (sfc.template && !sfc.template.src) {

0 commit comments

Comments
 (0)