import{createApp,defineComponent}from'chibivue'const App =defineComponent({template:` <!-- this is header. --> <header>header</header> <!-- this is main. main content is here! --> <main>main</main> <!-- this is footer --> <footer>footer</footer>`,})const app =createApp(App)app.mount('#app')
实现注释功能
目标开发者接口
这个功能不需要特别说明。
AST和解析器实现
关于如何实现注释,乍看之下似乎可以在解析时直接忽略它们。
但是,Vue的注释会被原样输出到HTML中。
这意味着我们需要渲染注释,因此需要在VNode中表示它们,编译器也需要生成相应的代码,
并且还需要实现创建注释节点的操作。
首先让我们实现AST和解析器。
AST
解析器
对于错误处理,暂时使用throw。
生成代码
在runtime-core中添加表示Comment的VNode。
实现createCommentVNode函数并作为helper公开。
在codegen中生成调用createCommentVNode的代码。
渲染实现
接下来实现renderer部分。
像往常一样,在patch函数中添加处理Comment的分支,在mount时创建注释。
对于patch过程,由于注释是静态的,所以不需要特别处理(代码中只是直接赋值)。
到此为止,注释功能应该已经实现了。让我们来测试一下实际效果!
完整源代码:GitHub