1
- import { parse as parseDOM } from '@vue/compiler-dom'
2
1
import { parse as parseSFC } from '@vue/compiler-sfc'
3
2
import { transform } from 'esbuild'
4
3
@@ -16,22 +15,26 @@ defineProps<{
16
15
</script>
17
16
`
18
17
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 ,
23
28
async ( code ) => {
24
29
const res = await transform ( code , { loader : 'ts' , target : 'esnext' } )
25
30
return res . code
26
31
} ,
27
32
)
33
+ console . log ( `transpiled <template> block:` )
34
+ console . log ( `\`\`\`\n<template>${ templateBlockContents } </template>\n\`\`\`\n` )
28
35
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
36
38
const { 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