1- import { parse as parseDOM } from '@vue/compiler-dom'
21import { parse as parseSFC } from '@vue/compiler-sfc'
32import { transform } from 'esbuild'
43
@@ -16,22 +15,26 @@ defineProps<{
1615</script>
1716`
1817
19- const transpiledTemplate = await transpileVueTemplate (
20- src ,
21- parseDOM ( src , { parseMode : 'base' } ) ,
22- 0 ,
18+ const sfc = parseSFC ( src , {
19+ filename : 'test.vue' ,
20+ ignoreEmpty : true ,
21+ } )
22+
23+ // transpile template block
24+ const templateBlockContents = await transpileVueTemplate (
25+ sfc . descriptor . template . content ,
26+ sfc . descriptor . template . ast ,
27+ sfc . descriptor . template . loc . start . offset ,
2328 async ( code ) => {
2429 const res = await transform ( code , { loader : 'ts' , target : 'esnext' } )
2530 return res . code
2631 } ,
2732)
33+ console . log ( `transpiled <template> block:` )
34+ console . log ( `\`\`\`\n<template>${ templateBlockContents } </template>\n\`\`\`\n` )
2835
29- console . log ( transpiledTemplate )
30-
31- const sfc = parseSFC ( transpiledTemplate , {
32- filename : 'test.vue' ,
33- ignoreEmpty : true ,
34- } )
35-
36+ // transpile script block
37+ // notice: it is still in typescript, you need to transpile it to javascript later
3638const { content : scriptBlockContents } = await preTranspileScriptSetup ( sfc . descriptor , 'test.vue' )
37- console . log ( scriptBlockContents )
39+ console . log ( `transpiled <script setup> block:` )
40+ console . log ( `\`\`\`\n<script setup lang="ts">${ scriptBlockContents } </script>\n\`\`\`\n` )
0 commit comments