Skip to content

Commit

Permalink
feat: resolve components from <script setup>
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jun 15, 2022
1 parent d4787e5 commit 4b19339
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 17 deletions.
1 change: 1 addition & 0 deletions packages/compiler-sfc/src/compileTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ function actuallyCompile(
filename: options.filename
})
}
finalCompilerOptions.bindings = bindings

const { ast, render, staticRenderFns, tips, errors } = compile(
source,
Expand Down
17 changes: 3 additions & 14 deletions packages/compiler-sfc/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { WarningMessage } from 'types/compiler'
import { CompilerOptions, CompiledResult } from 'types/compiler'
import { SFCDescriptor } from './parseComponent'

export interface StartOfSourceMap {
Expand Down Expand Up @@ -31,20 +31,9 @@ export interface VueTemplateCompiler {
// we'll just shim this much for now - in the future these types
// should come from vue-template-compiler directly, or this package should be
// part of the vue monorepo.
export interface VueTemplateCompilerOptions {
modules?: Object[]
outputSourceRange?: boolean
whitespace?: 'preserve' | 'condense'
directives?: { [key: string]: Function }
}
export type VueTemplateCompilerOptions = CompilerOptions

export interface VueTemplateCompilerResults {
ast: Object | undefined
render: string
staticRenderFns: string[]
errors: (string | WarningMessage)[]
tips: (string | WarningMessage)[]
}
export type VueTemplateCompilerResults = CompiledResult

export const enum BindingTypes {
/**
Expand Down
23 changes: 21 additions & 2 deletions src/compiler/codegen/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { genHandlers } from './events'
import baseDirectives from '../directives/index'
import { camelize, no, extend } from 'shared/util'
import { camelize, no, extend, capitalize } from 'shared/util'
import { baseWarn, pluckModuleFunction } from '../helpers'
import { emptySlotScopeToken } from '../parser/index'
import {
Expand All @@ -13,6 +13,7 @@ import {
ASTText,
CompilerOptions
} from 'types/compiler'
import { BindingMetadata } from 'sfc/types'

type TransformFunction = (el: ASTElement, code: string) => string
type DataGenFunction = (el: ASTElement) => string
Expand Down Expand Up @@ -98,8 +99,19 @@ export function genElement(el: ASTElement, state: CodegenState): string {
data = genData(el, state)
}

let tag: string | undefined
// check if this is a component in <script setup>
const bindings = state.options.bindings
if (bindings && !bindings.__isScriptSetup) {
tag =
checkBindingType(bindings, el.tag) ||
checkBindingType(bindings, camelize(el.tag)) ||
checkBindingType(bindings, capitalize(camelize(el.tag)))
}
if (!tag) tag = `'${el.tag}'`

const children = el.inlineTemplate ? null : genChildren(el, state, true)
code = `_c('${el.tag}'${
code = `_c(${tag}${
data ? `,${data}` : '' // data
}${
children ? `,${children}` : '' // children
Expand All @@ -113,6 +125,13 @@ export function genElement(el: ASTElement, state: CodegenState): string {
}
}

function checkBindingType(bindings: BindingMetadata, key: string) {
const type = bindings[key]
if (type && type.startsWith('setup')) {
return key
}
}

// hoist static sub-trees out
function genStatic(el: ASTElement, state: CodegenState): string {
el.staticProcessed = true
Expand Down
3 changes: 2 additions & 1 deletion src/types/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export type CompilerOptions = {
// for ssr optimization compiler
scopeId?: string

bindingMetadata?: BindingMetadata
// SFC analyzed script bindings from `compileScript()`
bindings?: BindingMetadata
}

export type WarningMessage = {
Expand Down

0 comments on commit 4b19339

Please sign in to comment.