惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
SegmentFault 最新的问题
G
Google Developers Blog
H
Help Net Security
月光博客
月光博客
阮一峰的网络日志
阮一峰的网络日志
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Proofpoint News Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
B
Blog RSS Feed
爱范儿
爱范儿
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 三生石上(FineUI控件)
大猫的无限游戏
大猫的无限游戏
人人都是产品经理
人人都是产品经理
GbyAI
GbyAI
D
Docker
Hugging Face - Blog
Hugging Face - Blog
I
InfoQ
博客园 - 司徒正美
Last Week in AI
Last Week in AI
Microsoft Security Blog
Microsoft Security Blog
美团技术团队
Stack Overflow Blog
Stack Overflow Blog
M
MIT News - Artificial intelligence

时间的朋友

Windows 命令行密码重置 Anaconda安装 typescript 注解解读1 Konvajs Shape加载自定义图片 sshpass 使用 why-is-node-running webgl笔记 SharedArrayBuffer is not defined blender 常用快捷键 | 时间的朋友 vue -- v3.4commit提交记录2 vue2 升级vue3报错问题整理 着色器 expressjs 源码 hyper-V arch linux 网络配置 element input数字格式化 three 拼接货架 WebAudio笔记 Windows nginx重启bat脚本 vue -- v3.4commit提交记录 URI malformed vue3 -- Class 对象在组件中使用范例 | 时间的朋友 ruby 安装和升级 element-plus 老版本cascader使用卡死问题 vue3 内置Transition组件 | 时间的朋友 前端memo的实现 | 时间的朋友 Vue -- vue-class-component源码 | 时间的朋友 linux 优化脚本 typescript 装饰器 | 时间的朋友 microbundle 源码 | 时间的朋友 WSL2问题解决WslRegisterDistribution failed with error: 0x800701bc
vue -- compileTemplate原理 | 时间的朋友
2023-02-04 · via 时间的朋友

Published: · LastMod: February 05, 2023 · 536 words

Vue3 compileTemplate 🔗

packages/compiler-sfc/src/compileTemplate.ts

“version”: “3.2.45”

vue3对于sfc文件的编译解读

compileTemplate 🔗

第一步,走的是文件预处理的脚本,类似如果我们在template中使用了pug等模板,会预先通过预处理函数处理过,再传入到主编译函数doCompileTemplate

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22

export function compileTemplate(
  options: SFCTemplateCompileOptions
): SFCTemplateCompileResults {
  const { preprocessLang, preprocessCustomRequire } = options
	// ......
  if (preprocessor) {
    try {
      // 
      return doCompileTemplate({
        ...options,
        // 预处理函数。。。,传入的source已经是处理过后的了
        source: preprocess(options, preprocessor)
      })
    } catch (e: any) {
      // ...
    }
  } else {
    // 没有预处理的,直接走compile函数
    return doCompileTemplate(options)
  }
}

doCompileTemplate 🔗

doCompileTemplate主函数中,主要实现的就是读取template中的内容,通过@vue/compiler-core暴露的compile方法进行编译,最终返回编译过的代码和源码等参数

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

function doCompileTemplate(SFCTemplateCompileOptions) {
	// ....
  let nodeTransforms: NodeTransform[] = []
  if (isObject(transformAssetUrls)) {
    const assetOptions = normalizeOptions(transformAssetUrls)
    nodeTransforms = [
      createAssetUrlTransformWithOptions(assetOptions),
      createSrcsetTransformWithOptions(assetOptions)
    ]
  } else if (transformAssetUrls !== false) {
    nodeTransforms = [transformAssetUrl, transformSrcset]
  }

	// 调用编译内核,具体转换的代码在@vue/compiler-core之中实现
  let { code, ast, preamble, map } = compiler.compile(source, {
    //...
  })

  // ...
	
  return { code, ast, preamble, source, errors, tips, map }
}

transformAssetUrl 🔗

这个方法实现的是转换vdom中的相对路径转换成绝对路径

1
2
3
4
5
// Before
createVNode('img', { src: './logo.png' })
// After
import _imports_0 from './logo.png'
createVNode('img', { src: _imports_0 })