Skip to content

Commit 7b43186

Browse files
committed
fix(core|utils): not propertly handling type stripping
Close #17
1 parent 69314a5 commit 7b43186

File tree

7 files changed

+20
-20
lines changed

7 files changed

+20
-20
lines changed

apps/playground/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@
6060
"monaco-editor-core": "^0.52.2",
6161
"reka-ui": "^2.2.1",
6262
"splitpanes": "^4.0.3",
63-
"sucrase": "^3.35.0",
64-
"superjson": "^2.2.2",
6563
"unplugin-vue-router": "^0.12.0",
6664
"vite": "^6.3.5",
6765
"vscode-uri": "^3.1.0",

apps/playground/src/components/Editor/transform.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22

33
// https://github.com/vuejs/repl/blob/5e092b6111118f5bb5fc419f0f8f3f84cd539366/src/transform.ts
44

5-
import type { Transform } from 'sucrase'
65
import type {
76
BindingMetadata,
87
CompilerOptions,
98
SFCDescriptor,
109
} from 'vue/compiler-sfc'
1110
import type { File, Store } from './store'
1211

12+
import { transformTS } from '@velin-dev/utils/transformers/typescript'
1313
import hashId from 'hash-sum'
14-
import { transform } from 'sucrase'
1514

1615
export const COMP_IDENTIFIER = `__sfc__`
1716

@@ -23,13 +22,6 @@ function testJsx(filename: string | undefined | null) {
2322
return !!(filename && /(\.|\b)[jt]sx$/.test(filename))
2423
}
2524

26-
function transformTS(src: string, isJSX?: boolean) {
27-
return transform(src, {
28-
transforms: ['typescript', ...(isJSX ? (['jsx'] as Transform[]) : [])],
29-
jsxRuntime: 'preserve',
30-
}).code
31-
}
32-
3325
export async function compileFile(
3426
store: Store,
3527
{ filename, code, compiled }: File,
@@ -320,6 +312,7 @@ async function doCompileScript(
320312
},
321313
},
322314
})
315+
323316
let code = compiledScript.content
324317
if (isTS) {
325318
code = await transformTS(code, isJSX)

apps/playground/src/prompts/Prompt.velin.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<script setup>
1+
<script setup lang="ts">
22
import { useCounter } from '@vueuse/core'
33
44
defineProps({

packages/core/src/render-shared/compile.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import type { CompilerOptions, SFCScriptBlock, SFCTemplateCompileResults } from '@vue/compiler-sfc'
44

5-
import { testTs } from '@velin-dev/utils/transformers/typescript'
5+
import { testTs, transformTS } from '@velin-dev/utils/transformers/typescript'
66
import { compileScript, compileTemplate, parse } from '@vue/compiler-sfc'
77

88
import { isUseInlineTemplate } from './template'
@@ -45,6 +45,10 @@ export async function compileSFC(source: string): Promise<CompiledResult> {
4545
},
4646
})
4747

48+
if (isTS) {
49+
scriptResult.content = transformTS(scriptResult.content)
50+
}
51+
4852
return {
4953
template: templateResult,
5054
script: scriptResult,
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { describe, expect, it } from 'vitest'
2+
3+
import { transformTS } from './transform'
4+
5+
describe('transformTS', async () => {
6+
it('should transform TS', () => {
7+
const result = transformTS(`import { defineComponent as _defineComponent } from 'vue'\nimport { toDisplayString as _toDisplayString, createElementVNode as _createElementVNode, unref as _unref, openBlock as _openBlock, createElementBlock as _createElementBlock } from "vue"\n\nimport { useCounter } from '@vueuse/core'\n\n\nexport default /*@__PURE__*/_defineComponent({\n props: {\n text: String,\n number: Number,\n check: Boolean,\n},\n setup(__props) {\n\n\n\nconst { count } = useCounter()\n\nreturn (_ctx: any,_cache: any) => {\n return (_openBlock(), _createElementBlock("div", null, [\n _createElementVNode("div", null, _toDisplayString(__props.text), 1 /* TEXT */),\n _createElementVNode("div", null, _toDisplayString(__props.number), 1 /* TEXT */),\n _createElementVNode("div", null, _toDisplayString(__props.check), 1 /* TEXT */),\n _createElementVNode("div", null, "Internal count: " + _toDisplayString(_unref(count)), 1 /* TEXT */)\n ]))\n}\n}\n\n})`)
8+
expect(result).toBeDefined()
9+
expect(result).toEqual(`import { defineComponent as _defineComponent } from 'vue'\nimport { toDisplayString as _toDisplayString, createElementVNode as _createElementVNode, unref as _unref, openBlock as _openBlock, createElementBlock as _createElementBlock } from "vue"\n\nimport { useCounter } from '@vueuse/core'\n\n\nexport default /*@__PURE__*/_defineComponent({\n props: {\n text: String,\n number: Number,\n check: Boolean,\n},\n setup(__props) {\n\n\n\nconst { count } = useCounter()\n\nreturn (_ctx,_cache) => {\n return (_openBlock(), _createElementBlock("div", null, [\n _createElementVNode("div", null, _toDisplayString(__props.text), 1 /* TEXT */),\n _createElementVNode("div", null, _toDisplayString(__props.number), 1 /* TEXT */),\n _createElementVNode("div", null, _toDisplayString(__props.check), 1 /* TEXT */),\n _createElementVNode("div", null, "Internal count: " + _toDisplayString(_unref(count)), 1 /* TEXT */)\n ]))\n}\n}\n\n})`)
10+
})
11+
})

packages/utils/src/transformers/typescript/transform.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export function testTs(filename: string | undefined | null) {
77
return !!(filename && /(\.|\b)tsx?$/.test(filename))
88
}
99

10-
export async function transformTS(src: string, isJSX?: boolean) {
10+
export function transformTS(src: string, isJSX?: boolean) {
1111
return transform(src, {
1212
transforms: ['typescript', ...(isJSX ? (['jsx'] as Transform[]) : [])],
1313
jsxRuntime: 'preserve',

pnpm-lock.yaml

Lines changed: 0 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)