Skip to content

Commit 0a2a5e1

Browse files
committed
fix(plugin-vue): properly handle in-template TS syntax + tests
1 parent 5acc277 commit 0a2a5e1

File tree

6 files changed

+26
-21
lines changed

6 files changed

+26
-21
lines changed

packages/playground/ssr-vue/src/components/ImportType.vue

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
</template>
44

55
<script setup lang="ts">
6-
import type { Foo } from 'dep-import-type/deep'
7-
6+
import { Foo } from 'dep-import-type/deep'
87
const msg: Foo = {}
98
</script>

packages/playground/vue/CustomBlock.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
<p class="custom-block">{{ t('hello') }}</p>
44
</template>
55

6-
<script lang="ts">
6+
<script>
77
import { getCurrentInstance } from 'vue'
88
99
function useI18n(locale = 'en') {
1010
const instance = getCurrentInstance()
11-
const resources = (instance.type as any).i18n || { en: {} }
11+
const resources = instance.type.i18n || { en: {} }
1212
function t(key) {
1313
const res = resources[locale] || {}
1414
return res[key]

packages/playground/vue/CustomElement.ce.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</template>
77

88
<script setup>
9-
import { defineProps, reactive, onBeforeMount } from 'vue'
9+
import { reactive, onBeforeMount } from 'vue'
1010
1111
defineProps({
1212
label: String

packages/playground/vue/Main.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template>
22
<div class="comments"><!--hello--></div>
33
<h1>Vue SFCs</h1>
4-
<pre>{{ time }}</pre>
4+
<pre>{{ time as string }}</pre>
55
<div class="hmr-block">
66
<Hmr />
77
</div>

packages/plugin-vue/src/main.ts

+19-13
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { transformTemplateInMain } from './template'
1414
import { isOnlyTemplateChanged, isEqualBlock } from './handleHotUpdate'
1515
import { RawSourceMap, SourceMapConsumer, SourceMapGenerator } from 'source-map'
1616
import { createRollupError } from './utils/error'
17+
import { transformWithEsbuild } from 'vite'
1718

1819
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
1920
export async function transformMain(
@@ -191,8 +192,24 @@ export async function transformMain(
191192

192193
output.push(`export default _sfc_main`)
193194

195+
// handle TS transpilation
196+
let resolvedCode = output.join('\n')
197+
if (
198+
descriptor.script?.lang === 'ts' ||
199+
descriptor.scriptSetup?.lang === 'ts'
200+
) {
201+
const { code, map } = await transformWithEsbuild(
202+
resolvedCode,
203+
filename,
204+
{ loader: 'ts' },
205+
resolvedMap
206+
)
207+
resolvedCode = code
208+
resolvedMap = resolvedMap ? (map as any) : resolvedMap
209+
}
210+
194211
return {
195-
code: output.join('\n'),
212+
code: resolvedCode,
196213
map: resolvedMap || {
197214
mappings: ''
198215
}
@@ -255,19 +272,8 @@ async function genScriptCode(
255272
(!script.lang || (script.lang === 'ts' && options.devServer)) &&
256273
!script.src
257274
) {
258-
scriptCode = script.content
275+
scriptCode = rewriteDefault(script.content, '_sfc_main')
259276
map = script.map
260-
if (script.lang === 'ts') {
261-
const result = await options.devServer!.transformWithEsbuild(
262-
scriptCode,
263-
descriptor.filename,
264-
{ loader: 'ts' },
265-
map
266-
)
267-
scriptCode = result.code
268-
map = result.map
269-
}
270-
scriptCode = rewriteDefault(scriptCode, `_sfc_main`)
271277
} else {
272278
if (script.src) {
273279
await linkSrcToDescriptor(script.src, descriptor, pluginContext)

packages/plugin-vue/src/template.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { getResolvedScript } from './script'
1313
import { createRollupError } from './utils/error'
1414

1515
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
16-
export function transformTemplateAsModule(
16+
export async function transformTemplateAsModule(
1717
code: string,
1818
descriptor: SFCDescriptor,
1919
options: ResolvedOptions,
@@ -36,7 +36,7 @@ export function transformTemplateAsModule(
3636

3737
return {
3838
code: returnCode,
39-
map: result.map as any
39+
map: result.map
4040
}
4141
}
4242

0 commit comments

Comments
 (0)