template js 定义变量_Vue源码全面解析二十六 Template模板解析前后(cache缓存模板)

de9d1f19baf149d5a951216c91da9f23

我们打开“src/compiler/to-function.js”文件,代码如下:

export function createCompileToFunctionFn (compile: Function): Function {  const cache = Object.create(null)  return function compileToFunctions (    template: string,    options?: CompilerOptions,    vm?: Component  ): CompiledFunctionResult {    options = extend({}, options)    const warn = options.warn || baseWarn    delete options.warn        // ...省略部分代码        // check cache    const key = options.delimiters ? String(options.delimiters) + template : template    if (cache[key]) {      return cache[key]    }    // compile    const compiled = compile(template, options)    // check compilation errors/tips    if (process.env.NODE_ENV !== 'production') {      if (compiled.errors && compiled.errors.length) {        if (options.outputSourceRange) {          compiled.errors.forEach(e => {            warn(  `Error compiling template:${e.msg}` +generateCodeFrame(template, e.start, e.end),vm)          })        } else {          warn(`Error compiling template:${template}` +compiled.errors.map(e => `- ${e}`).join('') + '',vm)        }      }      if (compiled.tips && compiled.tips.length) {        if (options.outputSourceRange) {          compiled.tips.forEach(e => tip(e.msg, vm))        } else {          compiled.tips.forEach(msg => tip(msg, vm))        }      }    }    // turn code into functions    const res = {}    const fnGenErrors = []    res.render = createFunction(compiled.render, fnGenErrors)    res.staticRenderFns = compiled.staticRenderFns.map(code => {      return createFunction(code, fnGenErrors)    })    if (process.env.NODE_ENV !== 'production') {      if ((!compiled.errors || !compiled.errors.length) && fnGenErrors.length) {        warn(`Failed to generate render function:` +fnGenErrors.map(({ err, code }) => `${err.toString()} in${code}`).join(''),vm)      }    }    return (cache[key] = res)  }}

该函数其实主要是3个过程:

1、是否存在编译缓存

2、编译模板(compile)

3、返回编译结果,并缓存

接下来,我们一起拆分一下函数:

    const key = options.delimiters ? String(options.delimiters) + template : template    if (cache[key]) {      return cache[key]    }

定义 "key" 变量,如果存在自定义分隔符(delimiters)那就是 “delimiters” 和 “template”组合,否则就是 "template"。

然后就是判断是否有缓存,如果存在没救直接返回缓存。

// compileconst compiled = compile(template, options)

模板编译,核心的模板解析函数入口。

  const res = {}  const fnGenErrors = []  res.render = createFunction(compiled.render, fnGenErrors)  res.staticRenderFns = compiled.staticRenderFns.map(code => {    return createFunction(code, fnGenErrors)  })  if (process.env.NODE_ENV !== 'production') {    if ((!compiled.errors || !compiled.errors.length) && fnGenErrors.length) {      warn(`Failed to generate render function:` +fnGenErrors.map(({ err, code }) => `${err.toString()} in${code}`).join(''),vm)    }  }  return (cache[key] = res)

将编译过后的 “render” 属性代码转换为函数,最后编译结果,并缓存。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值