Skip to content

Commit 17c2894

Browse files
committed
feat: jsx-explorer support options
1 parent 98b4f92 commit 17c2894

2 files changed

Lines changed: 99 additions & 17 deletions

File tree

packages/jsx-explorer/src/index.ts

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
/* eslint-disable no-console */
22
// eslint-disable-next-line import/no-unresolved
33
import * as m from 'monaco-editor';
4-
import { h, createApp } from 'vue';
4+
import { watchEffect } from 'vue';
55
import { transform } from '@babel/core';
66
import babelPluginJsx from '../../babel-plugin-jsx/src';
7+
import { initOptions, compilerOptions } from './options';
78
import './index.css';
89

910
declare global {
@@ -15,17 +16,6 @@ declare global {
1516

1617
window.init = () => {
1718
const { monaco } = window;
18-
createApp(
19-
() => h('h1', null, 'Vue JSX Explorer'),
20-
).mount('#header');
21-
22-
// @ts-ignore
23-
if (module.hot) {
24-
// @ts-ignore
25-
module.hot.accept('../../babel-plugin-jsx/src', () => {
26-
compile();
27-
});
28-
}
2919

3020
const sharedEditorOptions: m.editor.IStandaloneEditorConstructionOptions = {
3121
theme: 'vs-dark',
@@ -61,14 +51,15 @@ window.init = () => {
6151
...sharedEditorOptions,
6252
});
6353

64-
const compile = () => {
54+
const reCompile = () => {
6555
const src = editor.getValue();
66-
localStorage.setItem('state', src);
56+
const state = JSON.stringify(compilerOptions);
57+
localStorage.setItem('state', state);
6758
window.location.hash = encodeURIComponent(src);
6859
console.clear();
6960
transform(src, {
7061
babelrc: false,
71-
plugins: [[babelPluginJsx, { transformOn: true, optimize: true }]],
62+
plugins: [[babelPluginJsx, compilerOptions]],
7263
ast: true,
7364
}, (err, result = {}) => {
7465
const res = result!;
@@ -87,10 +78,11 @@ window.init = () => {
8778
output.layout();
8879
});
8980

90-
compile();
81+
initOptions();
82+
watchEffect(reCompile);
9183

9284
// update compile output when input changes
93-
editor.onDidChangeModelContent(debounce(compile));
85+
editor.onDidChangeModelContent(debounce(reCompile));
9486
};
9587

9688
function debounce<T extends(...args: any[]) => any>(
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import {
2+
h, reactive, createApp,
3+
} from 'vue';
4+
import { VueJSXPluginOptions } from '../../babel-plugin-jsx/src';
5+
6+
export const compilerOptions: VueJSXPluginOptions = reactive({
7+
mergeProps: true,
8+
optimize: false,
9+
transformOn: false,
10+
enableObjectSlots: true,
11+
});
12+
13+
const App = {
14+
setup() {
15+
return () => [
16+
h('h1', 'Vue 3 JSX Explorer'),
17+
h(
18+
'a',
19+
{
20+
href: 'https://app.netlify.com/sites/vue-next-jsx-explorer/deploys',
21+
target: '_blank',
22+
},
23+
'History',
24+
),
25+
26+
h('div', { id: 'options-wrapper' }, [
27+
h('div', { id: 'options-label' }, 'Options ↘'),
28+
h('ul', { id: 'options' }, [
29+
30+
// mergeProps
31+
h('li', [
32+
h('input', {
33+
type: 'checkbox',
34+
id: 'mergeProps',
35+
name: 'mergeProps',
36+
checked: compilerOptions.mergeProps,
37+
onChange(e: Event) {
38+
compilerOptions.mergeProps = (e.target as HTMLInputElement).checked;
39+
},
40+
}),
41+
h('label', { for: 'mergeProps' }, 'mergeProps'),
42+
]),
43+
44+
// optimize
45+
h('li', [
46+
h('input', {
47+
type: 'checkbox',
48+
id: 'optimize',
49+
checked: compilerOptions.optimize,
50+
onChange(e: Event) {
51+
compilerOptions.optimize = (e.target as HTMLInputElement).checked;
52+
},
53+
}),
54+
h('label', { for: 'optimize' }, 'optimize'),
55+
]),
56+
57+
// transformOn
58+
h('li', [
59+
h('input', {
60+
type: 'checkbox',
61+
id: 'transformOn',
62+
checked: compilerOptions.transformOn,
63+
onChange(e: Event) {
64+
compilerOptions.transformOn = (e.target as HTMLInputElement).checked;
65+
},
66+
}),
67+
h('label', { for: 'transformOn' }, 'transformOn'),
68+
]),
69+
70+
// enableObjectSlots
71+
h('li', [
72+
h('input', {
73+
type: 'checkbox',
74+
id: 'transformOn',
75+
checked: compilerOptions.enableObjectSlots,
76+
onChange(e: Event) {
77+
compilerOptions.enableObjectSlots = (e.target as HTMLInputElement).checked;
78+
},
79+
}),
80+
h('label', { for: 'enableObjectSlots' }, 'enableObjectSlots'),
81+
]),
82+
]),
83+
]),
84+
];
85+
},
86+
};
87+
88+
export function initOptions() {
89+
createApp(App).mount(document.getElementById('header')!);
90+
}

0 commit comments

Comments
 (0)