Skip to content

Commit f5827fd

Browse files
committed
fix(compiler-sfc): do not resolve assets from setup bindings
when not using script setup fix #3270, fix #3275
1 parent 4d9f9fd commit f5827fd

File tree

5 files changed

+39
-3
lines changed

5 files changed

+39
-3
lines changed

packages/compiler-core/__tests__/transforms/transformElement.spec.ts

+24-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import {
22
CompilerOptions,
33
baseParse as parse,
44
transform,
5-
ErrorCodes
5+
ErrorCodes,
6+
BindingTypes
67
} from '../../src'
78
import {
89
RESOLVE_COMPONENT,
@@ -78,6 +79,28 @@ describe('compiler: element transform', () => {
7879
expect(root.components).toContain(`Example__self`)
7980
})
8081

82+
test('resolve component from setup bindings', () => {
83+
const { root, node } = parseWithElementTransform(`<Example/>`, {
84+
bindingMetadata: {
85+
Example: BindingTypes.SETUP_MAYBE_REF
86+
}
87+
})
88+
expect(root.helpers).not.toContain(RESOLVE_COMPONENT)
89+
expect(node.tag).toBe(`$setup["Example"]`)
90+
})
91+
92+
test('do not resolve component from non-script-setup bindings', () => {
93+
const bindingMetadata = {
94+
Example: BindingTypes.SETUP_MAYBE_REF
95+
}
96+
Object.defineProperty(bindingMetadata, '__isScriptSetup', { value: false })
97+
const { root } = parseWithElementTransform(`<Example/>`, {
98+
bindingMetadata
99+
})
100+
expect(root.helpers).toContain(RESOLVE_COMPONENT)
101+
expect(root.components).toContain(`Example`)
102+
})
103+
81104
test('static props', () => {
82105
const { node } = parseWithElementTransform(`<div id="foo" class="bar" />`)
83106
expect(node).toMatchObject({

packages/compiler-core/src/options.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,10 @@ export const enum BindingTypes {
9494
OPTIONS = 'options'
9595
}
9696

97-
export interface BindingMetadata {
97+
export type BindingMetadata = {
9898
[key: string]: BindingTypes | undefined
99+
} & {
100+
__isScriptSetup?: boolean
99101
}
100102

101103
interface SharedTransformCodegenOptions {

packages/compiler-core/src/transforms/transformElement.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ export function resolveComponentType(
286286

287287
function resolveSetupReference(name: string, context: TransformContext) {
288288
const bindings = context.bindingMetadata
289-
if (!bindings) {
289+
if (!bindings || bindings.__isScriptSetup === false) {
290290
return
291291
}
292292

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

+5
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,7 @@ describe('SFC analyze <script> bindings', () => {
962962

963963
expect(scriptAst).toBeDefined()
964964
})
965+
965966
it('recognizes props array declaration', () => {
966967
const { bindings } = compile(`
967968
<script>
@@ -974,6 +975,7 @@ describe('SFC analyze <script> bindings', () => {
974975
foo: BindingTypes.PROPS,
975976
bar: BindingTypes.PROPS
976977
})
978+
expect(bindings!.__isScriptSetup).toBe(false)
977979
})
978980

979981
it('recognizes props object declaration', () => {
@@ -997,6 +999,7 @@ describe('SFC analyze <script> bindings', () => {
997999
baz: BindingTypes.PROPS,
9981000
qux: BindingTypes.PROPS
9991001
})
1002+
expect(bindings!.__isScriptSetup).toBe(false)
10001003
})
10011004

10021005
it('recognizes setup return', () => {
@@ -1017,6 +1020,7 @@ describe('SFC analyze <script> bindings', () => {
10171020
foo: BindingTypes.SETUP_MAYBE_REF,
10181021
bar: BindingTypes.SETUP_MAYBE_REF
10191022
})
1023+
expect(bindings!.__isScriptSetup).toBe(false)
10201024
})
10211025

10221026
it('recognizes async setup return', () => {
@@ -1037,6 +1041,7 @@ describe('SFC analyze <script> bindings', () => {
10371041
foo: BindingTypes.SETUP_MAYBE_REF,
10381042
bar: BindingTypes.SETUP_MAYBE_REF
10391043
})
1044+
expect(bindings!.__isScriptSetup).toBe(false)
10401045
})
10411046

10421047
it('recognizes data return', () => {

packages/compiler-sfc/src/compileScript.ts

+6
Original file line numberDiff line numberDiff line change
@@ -1586,6 +1586,12 @@ function analyzeScriptBindings(ast: Statement[]): BindingMetadata {
15861586

15871587
function analyzeBindingsFromOptions(node: ObjectExpression): BindingMetadata {
15881588
const bindings: BindingMetadata = {}
1589+
// #3270, #3275
1590+
// mark non-script-setup so we don't resolve components/directives from these
1591+
Object.defineProperty(bindings, '__isScriptSetup', {
1592+
enumerable: false,
1593+
value: false
1594+
})
15891595
for (const property of node.properties) {
15901596
if (
15911597
property.type === 'ObjectProperty' &&

0 commit comments

Comments
 (0)