wip: fix component resolution check

This commit is contained in:
Evan You 2022-06-15 21:38:05 +08:00
parent aa2b1f4d93
commit bd8409bc98
3 changed files with 25 additions and 9 deletions

View File

@ -39,6 +39,8 @@ export function prefixIdentifiers(
plugins
})
const isScriptSetup = bindings && bindings.__isScriptSetup !== false
walkIdentifiers(
ast,
ident => {
@ -47,7 +49,7 @@ export function prefixIdentifiers(
return
}
if (!bindings) {
if (!isScriptSetup) {
s.prependRight(ident.start!, '_vm.')
return
}
@ -62,7 +64,7 @@ export function prefixIdentifiers(
s.prependRight(
node.start!,
`var _vm=this,_c=_vm._self._c${
bindings ? `,_setup=_vm._setupProxy;` : `;`
isScriptSetup ? `,_setup=_vm._setupProxy;` : `;`
}`
)
}

View File

@ -102,7 +102,7 @@ export function genElement(el: ASTElement, state: CodegenState): string {
let tag: string | undefined
// check if this is a component in <script setup>
const bindings = state.options.bindings
if (bindings && !bindings.__isScriptSetup) {
if (bindings && bindings.__isScriptSetup !== false) {
tag =
checkBindingType(bindings, el.tag) ||
checkBindingType(bindings, camelize(el.tag)) ||

View File

@ -4,11 +4,12 @@ import { generate } from 'compiler/codegen'
import { isObject, isFunction, extend } from 'shared/util'
import { isReservedTag } from 'web/util/index'
import { baseOptions } from 'web/compiler/options'
import { BindingTypes } from '../../../../packages/compiler-sfc/src/types'
function assertCodegen(template, generatedCode, ...args) {
let staticRenderFnCodes = []
let staticRenderFnCodes: string[] = []
let generateOptions = baseOptions
let proc = null
let proc: Function | null = null
let len = args.length
while (len--) {
const arg = args[len]
@ -28,7 +29,6 @@ function assertCodegen(template, generatedCode, ...args) {
expect(res.staticRenderFns).toEqual(staticRenderFnCodes)
}
/* eslint-disable quotes */
describe('codegen', () => {
it('generate directive', () => {
assertCodegen(
@ -624,7 +624,7 @@ describe('codegen', () => {
expect(
'Inline-template components must have exactly one child element.'
).toHaveBeenWarned()
expect(console.error.mock.calls.length).toBe(3)
expect((console.error as any).mock.calls.length).toBe(3)
})
it('generate static trees inside v-for', () => {
@ -689,7 +689,7 @@ describe('codegen', () => {
})
it('not specified ast type', () => {
const res = generate(null, baseOptions)
const res = generate(undefined, baseOptions)
expect(res.render).toBe(`with(this){return _c("div")}`)
expect(res.staticRenderFns).toEqual([])
})
@ -709,5 +709,19 @@ describe('codegen', () => {
`with(this){return _c('div',[(ok)?_l((1),function(i){return _c('foo',{key:i})}):_e()],2)}`
)
})
it('component with bindings ', () => {
const ast = parse(`<div><Foo/><foo-bar></foo-bar></div>`, baseOptions)
optimize(ast, baseOptions)
const res = generate(ast, {
...baseOptions,
bindings: {
Foo: BindingTypes.SETUP_CONST,
FooBar: BindingTypes.SETUP_CONST
}
})
expect(res.render).toMatchInlineSnapshot(
'"with(this){return _c(\'div\',[_c(Foo),_c(FooBar)],1)}"'
)
})
})
/* eslint-enable quotes */