@@ -20,7 +20,6 @@ pnpm install vue-sfc-transformer
20
20
```
21
21
22
22
``` js
23
- import { parse as parseDOM } from ' @vue/compiler-dom'
24
23
import { parse as parseSFC } from ' @vue/compiler-sfc'
25
24
import { transform } from ' esbuild'
26
25
@@ -38,40 +37,28 @@ defineProps<{
38
37
</script>
39
38
`
40
39
41
- const transpiledTemplate = await transpileVueTemplate (
42
- src,
43
- parseDOM (src, { parseMode: ' base' }),
44
- 0 ,
40
+ const sfc = parseSFC (src, {
41
+ filename: ' test.vue' ,
42
+ ignoreEmpty: true ,
43
+ })
44
+
45
+ // transpile template block
46
+ const templateBlockContents = await transpileVueTemplate (
47
+ sfc .descriptor .template .content ,
48
+ sfc .descriptor .template .ast ,
49
+ sfc .descriptor .template .loc .start .offset ,
45
50
async (code ) => {
46
51
const res = await transform (code, { loader: ' ts' , target: ' esnext' })
47
52
return res .code
48
53
},
49
54
)
50
-
51
- console .log (transpiledTemplate)
52
- // <template>
53
- // <div v-if="test" />
54
- // </template>
55
- //
56
- // <script setup lang="ts">
57
- // defineProps<{
58
- // test?: string
59
- // }>()
60
- // </script>
61
-
62
- const sfc = parseSFC (transpiledTemplate, {
63
- filename: ' test.vue' ,
64
- ignoreEmpty: true ,
65
- })
55
+ console .log (templateBlockContents)
56
+ // <div v-if="test" />
66
57
67
58
const { content: scriptBlockContents } = await preTranspileScriptSetup (sfc .descriptor , ' test.vue' )
68
59
console .log (scriptBlockContents)
69
-
70
60
// defineProps({
71
- // test: {
72
- // type: String,
73
- // required: false
74
- // }
61
+ // test: { type: String, required: false }
75
62
// })
76
63
```
77
64
@@ -81,6 +68,8 @@ If you are using `mkdist`, `vue-sfc-transformer` exports a loader you can use:
81
68
import { vueLoader } from ' vue-sfc-transformer/mkdist'
82
69
```
83
70
71
+ > ` mkdist ` will automatically use the loader from ` vue-sfc-transformer ` when you pass ` vue ` to the ` loaders ` option and have this package installed.
72
+
84
73
## 💻 Development
85
74
86
75
- Clone this repository
0 commit comments