• vue-template-compiler的作用


    今天构建时需要一个问题: vue-template-compiler 和vue的版本不一致的问题

     解决: 在package.json中把两个包的版本配置一致即可

    那么为什么要版本一致,  vue-template-compiler到底是干嘛的

    从以上图可得知 
    vue-template-compiler该模块可用于将 Vue 2.0 模板预编译为渲染函数(template => ast => render),以避免运行时编译开销和 CSP 限制。大都数场景下,与 vue-loader一起使用,只有在编写具有非常特定需求的构建工具时,才需要单独使用它

    需要注意的是,This package is auto-generated. For pull requests please see src/platforms/web/entry-compiler.js.这个包是自动生成的,请查看 entry-compiler.js 文件。

    其文件路径,vue/src/platforms/web/entry-compiler.js

    1. export { parseComponent } from 'sfc/parser'
    2. export { compile, compileToFunctions } from './compiler/index'
    3. export { ssrCompile, ssrCompileToFunctions } from './server/compiler'
    4. export { generateCodeFrame } from 'compiler/codeframe'

    可得知,vue-template-compiler 的代码是从 vue 源码中抽离的!接着,我们对比一下 vue-template-compiler 和 vue 关于编译的 API。发现对于 compile 等函数是一致,只是 vue-template-compiler 开放的参数和方法更多。因此,vuevue-template-compiler 的版本必须一致(同一份源码)!

    vue-template-compiler的API

    1. const compiler = require('vue-template-compiler')
    2. const result = compiler.compile(
    3. {
    4. ast: {
    5. type: 1,
    6. tag: 'div',
    7. attrsList: [ [Object] ],
    8. attrsMap: { id: 'test' },
    9. rawAttrsMap: {},
    10. parent: undefined,
    11. children: [ [Object], [Object], [Object] ],
    12. plain: false,
    13. attrs: [ [Object] ],
    14. static: false,
    15. staticRoot: false
    16. },
    17. render: `with(this){return _c('div',{attrs:{"id":"test"}},[
    18. _m(0), // 上述提到的静态子树,索引为0 This is my vue render test
    19. _v(" "), // 空白节点 之间的换行内容
    20. _c('p',[_v("my name is "+_s(myName))]) // my name is {{myName}}
    21. ])}`,
    22. staticRenderFns: [
    23. `with(this){return _c('div',[_c('p',[_v("This is my vue render test")])])}`
    24. ],
    25. errors: [],
    26. tips: []
    27. }
    28. )
    29. console.log(result)

    vue 关于编译的 API

    1. {
    2. template: {
    3. type: 'template',
    4. content: '\n{{ msg }}\n',
    5. start: 10,
    6. attrs: {},
    7. end: 54
    8. },
    9. script: {
    10. type: 'script',
    11. content: '\n' +
    12. 'export default {\n' +
    13. ' data () {\n' +
    14. ' return {\n' +
    15. " msg: 'Hello world!'\n" +
    16. ' }\n' +
    17. ' }\n' +
    18. '}\n',
    19. start: 77,
    20. attrs: {},
    21. end: 174
    22. },
    23. styles: [
    24. {
    25. type: 'style',
    26. content: '\n.example {\n color: red;\n}\n',
    27. start: 194,
    28. attrs: {},
    29. end: 236
    30. }
    31. ],
    32. customBlocks: [
    33. {
    34. type: 'custom1',
    35. content: '自定义块',
    36. start: 257,
    37. attrs: {},
    38. end: 261
    39. }
    40. ],
    41. errors: []
    42. }

     vue在渲染阶段会把模板编译为AST,然后根据AST生成render函数,底层通过调用render函数会生成VNode创建虚拟DOM。

  • 相关阅读:
    猿创征文|[云原生] docker查看日志用法笔记
    期货-股票交易规则
    学Python的漫画漫步进阶 -- 第十五步.访问数据库
    9.7-一定要开始学了
    J9数字论:一文看懂私有链与联盟链
    Kyligence李栋:从数据湖到指标中台,提升数据分析ROI
    git常用命令和参数有哪些?【git看这一篇就够了】
    ssm校园拼车服务系统毕业设计源码211633
    手动实现简单限流函数
    今天博客访问量达到10W了,纪念一下
  • 原文地址:https://blog.csdn.net/qq_43340606/article/details/126173986