| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- import { SFCDescriptor } from '../parse'
- import {
- NodeTypes,
- SimpleExpressionNode,
- parserOptions,
- walkIdentifiers,
- TemplateChildNode,
- ExpressionNode
- } from '@vue/compiler-dom'
- import { createCache } from '../cache'
- import { camelize, capitalize, isBuiltInDirective } from '@vue/shared'
- /**
- * Check if an import is used in the SFC's template. This is used to determine
- * the properties that should be included in the object returned from setup()
- * when not using inline mode.
- */
- export function isImportUsed(local: string, sfc: SFCDescriptor): boolean {
- return resolveTemplateUsageCheckString(sfc).has(local)
- }
- const templateUsageCheckCache = createCache<Set<string>>()
- function resolveTemplateUsageCheckString(sfc: SFCDescriptor) {
- const { content, ast } = sfc.template!
- const cached = templateUsageCheckCache.get(content)
- if (cached) {
- return cached
- }
- const ids = new Set<string>()
- ast!.children.forEach(walk)
- function walk(node: TemplateChildNode) {
- switch (node.type) {
- case NodeTypes.ELEMENT:
- if (
- !parserOptions.isNativeTag!(node.tag) &&
- !parserOptions.isBuiltInComponent!(node.tag)
- ) {
- ids.add(camelize(node.tag))
- ids.add(capitalize(camelize(node.tag)))
- }
- for (let i = 0; i < node.props.length; i++) {
- const prop = node.props[i]
- if (prop.type === NodeTypes.DIRECTIVE) {
- if (!isBuiltInDirective(prop.name)) {
- ids.add(`v${capitalize(camelize(prop.name))}`)
- }
- // process dynamic directive arguments
- if (prop.arg && !(prop.arg as SimpleExpressionNode).isStatic) {
- extractIdentifiers(ids, prop.arg)
- }
- if (prop.name === 'for') {
- extractIdentifiers(ids, prop.forParseResult!.source)
- } else if (prop.exp) {
- extractIdentifiers(ids, prop.exp)
- }
- }
- if (
- prop.type === NodeTypes.ATTRIBUTE &&
- prop.name === 'ref' &&
- prop.value?.content
- ) {
- ids.add(prop.value.content)
- }
- }
- node.children.forEach(walk)
- break
- case NodeTypes.INTERPOLATION:
- extractIdentifiers(ids, node.content)
- break
- }
- }
- templateUsageCheckCache.set(content, ids)
- return ids
- }
- function extractIdentifiers(ids: Set<string>, node: ExpressionNode) {
- if (node.ast) {
- walkIdentifiers(node.ast, n => ids.add(n.name))
- } else if (node.ast === null) {
- ids.add((node as SimpleExpressionNode).content)
- }
- }
|