/** * 这里我们需要关心的小程序种类有两类: * 1. 模板递归: * - 支持:tmpl0 套 tmpl0 * - 不支持:这就使得我们必须生成多级的模板,tmpl0 套 tmpl1,tmpl1 套 tmpl2…… * 直到超过阈值 N (N = config.miniapp.baseLevel) tmplN 会套组件 comp,组件 comp 重新再套 tmpl0。 * 2. 小程序脚本语言(wxs, sjs, etc...): * - 支持:可以在模板使用函数缩减模板大小或提高性能(存疑),例如判断一个值是不是假值(falsy value)。 * 将来或许会把数据序列化^1 的操作也放到小程序脚本语言里。 * - 不支持:使用纯 *xml 语法 * * ^1: packages/taro-runtime/src/hydrate.ts */ import { focusComponents, internalComponents, nestElements, singleQuote, voidElements } from './components' import { COMPILE_MODE_IDENTIFIER_PREFIX } from './constants' import { isBooleanStringLiteral, isFunction, isNumber, isObjectStringLiteral, isString } from './is' import { Shortcuts } from './shortcuts' import { capitalize, getComponentsAlias, hasOwn, indent, toCamelCase, toDashed, toKebabCase } from './utils' interface Component { nodeName: string nodeAlias: string attributes: Attributes } interface Components { [key: string]: Record } interface ComponentConfig { includes: Set exclude: Set thirdPartyComponents: Map> includeAll: boolean /** 当为 true 时,跳过最后一层 container template 中对 的引用,超出 baseLevel 的节点将被静默截断 */ skipRecursiveComponent?: boolean } export interface IAdapter { if: string else: string elseif: string for: string forItem: string forIndex: string key: string xs?: string type: string } export type Attributes = Record export const styles = { style: `i.${Shortcuts.Style}`, class: `i.${Shortcuts.Class}` } export const events = { bindtap: 'eh' } const weixinAdapter: IAdapter = { if: 'wx:if', else: 'wx:else', elseif: 'wx:elif', for: 'wx:for', forItem: 'wx:for-item', forIndex: 'wx:for-index', key: 'wx:key', xs: 'wxs', type: 'weapp' } export class BaseTemplate { protected _baseLevel = 0 protected _isUseXS = true protected _isUseCompileMode = false protected exportExpr = 'module.exports =' protected isSupportRecursive: boolean protected miniComponents: Components protected componentConfig: ComponentConfig | undefined protected thirdPartyPatcher: Record> = {} protected modifyCompProps?: (compName: string, target: Record) => Record protected modifyLoopBody?: (child: string, nodeName: string) => string protected modifyLoopContainer?: (children: string, nodeName: string) => string protected modifyTemplateResult?: (res: string, nodeName: string, level: number, children: string) => string protected modifyThirdPartyLoopBody?: (child: string, nodeName: string) => string public supportXS = false public isXMLSupportRecursiveReference = true public Adapter = weixinAdapter /** 组件列表 */ public internalComponents = internalComponents /** 可以 focus 聚焦的组件 */ public focusComponents: Set = focusComponents /** 不需要渲染子节点的元素 */ public voidElements: Set = voidElements /** 可以递归调用自身的组件 */ public nestElements: Map = nestElements public componentsAlias set baseLevel (lv) { this._baseLevel = lv } get baseLevel () { return this._baseLevel } set isUseCompileMode (isUse) { this._isUseCompileMode = isUse } get isUseCompileMode () { return this._isUseCompileMode } set isUseXS (isUse) { this._isUseXS = this.supportXS && isUse } get isUseXS () { return this._isUseXS } private buildAttribute (attrs: Attributes, nodeName: string): string { return Object.keys(attrs) .map(k => `${k}="${k.startsWith('bind') || k.startsWith('on') || k.startsWith('catch') ? attrs[k] : `{${this.getAttrValue(attrs[k], k, nodeName)}}`}" `) .join('') } protected replacePropName (name: string, value: string, _componentName?: string, _componentAlias?) { if (value === 'eh') return name.toLowerCase() return name } public createMiniComponents (components: Components) { const result: Components = Object.create(null) const skipProps = ['$duplicateFromComponent'] for (const key in components) { if (hasOwn(components, key)) { let component = components[key] const compName = toDashed(key) const newComp: Record = Object.create(null) const componentAlias = this.componentsAlias[compName] if (isFunction(this.modifyCompProps)) { component = this.modifyCompProps(compName, component) } for (let prop in component) { if (hasOwn(component, prop) && !skipProps.includes(prop)) { const propInCamelCase = toCamelCase(prop) const propAlias = componentAlias[propInCamelCase] || propInCamelCase let propValue = component[prop] if (prop.startsWith('bind') || propValue === 'eh') { propValue = 'eh' } else if (propValue === '') { propValue = `i.${propAlias}` } else if (isBooleanStringLiteral(propValue) || isNumber(+propValue)) { propValue = this.isUseXS ? `xs.b(i.${propAlias},${propValue})` : `i.${propAlias}===undefined?${propValue}:i.${propAlias}` } else if (isObjectStringLiteral(propValue)) { propValue = this.isUseXS ? `xs.d(i.${propAlias})` : `i.${propAlias}===undefined?${propValue}:i.${propAlias}` } else { propValue = `i.${propAlias}||${propValue || singleQuote('')}` } prop = this.replacePropName(prop, propValue, compName, componentAlias) newComp[prop] = propValue } } if (compName !== 'block') { Object.assign(newComp, styles, this.getEvents()) } if (compName === 'swiper-item') { delete newComp.style } if (compName === 'view') { const reg = /^(bind|on)(touchmove|TouchMove)$/ const comp = { ...newComp } Object.keys(comp).forEach(originKey => { if (!reg.test(originKey)) return const key = originKey.replace(reg, 'catch$2') comp[key] = comp[originKey] delete comp[originKey] }) result['catch-view'] = comp } if (compName === 'view' || compName === 'text' || compName === 'image') { const comp: Record = {} Object.keys(newComp).forEach(key => { const value = newComp[key] if (value !== 'eh') comp[key] = value }) result[`static-${compName}`] = comp if (compName === 'view') { result['pure-view'] = { style: comp.style, class: comp.class } result['click-view'] = { style: comp.style, class: comp.class, ...this.getClickEvent() } } } if (compName === 'slot' || compName === 'slot-view') { result[compName] = { slot: newComp?.name, ...styles } } else if (compName === 'native-slot') { result[compName] = { name: newComp?.name, } } else if (compName === 'list-builder') { result[compName] = { ...newComp, list: 'i.cn', } } else { result[compName] = newComp } } } return result } protected buildBaseTemplate () { const Adapter = this.Adapter const data = !this.isSupportRecursive && this.isUseXS ? `${this.dataKeymap(`i:item,c:1,l:xs.f('',item.${Shortcuts.NodeName})`)}` : this.isSupportRecursive ? this.dataKeymap('i:item') : this.dataKeymap('i:item,c:1') const xs = this.isUseXS ? (this.isSupportRecursive ? `xs.a(0, item.${Shortcuts.NodeName})` : `xs.a(0, item.${Shortcuts.NodeName}, '')`) : `'tmpl_0_' + item.${Shortcuts.NodeName}` return `${this.buildXsImportTemplate()}