🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] ***** ## web-compiler web编译 >[info] import ~~~ ;(导入)扩展,基础编译,web指令,web基础工具 import { extend } from 'shared/util' import { compile as baseCompile } from 'compiler/index' import directives from 'web/compiler/directives/index' import { isReservedTag, isUnaryTag, mustUseProp, getTagNamespace } from 'web/util/index' ~~~ >[info] module ~~~ ;分别存储保留空白,非保留空白 const cache1 = Object.create(null) const cache2 = Object.create(null) ;编译配置项 const baseOptions = { expectHTML: true, directives, isReservedTag, isUnaryTag, mustUseProp, getTagNamespace } ;web编译入口 export function compile (template, options) { options = options ? extend(extend({}, baseOptions), options) : baseOptions return baseCompile(template, options) } ;编译成渲染函数 export function compileToFunctions (template, options = {}) { ;是否保留空白节点 options.preserveWhitespace = options.preserveWhitespace !== false const cache = options.preserveWhitespace ? cache1 : cache2 ;缓存检查 if (cache[template]) { return cache[template] } ;准备编译 const res = {} const compiled = compile(template, options) ;渲染函数获取 res.render = new Function(compiled.render) ;静态渲染函数 const l = compiled.staticRenderFns.length if (l) { res.staticRenderFns = new Array(l) for (let i = 0; i < l; i++) { res.staticRenderFns[i] = new Function(compiled.staticRenderFns[i]) } } ;缓存编译结果 return (cache[template] = res) } ~~~ >[info] export