Skip to content

Commit 4768f26

Browse files
committed
fix(compiler-sfc/reactivity-transform): fix edge case where normal script has ref macros but script setup does not
1 parent a05b000 commit 4768f26

File tree

3 files changed

+45
-14
lines changed

3 files changed

+45
-14
lines changed

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

+17
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,23 @@ exports[`sfc ref transform usage in normal <script> 1`] = `
7171
"
7272
`;
7373
74+
exports[`sfc ref transform usage with normal <script> (has macro usage) + <script setup> (no macro usage) 1`] = `
75+
"import { ref as _ref } from 'vue'
76+
77+
let data = _ref()
78+
79+
export default {
80+
setup(__props, { expose }) {
81+
expose();
82+
83+
console.log(data.value)
84+
85+
return { data }
86+
}
87+
88+
}"
89+
`;
90+
7491
exports[`sfc ref transform usage with normal <script> + <script setup> 1`] = `
7592
"import { ref as _ref } from 'vue'
7693

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

+13
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,19 @@ describe('sfc ref transform', () => {
146146
})
147147
})
148148

149+
test('usage with normal <script> (has macro usage) + <script setup> (no macro usage)', () => {
150+
const { content } = compileWithReactivityTransform(`
151+
<script>
152+
let data = $ref()
153+
</script>
154+
<script setup>
155+
console.log(data)
156+
</script>
157+
`)
158+
expect(content).toMatch(`console.log(data.value)`)
159+
assertCode(content)
160+
})
161+
149162
describe('errors', () => {
150163
test('defineProps/Emit() referencing ref declarations', () => {
151164
expect(() =>

packages/compiler-sfc/src/compileScript.ts

+15-14
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,7 @@ import { compileTemplate, SFCTemplateCompileOptions } from './compileTemplate'
4848
import { warnOnce } from './warn'
4949
import { rewriteDefault } from './rewriteDefault'
5050
import { createCache } from './cache'
51-
import {
52-
shouldTransform as shouldTransformRef,
53-
transformAST as transformRefAST
54-
} from '@vue/reactivity-transform'
51+
import { shouldTransform, transformAST } from '@vue/reactivity-transform'
5552

5653
// Special compiler macros
5754
const DEFINE_PROPS = 'defineProps'
@@ -143,7 +140,7 @@ export function compileScript(
143140
let { script, scriptSetup, source, filename } = sfc
144141
// feature flags
145142
// TODO remove support for deprecated options when out of experimental
146-
const enableRefTransform =
143+
const enableReactivityTransform =
147144
!!options.reactivityTransform ||
148145
!!options.refSugar ||
149146
!!options.refTransform
@@ -170,6 +167,8 @@ export function compileScript(
170167
scriptLang === 'tsx' ||
171168
scriptSetupLang === 'ts' ||
172169
scriptSetupLang === 'tsx'
170+
171+
// resolve parser plugins
173172
const plugins: ParserPlugin[] = []
174173
if (!isTS || scriptLang === 'tsx' || scriptSetupLang === 'tsx') {
175174
plugins.push('jsx')
@@ -193,11 +192,11 @@ export function compileScript(
193192
sourceType: 'module'
194193
}).program
195194
const bindings = analyzeScriptBindings(scriptAst.body)
196-
if (enableRefTransform && shouldTransformRef(content)) {
195+
if (enableReactivityTransform && shouldTransform(content)) {
197196
const s = new MagicString(source)
198197
const startOffset = script.loc.start.offset
199198
const endOffset = script.loc.end.offset
200-
const { importedHelpers } = transformRefAST(scriptAst, s, startOffset)
199+
const { importedHelpers } = transformAST(scriptAst, s, startOffset)
201200
if (importedHelpers.length) {
202201
s.prepend(
203202
`import { ${importedHelpers
@@ -862,14 +861,14 @@ export function compileScript(
862861
}
863862
}
864863

865-
// apply ref transform
866-
if (enableRefTransform && shouldTransformRef(script.content)) {
867-
const { rootRefs: rootVars, importedHelpers } = transformRefAST(
864+
// apply reactivity transform
865+
if (enableReactivityTransform && shouldTransform(script.content)) {
866+
const { rootRefs, importedHelpers } = transformAST(
868867
scriptAst,
869868
s,
870869
scriptStartOffset!
871870
)
872-
refBindings = rootVars
871+
refBindings = rootRefs
873872
for (const h of importedHelpers) {
874873
helperImports.add(h)
875874
}
@@ -1109,12 +1108,14 @@ export function compileScript(
11091108
}
11101109
}
11111110

1112-
// 3. Apply ref sugar transform
1111+
// 3. Apply reactivity transform
11131112
if (
1114-
(enableRefTransform && shouldTransformRef(scriptSetup.content)) ||
1113+
(enableReactivityTransform &&
1114+
// normal <script> had ref bindings that maybe used in <script setup>
1115+
(refBindings || shouldTransform(scriptSetup.content))) ||
11151116
propsDestructureDecl
11161117
) {
1117-
const { rootRefs, importedHelpers } = transformRefAST(
1118+
const { rootRefs, importedHelpers } = transformAST(
11181119
scriptSetupAst,
11191120
s,
11201121
startOffset,

0 commit comments

Comments
 (0)