Skip to content

Commit 20ef600

Browse files
committedJan 23, 2024
feat(jsx-explorer): support tsx
1 parent d99206b commit 20ef600

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed
 

‎packages/jsx-explorer/src/index.ts

+25-10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import * as monaco from 'monaco-editor';
22
import { watchEffect } from 'vue';
33
import { transform } from '@babel/core';
44
import babelPluginJsx from '@vue/babel-plugin-jsx';
5+
// @ts-expect-error missing types
6+
import typescript from '@babel/plugin-syntax-typescript';
57
import {
68
type VueJSXPluginOptions,
79
compilerOptions,
@@ -25,6 +27,8 @@ function main() {
2527

2628
const sharedEditorOptions: monaco.editor.IStandaloneEditorConstructionOptions =
2729
{
30+
language: 'typescript',
31+
tabSize: 2,
2832
theme: 'vs-dark',
2933
fontSize: 14,
3034
wordWrap: 'on',
@@ -36,29 +40,37 @@ function main() {
3640
},
3741
};
3842

43+
monaco.languages.typescript.typescriptDefaults.setDiagnosticsOptions({
44+
noSemanticValidation: true,
45+
});
3946
monaco.languages.typescript.typescriptDefaults.setCompilerOptions({
4047
allowJs: true,
4148
allowNonTsExtensions: true,
4249
jsx: monaco.languages.typescript.JsxEmit.Preserve,
4350
target: monaco.languages.typescript.ScriptTarget.Latest,
51+
module: monaco.languages.typescript.ModuleKind.ESNext,
52+
isolatedModules: true,
4453
});
4554

4655
const editor = monaco.editor.create(document.getElementById('source')!, {
47-
value:
48-
decodeURIComponent(window.location.hash.slice(1)) ||
49-
persistedState.src ||
50-
'const App = () => <div>Hello World</div>',
51-
language: 'typescript',
52-
tabSize: 2,
5356
...sharedEditorOptions,
57+
model: monaco.editor.createModel(
58+
decodeURIComponent(window.location.hash.slice(1)) ||
59+
persistedState.src ||
60+
'const App = () => <div>Hello World</div>',
61+
'typescript',
62+
monaco.Uri.parse('file:///app.tsx')
63+
),
5464
});
5565

5666
const output = monaco.editor.create(document.getElementById('output')!, {
57-
value: '',
58-
language: 'javascript',
5967
readOnly: true,
60-
tabSize: 2,
6168
...sharedEditorOptions,
69+
model: monaco.editor.createModel(
70+
'',
71+
'typescript',
72+
monaco.Uri.parse('file:///output.tsx')
73+
),
6274
});
6375

6476
const reCompile = () => {
@@ -74,7 +86,10 @@ function main() {
7486
src,
7587
{
7688
babelrc: false,
77-
plugins: [[babelPluginJsx, { ...compilerOptions }]],
89+
plugins: [
90+
[babelPluginJsx, { ...compilerOptions }],
91+
[typescript, { isTSX: true }],
92+
],
7893
ast: true,
7994
},
8095
(err, result = {}) => {

0 commit comments

Comments
 (0)
Please sign in to comment.