多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] ***** ## 2 optimizer.js ast优化 >[info] import ~~~ ;(导入)基础工具 import { makeMap, isBuiltInTag } from 'shared/util' ~~~ >[info] module ~~~ ;平台保留标签 let isPlatformReservedTag ;解析结果优化 export function optimize (root, options) { ;是否平台保留标签 isPlatformReservedTag = options.isReservedTag || (() => false) ;第一次遍历 markStatic(root) ;第二次遍历 markStaticRoots(root) } ;第一次遍历 标记所有非静态节点 function markStatic (node) { node.static = isStatic(node) if (node.children) { for (let i = 0, l = node.children.length; i < l; i++) { const child = node.children[i] markStatic(child) if (!child.static) { node.static = false } } } } ;第二次遍历 标记所有静态节点 function markStaticRoots (node) { if (node.tag && (node.once || node.static)) { node.staticRoot = true return } if (node.children) { for (let i = 0, l = node.children.length; i < l; i++) { markStaticRoots(node.children[i]) } } } ;静态属性key const isStaticKey = makeMap( 'tag,attrsList,attrsMap,plain,parent,children,' + 'staticAttrs,staticClass' ) ;静态属性检测 function isStatic (node) { return !!(node.text || node.pre || ( !node.expression && // not text with interpolation !node.if && !node.for && // not v-if or v-for or v-else (!node.tag || isPlatformReservedTag(node.tag)) && // not a component !isBuiltInTag(node.tag) && // not a built-in (node.plain || Object.keys(node).every(isStaticKey)) // no dynamic bindings )) } ~~~ >[info] export ~~~ ;(导出)解析结果优化 export function optimize (root, options) { ~~~