fix(compiler-sfc): do not resolve assets from setup bindings
when not using script setup fix #3270, fix #3275
This commit is contained in:
parent
4d9f9fdf9d
commit
f5827fdf78
@ -2,7 +2,8 @@ import {
|
|||||||
CompilerOptions,
|
CompilerOptions,
|
||||||
baseParse as parse,
|
baseParse as parse,
|
||||||
transform,
|
transform,
|
||||||
ErrorCodes
|
ErrorCodes,
|
||||||
|
BindingTypes
|
||||||
} from '../../src'
|
} from '../../src'
|
||||||
import {
|
import {
|
||||||
RESOLVE_COMPONENT,
|
RESOLVE_COMPONENT,
|
||||||
@ -78,6 +79,28 @@ describe('compiler: element transform', () => {
|
|||||||
expect(root.components).toContain(`Example__self`)
|
expect(root.components).toContain(`Example__self`)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('resolve component from setup bindings', () => {
|
||||||
|
const { root, node } = parseWithElementTransform(`<Example/>`, {
|
||||||
|
bindingMetadata: {
|
||||||
|
Example: BindingTypes.SETUP_MAYBE_REF
|
||||||
|
}
|
||||||
|
})
|
||||||
|
expect(root.helpers).not.toContain(RESOLVE_COMPONENT)
|
||||||
|
expect(node.tag).toBe(`$setup["Example"]`)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('do not resolve component from non-script-setup bindings', () => {
|
||||||
|
const bindingMetadata = {
|
||||||
|
Example: BindingTypes.SETUP_MAYBE_REF
|
||||||
|
}
|
||||||
|
Object.defineProperty(bindingMetadata, '__isScriptSetup', { value: false })
|
||||||
|
const { root } = parseWithElementTransform(`<Example/>`, {
|
||||||
|
bindingMetadata
|
||||||
|
})
|
||||||
|
expect(root.helpers).toContain(RESOLVE_COMPONENT)
|
||||||
|
expect(root.components).toContain(`Example`)
|
||||||
|
})
|
||||||
|
|
||||||
test('static props', () => {
|
test('static props', () => {
|
||||||
const { node } = parseWithElementTransform(`<div id="foo" class="bar" />`)
|
const { node } = parseWithElementTransform(`<div id="foo" class="bar" />`)
|
||||||
expect(node).toMatchObject({
|
expect(node).toMatchObject({
|
||||||
|
@ -94,8 +94,10 @@ export const enum BindingTypes {
|
|||||||
OPTIONS = 'options'
|
OPTIONS = 'options'
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface BindingMetadata {
|
export type BindingMetadata = {
|
||||||
[key: string]: BindingTypes | undefined
|
[key: string]: BindingTypes | undefined
|
||||||
|
} & {
|
||||||
|
__isScriptSetup?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
interface SharedTransformCodegenOptions {
|
interface SharedTransformCodegenOptions {
|
||||||
|
@ -286,7 +286,7 @@ export function resolveComponentType(
|
|||||||
|
|
||||||
function resolveSetupReference(name: string, context: TransformContext) {
|
function resolveSetupReference(name: string, context: TransformContext) {
|
||||||
const bindings = context.bindingMetadata
|
const bindings = context.bindingMetadata
|
||||||
if (!bindings) {
|
if (!bindings || bindings.__isScriptSetup === false) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -962,6 +962,7 @@ describe('SFC analyze <script> bindings', () => {
|
|||||||
|
|
||||||
expect(scriptAst).toBeDefined()
|
expect(scriptAst).toBeDefined()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('recognizes props array declaration', () => {
|
it('recognizes props array declaration', () => {
|
||||||
const { bindings } = compile(`
|
const { bindings } = compile(`
|
||||||
<script>
|
<script>
|
||||||
@ -974,6 +975,7 @@ describe('SFC analyze <script> bindings', () => {
|
|||||||
foo: BindingTypes.PROPS,
|
foo: BindingTypes.PROPS,
|
||||||
bar: BindingTypes.PROPS
|
bar: BindingTypes.PROPS
|
||||||
})
|
})
|
||||||
|
expect(bindings!.__isScriptSetup).toBe(false)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('recognizes props object declaration', () => {
|
it('recognizes props object declaration', () => {
|
||||||
@ -997,6 +999,7 @@ describe('SFC analyze <script> bindings', () => {
|
|||||||
baz: BindingTypes.PROPS,
|
baz: BindingTypes.PROPS,
|
||||||
qux: BindingTypes.PROPS
|
qux: BindingTypes.PROPS
|
||||||
})
|
})
|
||||||
|
expect(bindings!.__isScriptSetup).toBe(false)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('recognizes setup return', () => {
|
it('recognizes setup return', () => {
|
||||||
@ -1017,6 +1020,7 @@ describe('SFC analyze <script> bindings', () => {
|
|||||||
foo: BindingTypes.SETUP_MAYBE_REF,
|
foo: BindingTypes.SETUP_MAYBE_REF,
|
||||||
bar: BindingTypes.SETUP_MAYBE_REF
|
bar: BindingTypes.SETUP_MAYBE_REF
|
||||||
})
|
})
|
||||||
|
expect(bindings!.__isScriptSetup).toBe(false)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('recognizes async setup return', () => {
|
it('recognizes async setup return', () => {
|
||||||
@ -1037,6 +1041,7 @@ describe('SFC analyze <script> bindings', () => {
|
|||||||
foo: BindingTypes.SETUP_MAYBE_REF,
|
foo: BindingTypes.SETUP_MAYBE_REF,
|
||||||
bar: BindingTypes.SETUP_MAYBE_REF
|
bar: BindingTypes.SETUP_MAYBE_REF
|
||||||
})
|
})
|
||||||
|
expect(bindings!.__isScriptSetup).toBe(false)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('recognizes data return', () => {
|
it('recognizes data return', () => {
|
||||||
|
@ -1586,6 +1586,12 @@ function analyzeScriptBindings(ast: Statement[]): BindingMetadata {
|
|||||||
|
|
||||||
function analyzeBindingsFromOptions(node: ObjectExpression): BindingMetadata {
|
function analyzeBindingsFromOptions(node: ObjectExpression): BindingMetadata {
|
||||||
const bindings: BindingMetadata = {}
|
const bindings: BindingMetadata = {}
|
||||||
|
// #3270, #3275
|
||||||
|
// mark non-script-setup so we don't resolve components/directives from these
|
||||||
|
Object.defineProperty(bindings, '__isScriptSetup', {
|
||||||
|
enumerable: false,
|
||||||
|
value: false
|
||||||
|
})
|
||||||
for (const property of node.properties) {
|
for (const property of node.properties) {
|
||||||
if (
|
if (
|
||||||
property.type === 'ObjectProperty' &&
|
property.type === 'ObjectProperty' &&
|
||||||
|
Loading…
x
Reference in New Issue
Block a user