Skip to content

Commit

Permalink
feat: add support for tsx
Browse files Browse the repository at this point in the history
  • Loading branch information
CaiWenlie committed Jan 17, 2023
1 parent 4af73cb commit d0829ca
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@ import hashId from 'hash-sum'

export const COMP_IDENTIFIER = `__sfc__`

function testTs(lang: string | null | undefined) {
return ['ts', 'tsx'].includes(lang!)
}

async function transformTS(src: string) {
return transform(src, {
transforms: ['typescript']
transforms: ['jsx', 'typescript'],
jsxPragma: 'h',
jsxFragmentPragma: 'Fragment'
}).code
}

Expand Down Expand Up @@ -74,7 +80,7 @@ export async function compileFile(
const scriptLang =
(descriptor.script && descriptor.script.lang) ||
(descriptor.scriptSetup && descriptor.scriptSetup.lang)
const isTS = scriptLang === 'ts'
const isTS = testTs(scriptLang)
if (scriptLang && !isTS) {
store.state.errors = [`Only lang="ts" is supported for <script> blocks.`]
return
Expand Down Expand Up @@ -253,7 +259,7 @@ async function doCompileScript(
expressionPlugins
)

if ((descriptor.script || descriptor.scriptSetup)!.lang === 'ts') {
if (testTs((descriptor.script || descriptor.scriptSetup)!.lang)) {
code = await transformTS(code)
}

Expand Down Expand Up @@ -304,7 +310,7 @@ async function doCompileTemplate(
`$1 ${fnName}`
)}` + `\n${COMP_IDENTIFIER}.${fnName} = ${fnName}`

if ((descriptor.script || descriptor.scriptSetup)?.lang === 'ts') {
if (testTs((descriptor.script || descriptor.scriptSetup)?.lang)) {
code = await transformTS(code)
}

Expand Down

0 comments on commit d0829ca

Please sign in to comment.