Skip to content

Commit a1e9881

Browse files
committed
feat: support ts in template expressions
1 parent b3f20eb commit a1e9881

File tree

2 files changed

+40
-19
lines changed

2 files changed

+40
-19
lines changed

src/transform.ts

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ export async function compileFile(
7171
const scriptLang =
7272
(descriptor.script && descriptor.script.lang) ||
7373
(descriptor.scriptSetup && descriptor.scriptSetup.lang)
74-
if (scriptLang && scriptLang !== 'ts') {
74+
const isTS = scriptLang === 'ts'
75+
if (scriptLang && !isTS) {
7576
store.state.errors = [`Only lang="ts" is supported for <script> blocks.`]
7677
return
7778
}
@@ -85,7 +86,13 @@ export async function compileFile(
8586
ssrCode += code
8687
}
8788

88-
const clientScriptResult = await doCompileScript(store, descriptor, id, false)
89+
const clientScriptResult = await doCompileScript(
90+
store,
91+
descriptor,
92+
id,
93+
false,
94+
isTS
95+
)
8996
if (!clientScriptResult) {
9097
return
9198
}
@@ -95,7 +102,13 @@ export async function compileFile(
95102
// script ssr only needs to be performed if using <script setup> where
96103
// the render fn is inlined.
97104
if (descriptor.scriptSetup) {
98-
const ssrScriptResult = await doCompileScript(store, descriptor, id, true)
105+
const ssrScriptResult = await doCompileScript(
106+
store,
107+
descriptor,
108+
id,
109+
true,
110+
isTS
111+
)
99112
if (ssrScriptResult) {
100113
ssrCode += ssrScriptResult[0]
101114
} else {
@@ -114,7 +127,8 @@ export async function compileFile(
114127
descriptor,
115128
id,
116129
bindings,
117-
false
130+
false,
131+
isTS
118132
)
119133
if (!clientTemplateResult) {
120134
return
@@ -126,7 +140,8 @@ export async function compileFile(
126140
descriptor,
127141
id,
128142
bindings,
129-
true
143+
true,
144+
isTS
130145
)
131146
if (ssrTemplateResult) {
132147
// ssr compile failure is fine
@@ -193,7 +208,8 @@ async function doCompileScript(
193208
store: ReplStore,
194209
descriptor: SFCDescriptor,
195210
id: string,
196-
ssr: boolean
211+
ssr: boolean,
212+
isTS: boolean
197213
): Promise<[string, BindingMetadata | undefined] | undefined> {
198214
if (descriptor.script || descriptor.scriptSetup) {
199215
try {
@@ -203,7 +219,10 @@ async function doCompileScript(
203219
inlineTemplate: true,
204220
templateOptions: {
205221
ssr,
206-
ssrCssVars: descriptor.cssVars
222+
ssrCssVars: descriptor.cssVars,
223+
compilerOptions: {
224+
expressionPlugins: isTS ? ['typescript'] : undefined
225+
}
207226
}
208227
})
209228
let code = ''
@@ -237,7 +256,8 @@ function doCompileTemplate(
237256
descriptor: SFCDescriptor,
238257
id: string,
239258
bindingMetadata: BindingMetadata | undefined,
240-
ssr: boolean
259+
ssr: boolean,
260+
isTS: boolean
241261
) {
242262
const templateResult = store.compiler.compileTemplate({
243263
source: descriptor.template!.content,
@@ -249,7 +269,8 @@ function doCompileTemplate(
249269
ssrCssVars: descriptor.cssVars,
250270
isProd: false,
251271
compilerOptions: {
252-
bindingMetadata
272+
bindingMetadata,
273+
expressionPlugins: isTS ? ['typescript'] : undefined
253274
}
254275
})
255276
if (templateResult.errors.length) {

test/main.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ const App = {
1414
watchEffect(() => history.replaceState({}, '', store.serialize()))
1515

1616
// setTimeout(() => {
17-
store.setFiles(
18-
{
19-
'index.html': '<h1>yo</h1>',
20-
'main.js': 'document.body.innerHTML = "<h1>hello</h1>"',
21-
'foo.js': 'document.body.innerHTML = "<h1>hello</h1>"',
22-
'bar.js': 'document.body.innerHTML = "<h1>hello</h1>"',
23-
'baz.js': 'document.body.innerHTML = "<h1>hello</h1>"'
24-
},
25-
'index.html'
26-
)
17+
// store.setFiles(
18+
// {
19+
// 'index.html': '<h1>yo</h1>',
20+
// 'main.js': 'document.body.innerHTML = "<h1>hello</h1>"',
21+
// 'foo.js': 'document.body.innerHTML = "<h1>hello</h1>"',
22+
// 'bar.js': 'document.body.innerHTML = "<h1>hello</h1>"',
23+
// 'baz.js': 'document.body.innerHTML = "<h1>hello</h1>"'
24+
// },
25+
// 'index.html'
26+
// )
2727
// }, 1000);
2828

2929
store.setVueVersion('3.2.8')

0 commit comments

Comments
 (0)